How exactly does a java gui work? Does it constantly call repaint on all its components? Or is repaint only called when you specifically call it? I think the former is what happens, at least from my code.
the problem Im having is this is supposed to be a chess board with images. Here is my code
Now the problem is the pieces dont show up! However, if I comment out the following line so the code looks like this:Code:public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
if(color == Colors.WHITE)
g.setColor(Color.white);
else
g.setColor(Color.green);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
//System.out.println("here");
if(resident != null) {
removeAll();
add(new JLabel(resident.getImage()));
}
else
removeAll();
}
The pieces are double up on the squares, like its adding too many labels. How can I work around this? Also, am I correct in assuming that repaint is constantly called? If that is true it would make sense that its wipin gout the images too fast for me to see them. Also, the other weird thing is that when I comment out that line, the images only show up once i click somwhere in the frame. Here is the code from the constructor of my JFrame that should make them appear once the frame is shown.Code:if(resident != null) {
//removeAll();
add(new JLabel(resident.getImage()));
}
Shouldnt calling repaint call the paintcomponent method of the JPanel causing the image to display?Code:for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
squares[j][i].setResident(board.getSquare(j,i).getResident());
//System.out.println(squares[j][i].getResident());
squares[j][i].repaint();
}
}
