Java – the contents of the window disappear when minimized
I have a simple class that draws a line when the mouse is dragged or a point when the mouse is pressed (released)
When I minimize the application and restore it, the contents of the window disappear, except for the last point (pixel) I know the method super Paint (g) redraws the background every time the window changes, but the result seems to be the same whether I use it or not The difference between them is that when I don't use it, I draw more than one pixel on the window, not all my paintings How can I solve this problem?
This is the class
package painting; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JFrame; import javax.swing.JPanel; class CustomCanvas extends Canvas{ Point oldLocation= new Point(10,10); Point location= new Point(10,10); Dimension dimension = new Dimension(2,2); CustomCanvas(Dimension dimension){ this.dimension = dimension; this.init(); addListeners(); } private void init(){ oldLocation= new Point(0,0); location= new Point(0,0); } public void paintLine(){ if ((location.x!=oldLocation.x) || (location.y!=oldLocation.y)) { repaint(location.x,location.y,1,1); } } private void addListeners(){ addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent me){ oldLocation = location; location = new Point(me.getX(),me.getY()); paintLine(); } @Override public void mouseReleased(MouseEvent me){ oldLocation = location; location = new Point(me.getX(),me.getY()); paintLine(); } }); addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent me){ oldLocation = location; location = new Point(me.getX(),me.getY()); paintLine(); } }); } @Override public void paint(Graphics g){ super.paint(g); g.setColor(Color.red); g.drawLine(location.x,oldLocation.x,oldLocation.y); } @Override public Dimension getMinimumSize() { return dimension; } @Override public Dimension getPreferredSize() { return dimension; } } class CustomFrame extends JPanel { JPanel displayPanel = new JPanel(new BorderLayout()); CustomCanvas canvas = new CustomCanvas(new Dimension(200,200)); public CustomFrame(String titlu) { canvas.setBackground(Color.white); displayPanel.add(canvas,BorderLayout.CENTER); this.add(displayPanel); } } public class CustomCanvasFrame { public static void main(String args[]) { CustomFrame panel = new CustomFrame("Test Paint"); JFrame f = new JFrame(); f.add(panel); f.pack(); SwingConsole.run(f,700,700); } }
Solution
You have not stored the state of the point you are drawing When a panel is redrawn, it displays only information about the last point it was drawn
Reply to comments:
You need to have a points set, such as ArrayList < point > location = new ArrayList < point > ();
Then, in your listener: location add(new Point(me.getX(),me. getY()));
Finally, in paintline():
for (Point location : locations) { repaint(location.x,1); }
Collection locations are often referred to as display lists Most graphics programs use them
Reply to comments:
Yes, I hope so I just throw an idea according to your code to give you a starting point It's almost certainly a bad idea to do exactly what I describe