binarysmacker
binarysmacker

Reputation: 1122

Java - Repaint specific components

I'm learning Java as part of my degree but its very brief but what ever I do I like to make sure I atleast have some understanding.

Until now anything that I want displayed on the screen is put into the paintcomponent method of the JPanel.

However, I have drawn out some parts of my layout which never change, and only a certain thing in the middle rotates. I have a timer which calls repaint().

If I correct in thinking that everything including the components that never change are being removed and then redrawn and the whole paintcomponent method is being run every time.

To me I feel like I should (or there must be) a way where the static stuff is moved out / is only drawn once, and only the parts that I specifically want to be redrawing should stay in the paintcomponent method?

Is this correct or am I not fully understanding something?

Upvotes: 0

Views: 1016

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

  • I am assuming that you are not adding GUI components in the paintComponent method, right?
  • Components are not "removed" during a repaint
  • Better to put the stable part of any image into a single background BufferedImage and draw the BufferedImage in the paintComponent method. This can make for more efficient painting.
  • Consider calling an overload of repaint(...) that sets a bounding rectangle for the region to be painted.

Upvotes: 2

Related Questions