0

I have the following code:

    private void gardenJPanelMouseClicked(java.awt.event.MouseEvent evt) {                                          
        Graphics g = this.gardenJPanel.getGraphics();
        Graphics2D draw = (Graphics2D) g;

        int x = evt.getX();
        int y = evt.getY();
       
        draw.setStroke(new BasicStroke(pointStroke));

        draw.drawLine(x, y, x, y);
    }            

This following code draws on the JPanel perfectly. The only problem is that the drawings on the JPanel get reset when the tab is switched. How can I prevent the JPanel from getting reset to blank when I switch tabs? I cant figure out the issue.

camickr
  • 321,443
  • 19
  • 166
  • 288
Van66a
  • 1
  • 1
  • When performing custom painting on a component you should override the paintComponent method, otherwise you will lose the changes every time the panel is refreshed. The official java tutorial has an excellent section on how to work with custom painting: https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html – sorifiend Jul 22 '21 at 03:28
  • @sorifiend Can you provide an example on how to override it with my code? I saw that, and I am extremely confused. I am not sure how to do it with the GUI generator. Please help. – Van66a Jul 22 '21 at 03:34
  • @sorifiend I am using Net Beans. Also, I need to use a JPanel for this. – Van66a Jul 22 '21 at 03:39
  • @sorifiend Please take a look at my reply below. I need help on how to do this with GUI generator. – Van66a Jul 22 '21 at 04:51

1 Answers1

1

The short answer to your problem is that you need to create a custom component and override the paintComponont method of that JPanel etc, and then we can perform our custom painting in that method.

For example, we can make a class that extends JPanel that also includes the mouse click event:

public class MyCustomPanel extends JPanel implements MouseListener 
{
    //If you want to dynamically draw dots then use a list to manage them
    ArrayList<Point> points = new ArrayList<>();
    
    //Here is where the painting happens, we need to override the default paint behaviour, then add our own painting
    @Override
    protected void paintComponent(Graphics g)
    {
        //Call this first to perform default painting (borders etc)
        super.paintComponent(g);
    
        //Here we can add our custem painting
        Graphics2D draw = (Graphics2D) g;
        draw.drawString("Example painting", 10, 10);
    
        //If you want to dynamically draw dots then use a list to manage them:
        for (Point point : points)
        {
            draw.drawLine(point.x, point.y, point.x, point.y);
        }
    }
    
    //Add a new point and refresh the graphics
    @Override
    public void mouseClicked(MouseEvent e)
    {
        points.add(new Point(e.getX(), e.getY()));
        this.repaint();
    }
}

Then to insert the myCustomPanel we don't use the UI generator, instead we can add it directly to the JFrame like this:

MyCustomPanel panel = new MyCustomPanel();
yourJFframe.add(new myCustomPanel());
sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • I am still having trouble. In private void gardenJPanelMouseClicked function, I am still not sure what to add. – Van66a Jul 22 '21 at 04:12
  • I need help implementing this with my current function. – Van66a Jul 22 '21 at 04:23
  • @Van66a You can remove `gardenJPanelMouseClicked` function, this class replaces the panel and the click function, and will successfully draw a dot at every point you click on. If you are unsure how the use this answer then you really need to get an understanding of how java works by taking the [swing tutorials](https://docs.oracle.com/javase/tutorial/uiswing/index.html) and especially the last one on [custom painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – sorifiend Jul 22 '21 at 22:41
  • One other option that is slightly messy is to draw your changes onto a buffered image, and then every time you draw something new you can simply set that updated [image as the JPanel background](https://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background#19125946), or you can use a JLabel component that you can assign a background image to. – sorifiend Jul 22 '21 at 22:45