The Artima Developer Community
Sponsored Link

Java Answers Forum
draw behind component.

3 replies on 1 page. Most recent reply: Apr 7, 2005 10:31 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

draw behind component. Posted: Apr 7, 2005 7:30 AM
Reply to this message Reply
Advertisement
I have a specialized panel containing some RadioButtons.

Now I need to draw some lines and Polygons on this panel.

So I did overwrite the paint method this way:
public void paint (Graphics g) {
  super.paint(g);
  g.drawLine (...);
  g.fillPolygon (...);
  // *1
}


The consequence is, that the Radio Buttons get overpainted wih the lines.

So at *1 I called the paint method of the buttons again, but that did not make any difference.

It also makes no difference, if the RadioButtons are opaque or not.

Is there a way to do this right?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: draw behind component. Posted: Apr 7, 2005 7:34 AM
Reply to this message Reply
Edit: I called of course the repaint() method of the items, since calling paint(g) would cause the RadioButton to be painted in the left upper corner of the panel.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: draw behind component. Posted: Apr 7, 2005 11:10 AM
Reply to this message Reply
Hi,

If you are using Swings then you should override the paintComponent() method instead of paint() method for custom painting on the component.

You problem will be solved by overriding paintComponent() method of JPanel.
public void paintComponent(Graphics g) {
  super.paintComponent(g); // don't forget to call this
  g.drawLine (...);
  g.fillPolygon (...);
}


While perfoming paint operation in swings the following methods get called in the given order:
1. paintComponent() - paints the component itself.
2. paintBorder() - draw the border of the component.
3. paintChildren() - asks the child controls to paint themselves.

These methods are called from the paint() method, therefore overriding of paint() method should be avoided.

Regards,
Amol Brid.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: draw behind component. Posted: Apr 7, 2005 10:31 PM
Reply to this message Reply
It worked!

Thanks a lot.

Flat View: This topic has 3 replies on 1 page
Topic: Code not giving desired output Previous Topic   Next Topic Topic: Issues Initialzing and Accessing Integer Array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use