Search code examples
javaswinggraphicsawtpaintcomponent

Making a graphics object move


In my code I override the paintComponent method and draw a rectangle inside JPanel. The JPanel is then added to JFrame and I use a method that updates the position of the rectangle via a Thread.

The rectangle does actually move .. but only when I minimize the window and maximize it back again. So in other words.. it doesn't do it as I'm looking at it.. the rectangle will just sit there.. but after I minimize and bring the window back up, the rectangle will have moved.

What can I do to fix this?


Solution

  • I am guessing you forgot to call the Repaint of the JPanel whose paintComponent you are overriding everytime you update the object. The reason you are only seeing the change on minimize/maximize is because that is when swing calls the repaint of the component.