Repaint() doesn’t work

Somehow repaint doesn’t work when I try to call it. It doesn’t give an error, it looks like it just ignores the whole function.

Here is my code:

public class Neerslag extends JComponent {
JPanel painting = new JPanel();
private int j = 0;

  Neerslag() {
    setPreferredSize(new Dimension(370, 200));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.GRAY);

    g.fillOval(j, 20, 100, 20);
    g.fillOval(j + 10, 30, 100, 20);
    System.out.println("test2");

  }

  public void move(int j) {
    System.out.println("test");
    if (j < 50) {
        j++;
    } else {
        j = -j;
    }
    /*try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        System.err.println("sleep exception");
    }*/
    this.repaint();
  }

  public JPanel showPaint() {
    painting.add(new Neerslag());
    return painting;
  }

  public void Clear() {
    painting.repaint();
    System.out.println("hello");
  }
}

and

public class NeerslagFrame extends JFrame implements MouseListener {
NeerslagPanel panel1 = new NeerslagPanel();;
private int i=0;
Neerslag neerslag = new Neerslag();

public static void main(String[] args) {
    NeerslagFrame textFieldFrame = new NeerslagFrame();
    textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    textFieldFrame.setSize(500, 350);
    textFieldFrame.setVisible(true);

}

public NeerslagFrame(){
    super ("Neerslag");
    //panel1 = 
    this.addMouseListener(this);
    this.add(panel1.addSideBar(), BorderLayout.EAST);
    this.add(panel1.addSouthBar(), BorderLayout.SOUTH);
    this.add(neerslag.showPaint());
}
public void Clear(){
    //Neerslag paint = new Neerslag();
    this.add(new Neerslag());
}

@Override
public void mouseClicked(MouseEvent arg0) {
    neerslag.move(i);
    i++;
    neerslag.repaint();
    neerslag.revalidate();
    //this.add(neerslag.showPaint());
}
@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


}

What have I missed? It does show the initial two ovals, but just doesn’t repaint them once I changed the “j” value. Thank you in advance!

Answer

repaint isn’t really what you want, what you “seem” to want is revalidate AND repaint.

Something like…

@Override
public void mouseClicked(MouseEvent arg0) {
    neerslag.move(i);
    i++;
    neerslag.revalidate();
    neerslag.repaint();
    this.add(neerslag.showPaint());
}

for example.

Now, for what it’s worth, your design seems backwards, that is, rather than having Neerslag container some other container which seems to then add instance of itself to it, instead Neerslag should simply care only about itself and nothing else.

You should move the painting panel out and manage it separately, as Neerslag simply should not care about it – IMHO

Leave a Reply

Your email address will not be published. Required fields are marked *