aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes graphics question: paint(), repaint(), update() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "graphics question: paint(), repaint(), update()" Watch "graphics question: paint(), repaint(), update()" New topic
Author

graphics question: paint(), repaint(), update()

litzhr litzhr
Greenhorn

Joined: Sep 06, 2004
Posts: 3
Hi

I have a GUI which consists of swing components and I want to paint onto them. For example if somebody select a component I want to paint a rectangle around it.

The select event stores the component in a variable and calls repaint(). This shoul call paint() which looks like this:

public void paint( graphics g){

draw the rectangle

super.paint(g);

}

The rectangle is actually painted for half a second. Then it is gone again. I want to have it painted permanentely. Why is the rectangle erased after that? Is it because of the update() method?

I tried to overwrite the update method with a plain method:

public void update(graphics g) { }

it didnt do anything.


Another question. I have to paint differnt stuff regarding what events are thrown. Is it a good programming style to put all the draw stuff into the paint method and look what happend like:

if(event a)

else if(event b)

...
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The call to super.paint(g) should cause the Rectangle to be painted over when the Container paints its child components (as advertised: see paint method in JComponent api Method Detail). If you want the rectangle to persist, you could draw it after the call to super.paint. The paint method will draw on top of its parent (enclosing class) container and all of the child components.

If not drawing on top of child components of a Container, other options include:

1 — set a border on the selected component, eg

2 — you could override the paintComponent method, get the bounds of the selected component and use it to draw your rectangle around the component.

About manipulating variables inside your paint code from within your event code: yes, this is considered good practice. You can use booleans, int flags, null state and the like. The paint code should be concerned only with rendering your graphics/images. The event code is where you control what goes on in the paint code.
[ November 15, 2004: Message edited by: Craig Wood ]
litzhr litzhr
Greenhorn

Joined: Sep 06, 2004
Posts: 3
thanks! I just got it when i was driving. Just call it after the drawing. OHh man!


The border thing is much more elegant I think.
 
Consider Paul's rocket mass heater.
 
subject: graphics question: paint(), repaint(), update()