简体   繁体   中英

Override paint(graphics g) Java

I have a little issue with my GUI in NetBeans. I draw images (dots) when a user clics in a JPanel at the mouse clic location. This part works just fine. I store each image locations in two different ArrayList that contains the X location and Y location. Now what I want to do is to delete the latest image drawn in the Panel after a button is clicked. So what I did is remove the last index of both ArrayList, and then call repaint() to draw all the images from the locations in both X and Y ArrayList (code below).

What is weird is that I need to resize the GUI (put it in full screen or just change its' size) in order for the drawn images to show up again in the JPanel otherwise, the panel remains empty.

Here's the parts of code that are affected :

public void paint(Graphics g) {

    super.paint(g);
    for(int i=0;i<=listePointsX.size()-1;i++) {
        try{
            BufferedImage icon = ImageIO.read(getClass().getResourceAsStream("/myimage.png"));
            Graphics graphe = jPanel1.getGraphics();
            graphe.setColor(Color.BLACK);
            graphe.drawImage(icon, this.listePointsX.get(i),this.listePointsY.get(i), rootPane);
        }catch(Exception e1){

        }
    }

private void jButtonUndoActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if(listePointsX.size()>0){
        int lastObject= listePointsX.size();
        listePointsX.remove(lastObject-1);
        listePointsY.remove(lastObject-1);
        jPanel1.repaint();         
    }
    else{

    }


}   

Any idea what I need to do to some kind of "refresh" the whole thing? Am I doing something wrong? Tried searching about that but did not find anyting...

Graphics graphe = jPanel1.getGraphics(); is NOT how painting should work, instead, you should have overriden the panel's paintComponent method and painted the points within in.

See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing

Instead, your panel should be doing ALL the work, managing the points in the ArrayList and painting them. You parent component "might" have the ability to add or remove points if that meets your design requirements, but the core responsibility remains with the panel.

Avoid performing any long running or block operations within the paint methods, they should run as fast as possible. Since the image never changes, you should simply load once (either when the class is constructed or when you first need the image) and keep using the same reference.

Alright it worked just fine now. I had to do it the way you told me up here. I created a new class that extends jPanel (below). Then in my main Form, had to create an object of this class. Whenever a user makes a click, it will call this Drawing class object and add an item to the ArrayList (this object manages everything regarding created points... It looks like this :

public class MyDrawingClass extends JPanel {
ArrayList<Integer> arrayListPointX = new ArrayList<>();
ArrayList<Integer> arrayListPointY = new ArrayList<>();

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    try{
    BufferedImage icon = ImageIO.read(getClass().getResourceAsStream("/images/dot.png"));
    g.setColor(Color.BLACK);
    if(arrayListPointX.size()<=0){
        ...
    }
    else{
        for(int i=0;i<listePointsX.size();i++){
                g.setColor(Color.BLACK);
                g.drawImage(icon, listePointsX.get(i), listePointsY.get(i), rootPane);
        }
    }
    }catch(Exception e){
        ...
    }
}

So if I want to "undo", let's say my object of "MyDrawingClass" is called "draw". I can do : draw.arrayListPointX.remove(draw.arrayListPointX.size()-1); and call repaint(); to display remaining points.

Thanks for your tips appreciate it ! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM