Drawing on a JPanel of a form

Answered

Dear Intelli J community,

I just started using Intelli J Idea and one of my first projects is to plot some geometric forms to a JPanel of a GUI defined in a form. Therefore, I found a tutorial where a class extending the JPanel was defined and the paintCompontent() method was overloaded. 


public class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int y2 = (int)(40 * Math.random());
Line2D line = new Line2D.Double(10, 10, 60, y2);
Rectangle2D rectangle = new Rectangle2D.Double(200, 120, 70, 30);
Ellipse2D oval = new Ellipse2D.Double(400, 200, 40, 60);
g2.draw(line);
g2.setPaint(Color.RED);
g2.fill(rectangle);
g2.setPaint(Color.ORANGE);
g2.fill(oval);
}
}

This would run fine if Iuse it together with this code:

public class MainClass {
public static void main(String[] args) {
MyPanel s = new MyPanel();
JFrame f = new JFrame();
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
}
}

Then I tried combining this with a form I created. And this is where I have problems. I would like to have a form with a button and a JPanel. When I press the button the same geometric figures as above are being drawn on the JPanel defined in the form. I think my best try is like this:

public class MainWindow {
private JPanel panelMain;
private JButton buttonCalculate;
private JPanel panelPlot;

public MainWindow() {
buttonCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panelPlot = new MyPanel();
panelPlot.setBackground(Color.CYAN);
panelPlot.setSize(200, 200);
panelPlot.setVisible(true);
}
});
}

public static void main(String[] args) {
JFrame f = new JFrame("MyFirstGraphTool");
f.setContentPane(new MainWindow().panelMain);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setVisible(true);
}
}



But simply saving my derived JPlane object to the bound property does not change anything.
And also the setBackgroundColor() method does not change anything.

Do you know any tutorials or more detailed explanation of how this can be done?

Thanks and kind regards,
David
0
6 comments

Use custom create option for the panel if you want to construct it manually instead of using the GUI Designer code that will override your customizations.

Another option is to create another JPanel manually from your code and add it to the existing JPanel that was created with the GUI Designer.

1
Avatar
Permanently deleted user

Thanks for the answer. First I tried adding my custom JPanel to the existing one using the code below. But this causes a null pointer exception. I tried further and found out that I can't add components to components created in the form at all. Is there somewhere an example for this?

public class MainWindow {
private JButton buttonPrint;
private JPanel panelPlotFrame;
private JPanel panelMain;

public MainWindow() {
buttonPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel diagram = new MyPanel();
panelPlotFrame.add(diagram);
}
});
}

public static void main(String[] args) {
JFrame main_frame = new JFrame();
main_frame.setContentPane(new MainWindow().panelMain);
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_frame.setResizable(false);
main_frame.setSize(700, 600);
main_frame.setVisible(true);
}
}


0

Please share the complete project that doesn't work for you: https://uploads.services.jetbrains.com/.

0
Avatar
Permanently deleted user

Thanks for the help. I uploaded the file:

PanelTest.zip


0

Change the layout manager to something simple:

BorderLayout should work, see https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

After you add the component to the panel dynamically, it must be revalidated.

buttonPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel diagram = new PlotPanel();
panelPlotFrame.add(diagram);
panelPlotFrame.revalidate();
}
});
1
Avatar
Permanently deleted user

Thanks a lot!

Works now in combination with the graphical GUI editor.

0

Please sign in to leave a comment.