I just get into the graphics in Java and I have a problem. I created a JFrame window (NetBeans constructor) with a JPanel panel, and I drew some graphics on it. Then I added JButton, which changed a variable that would change the position of the X square to JPanel.
On, click this code:
drawObject.setX(150);
drawObject.repaint();
drawObject is an instance of this class:
public class sola extends JPanel {
private int x = 10;
@Override
public void paintComponent(Graphics g){
super.paintComponents(g);
super.setBackground(Color.WHITE);
g.setColor(Color.ORANGE);
g.fill3DRect(x, 160, 100, 50, true);
}
public void setX(int xX){
x = xX;
}
}
Now when I press JButton, the rectangle moves to a new position, however it is still displayed in the old position. Only when I resize the window does it refresh and the old rectangle disappears. How can I solve this problem so that when I press the button, the rectangle is visible only in the new position?
source
share