I have this problem bothering me for days. I’m making a special paint program. I made a JPanel, and added custom jComponents which are painted using the paint(..) method.
The problem is, whenever I resize the window, all the added components “disappear” (or just don’t paint) so I end up with a empty frame.
Also i noticed some strange behaviors of swing when using this method. I have added comments to the code describing this problem.
package simple;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class SimpleFrame extends JFrame {
JPanel paintArea;
SimpleCanvas c1;
SimpleCanvas c2;
ArrayList<SimpleCanvas> list;
public static void main(String[] args) {
SimpleFrame frame = new SimpleFrame();
}
public SimpleFrame() {
super("Test");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//The panel to which my SimpleCanvas objects are added
paintArea = new JPanel();
paintArea.setPreferredSize(new Dimension(600, 500));
paintArea.addMouseListener(new paintAreaMouseEvents());
getContentPane().add(paintArea, BorderLayout.CENTER);
setVisible(true);
paintArea.setVisible(true);
//A list to hold all the objects together
list = new ArrayList<SimpleCanvas>(10);
//The same as in class paintAreaMouseEvent, but doesnt work
SimpleCanvas c = new SimpleCanvas();
c.setBounds(60, 100, 100, 50);
list.add(c);
paintArea.add(list.get(list.size() - 1));
paintArea.repaint();
}
//When you click the mouse, it makes an oval
class paintAreaMouseEvents extends MouseAdapter {
@Override
//This does work.
public void mouseClicked (MouseEvent me) {
SimpleCanvas c = new SimpleCanvas();
c.setBounds(me.getX() - 50, me.getY() - 25, 100, 50);
list.add(c);
paintArea.add(list.get(list.size() - 1));
paintArea.repaint();
}
}
}
And here is the SimpleCanvas class
package simple;
import java.awt.*;
import javax.swing.JComponent;
public class SimpleCanvas extends JComponent {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillOval(0, 0, 100, 50);
}
}
Thanks 🙂
BTW: Just wanted to say this site is amazing. I came here a lot while using Google, and now I finally decided to make an account.
Your problem is a layout problem. Add:
to your JComponent’s paint method to see what happens when you resize the GUI.
This is happening because the resize is invoking the layout manager actions.
To solve this, don’t use
setBounds(...)to size components but rather the layout managers. Also override your JComponent’s getPreferredSize method if you want the layout managers to respect a specific size for it. Finally, don’t paint with thepaintmethod but rather thepaintComponentmethod. The tutorials will explain why.Also, if you absolutely need to position something using absolute positioning, then the container must use a null layout:
Edit
If I were doing something like your program above, I’m not sure that I’d add new components to a JPanel but rather would simply have the drawing JPanel hold a list of Shapes and then have the shapes drawn in its paintComponent method using a for loop. For example:
Edit 2
with dragging of shapes