Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
litzhr litzhr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic