Hello!
I'm a VB.net developer that is trying to learn Java. I am using IntelliJ on Windows 10.
Using Swing, I have created a simple JFrame with 2 buttons, Render and OK. There is also a JPanel in the center of the frame. Clicking "OK" exits the program. What I would like to do is click "Render" and have a line drawn onto the JPanel.
This is the code I have:
import javax.swing.*; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Graphics; import java.awt.Color; public class MainForm extends JPanel{ private JButton btnOK; private JPanel pnlMainPanel; private JButton btnRender; private JPanel pnlImage; private static int width = 800; private static int height = 600; public MainForm() { btnOK.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnRender.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); } public static void main(String[] args) { JFrame frame = new JFrame("Drawing Test"); frame.setContentPane(new MainForm().pnlMainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
What I don't know how to do, and cant figure out, is how to invoke the JPanel's paintComponent() from the action listener of the Render button. Thanks in advance for any help and/or suggestions.