Skip to main content

[JAVA2D] repainting JPanel

11 replies [Last post]
Anonymous

I'm having problems trying to figure out how to redraw a JPanel with
custom graphics after a JDialog, displayed on top of the JPanel, has
been disposed of. Also having the same problem when the window has lost
focus or been maximized after being minimized to toolbar.

If I overwrite the paintComponent() method of the JPanel, will Java know
to paint the JPanel after the JDialog has been disposed, or if focus has
returned to the window? Or do I have to implement focusListeners with
the paintComponent() method?

Thanks in advance,
Walter Shirey

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Andrei Kouznetsov

>>no need to call drawingPanel.getGraphics() in drawMap() because you
>have
>>alredy Graphics from paintComponent.
>
>>drawingPanel = new JPanel(){
>> public void paintComponent(Graphics g){
>> super.paintComponent(g);
>> drawMap((Graphics2D)g);
>> }
>> };
>
>>private void drawMap(Graphics2D g2){
>>g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALU
>E
>>ANTIALIAS_ON);
>> // custom paint code
>> }
>
>If I do this, nothing is drawn! It shouldn't have any effect on
>repainting overlapping areas, which is the problem I'm having. I'm just
>creating an extra Graphics object.

then use Graphics#create():

drawingPanel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
drawMap((Graphics2D)g.create());
//or
//drawMap((Graphics2D)g.create(x, y, w, h));
}
};

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Dmitri Trembovetski

On Mon, Feb 09, 2004 at 01:31:40AM -0700, Andrei Kouznetsov wrote:
> >>no need to call drawingPanel.getGraphics() in drawMap() because you
> >have
> >>alredy Graphics from paintComponent.
> >
> >>drawingPanel = new JPanel(){
> >> public void paintComponent(Graphics g){
> >> super.paintComponent(g);
> >> drawMap((Graphics2D)g);
> >> }
> >> };
> >
> >>private void drawMap(Graphics2D g2){
> >>g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALU
> >E
> >>ANTIALIAS_ON);
> >> // custom paint code
> >> }
> >
> >If I do this, nothing is drawn! It shouldn't have any effect on
> >repainting overlapping areas, which is the problem I'm having. I'm just
> >creating an extra Graphics object.

Note that by calling component.getGraphics() you were not just
creating an extra Graphics object, but a graphics context to render
to the screen directly, bypassing the double-buffering swing
painting mechanism. In a well-behaved swing application this should
never be done for the exact reasons you're experiencing.

Andrei's suggestion is the correct way to do it.

Thank you,
Dmitri

>
> then use Graphics#create():
>
> drawingPanel = new JPanel(){
> public void paintComponent(Graphics g){
> super.paintComponent(g);
> drawMap((Graphics2D)g.create());
> //or
> //drawMap((Graphics2D)g.create(x, y, w, h));
> }
> };
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Walter H Shirey

>Are you mixing heavyweight and lightweight components?

>http://java.sun.com/products/jfc/tsc/articles/mixing/

No, they're all Swing components. I'm using NetBeans if that makes any
difference.

-Walter

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Walter H Shirey

>>drawingPanel = new JPanel(){
>> public void paintComponent(Graphics g){
>> super.paintComponent(g);
>> drawMap();
>> }
>> };
>>
>>private void drawMap(){
>> Graphics2D g2 = (Graphics2D) drawingPanel.getGraphics();
>>
>>g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VAL
UE
>>_ANTIALIAS_ON);
>> // custom paint code
>> }
>>

>no need to call drawingPanel.getGraphics() in drawMap() because you
have
>alredy Graphics from paintComponent.

>drawingPanel = new JPanel(){
> public void paintComponent(Graphics g){
> super.paintComponent(g);
> drawMap((Graphics2D)g);
> }
> };

>private void drawMap(Graphics2D g2){
>g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALU
E
>ANTIALIAS_ON);
> // custom paint code
> }

If I do this, nothing is drawn! It shouldn't have any effect on
repainting overlapping areas, which is the problem I'm having. I'm just
creating an extra Graphics object.

Walter

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Andrei Kouznetsov

>drawingPanel = new JPanel(){
> public void paintComponent(Graphics g){
> super.paintComponent(g);
> drawMap();
> }
> };
>
>private void drawMap(){
> Graphics2D g2 = (Graphics2D) drawingPanel.getGraphics();
>
>g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE
>_ANTIALIAS_ON);
> // custom paint code
> }
>

no need to call drawingPanel.getGraphics() in drawMap() because you have
alredy Graphics from paintComponent.

drawingPanel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
drawMap((Graphics2D)g);
}
};

private void drawMap(Graphics2D g2){
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE
_ANTIALIAS_ON);
// custom paint code
}

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Walter H Shirey

>If I overwrite the paintComponent() method of the JPanel, will Java
know to paint the JPanel after the >JDialog has been disposed, or if
focus has returned to the window?

I'm having problems redrawing areas in my JPanel where either a JMenu or
a JPopupMenu is overlapping. It will redraw everything correctly, except
the overlapped area? I tried to figure out what was wrong, but cannot
determine where the problem is.

Here is part of my code:

drawingPanel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
drawMap();
}
};

private void drawMap(){
Graphics2D g2 = (Graphics2D) drawingPanel.getGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE
_ANTIALIAS_ON);
// custom paint code
}

Thanks in advance,
Walter Shirey
========================================================================
=== To unsubscribe, send email to listserv@java.sun.com and include in
the body of the message "signoff JAVA2D-INTEREST". For general help,
send email to listserv@java.sun.com and include in the body of the
message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

David Eisner

Are you mixing heavyweight and lightweight components?

http://java.sun.com/products/jfc/tsc/articles/mixing/

-David

On Sat, 7 Feb 2004, Walter H Shirey wrote:

> a JPopupMenu is overlapping. It will redraw everything correctly, except
> the overlapped area? I tried to figure out what was wrong, but cannot
> determine where the problem is.

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Walter H Shirey

Can I override the paintComponent() method during initialization of the
JPanel object ?

JPanel jp = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
// custom paint code;
}
};

Thanks,
Walter Shirey

-----Original Message-----
From: Dmitri Trembovetski [mailto:Dmitri.Trembovetski@SUN.COM]
Sent: Fri 2/6/2004 12:46 PM
To: JAVA2D-INTEREST@JAVA.SUN.COM
Cc:
Subject: Re: [JAVA2D] repainting JPanel

Hi Walter,

On Fri, Feb 06, 2004 at 12:42:15PM -0600, Walter H Shirey wrote:
> I'm having problems trying to figure out how to redraw a
JPanel with
> custom graphics after a JDialog, displayed on top of the
JPanel, has
> been disposed of. Also having the same problem when the
window has lost
> focus or been maximized after being minimized to toolbar.
>
> If I overwrite the paintComponent() method of the JPanel,
will Java know
> to paint the JPanel after the JDialog has been disposed, or
if focus has
> returned to the window? Or do I have to implement
focusListeners with
> the paintComponent() method?

Swing/AWT will figure out which part needs to be redrawn and
will
call paintComponent with the appropriate clip. So, all you
need to
do is repaint your component in paintComponent (you might want
to
check the clip bounds to optimize rendering).

Thank you,
Dmitri

========================================================================
===
To unsubscribe, send email to listserv@java.sun.com and include
in the body
of the message "signoff JAVA2D-INTEREST". For general help,
send email to
listserv@java.sun.com and include in the body of the message
"help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Dave Kavanagh

yup! aren't anonymous classes neat? In this case, I wouldn't do it. I save it
for much lighter weight stuff.

David

Quoting Walter H Shirey :

> Can I override the paintComponent() method during initialization of the
> JPanel object ?
>
> JPanel jp = new JPanel(){
> public void paintComponent(Graphics g){
> super.paintComponent(g);
> // custom paint code;
> }
> };
>
> Thanks,
> Walter Shirey
>
>
>
> -----Original Message-----
> From: Dmitri Trembovetski [mailto:Dmitri.Trembovetski@SUN.COM]
> Sent: Fri 2/6/2004 12:46 PM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Cc:
> Subject: Re: [JAVA2D] repainting JPanel
>
>
>
> Hi Walter,
>
> On Fri, Feb 06, 2004 at 12:42:15PM -0600, Walter H Shirey wrote:
> > I'm having problems trying to figure out how to redraw a
> JPanel with
> > custom graphics after a JDialog, displayed on top of the
> JPanel, has
> > been disposed of. Also having the same problem when the
> window has lost
> > focus or been maximized after being minimized to toolbar.
> >
> > If I overwrite the paintComponent() method of the JPanel,
> will Java know
> > to paint the JPanel after the JDialog has been disposed, or
> if focus has
> > returned to the window? Or do I have to implement
> focusListeners with
> > the paintComponent() method?
>
> Swing/AWT will figure out which part needs to be redrawn and
> will
> call paintComponent with the appropriate clip. So, all you
> need to
> do is repaint your component in paintComponent (you might want
> to
> check the clip bounds to optimize rendering).
>
> Thank you,
> Dmitri
>
>
> ========================================================================
> ===
> To unsubscribe, send email to listserv@java.sun.com and include
> in the body
> of the message "signoff JAVA2D-INTEREST". For general help,
> send email to
> listserv@java.sun.com and include in the body of the message
> "help".
>
>
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Nidel, Michael

Java's repainting system can be pretty mystifying at times. But in this case the behavior SHOULD* be straightforward.
Swing should* detect when your JDialog has been closed, and the RepaintManager should* automatically call your
component's paint() method (with the correct clipping region). Unless you override paint() it should call paintComponent().

Of course from your explanation, for some reason you are not getting repainted. Check if your paintComponent()
method is actually being called at this point -- if so, check the clipping region on the Graphics argument.

It might also be helpful to know what platform you are working with -- even though Java should* work on all platforms,
the behavior is not guaranteed to be consistent, especially with something as OS-driven as windowing/repaints.

Mike

* - not a guarantee

-----Original Message-----
From: Discussion list for Java 2D API [mailto:JAVA2D-INTEREST@JAVA.SUN.COM]On Behalf Of Walter H Shirey
Sent: Friday, February 06, 2004 1:42 PM
To: JAVA2D-INTEREST@JAVA.SUN.COM
Subject: [JAVA2D] repainting JPanel

I'm having problems trying to figure out how to redraw a JPanel with custom graphics after a JDialog, displayed on top of the JPanel, has been disposed of. Also having the same problem when the window has lost focus or been maximized after being minimized to toolbar.

If I overwrite the paintComponent() method of the JPanel, will Java know to paint the JPanel after the JDialog has been disposed, or if focus has returned to the window? Or do I have to implement focusListeners with the paintComponent() method?

Thanks in advance,
Walter Shirey
=========================================================================== To unsubscribe, send email to listserv@java.sun.com and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Dmitri Trembovetski

Hi Walter,

On Fri, Feb 06, 2004 at 12:42:15PM -0600, Walter H Shirey wrote:
> I'm having problems trying to figure out how to redraw a JPanel with
> custom graphics after a JDialog, displayed on top of the JPanel, has
> been disposed of. Also having the same problem when the window has lost
> focus or been maximized after being minimized to toolbar.
>
> If I overwrite the paintComponent() method of the JPanel, will Java know
> to paint the JPanel after the JDialog has been disposed, or if focus has
> returned to the window? Or do I have to implement focusListeners with
> the paintComponent() method?

Swing/AWT will figure out which part needs to be redrawn and will
call paintComponent with the appropriate clip. So, all you need to
do is repaint your component in paintComponent (you might want to
check the clip bounds to optimize rendering).

Thank you,
Dmitri

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".