wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Graphics2D window not being repaired? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics2D window not being repaired?" Watch "Graphics2D window not being repaired?" New topic
Author

Graphics2D window not being repaired?

Walter Sheets
Greenhorn

Joined: Jan 23, 2009
Posts: 8
Here's a trivial test program to illustrate my newbie question.
I used NetBeans to do this and it created lots of setup code
that's not included here:


This example draws a diagonal line across the window, which
moves as you resize the window.

Now, the line redraws properly while resizing the window, but
doesn't get repaired when it's damaged.

If I remove the super.paint(g), then the line gets repaired okay,
but I wind up with lots of leftover lines that shouldn't be there.

Clearly there are some basic concepts I don't understand yet,
even after reading the tutorial on writing custom paint code.

Many thanks for any clues!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You would do well to read up on the graphics sections of the Sun Java tutorials. For one, you shouldn't paint directly on a JFrame but rather on a JComponent such as a JPanel. When you do this, don't override paint but instead paintComponent. For instance, run this code:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Pete Stein is right, and notice the first line of the paintComponent() method. This ensures that before repainting your panel returns to its original appearance, so you don't get multiple drawings superimposed on one another.
Walter Sheets
Greenhorn

Joined: Jan 23, 2009
Posts: 8
Thanks to both for very helpful answers. I'm clear now that the NetBeans IDE
is causing my confusion.

In Pete's example, his new class extends Object (by default IIUC) and then
defines a method that adds the top level JFrame and the JPanel to the JFrame.

You don't override 'paint' because the Object class doesn't have a paint method,
which is defined instead in awt.Component.

NetBeans OTOH, seems to turn this arrangement on it's head. You start by
defining the top level JFrame and then drag the JPanel onto it, which leaves
me with no way to add new methods for the JPanel -- that I can see, anyway.
Adding the JPanel seems to serve no purpose at all, in fact.

All of this confusion is caused entirely by the NetBeans IDE, not by the java
tutorials I've read. (And I *have* read them, but not often enough yet, it
seems. I think I need to understand UI delegates before any of this will
make sense to me.)

Do either of you use NetBeans for this kind of thing? Any hints?

Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Walter Sheets wrote:You don't override 'paint' because the Object class doesn't have a paint method,
which is defined instead in awt.Component.

I could override paint in my JPanel anonymous inner class, but I choose to instead override the paintComponent method of this class, because in Swing paint not only paints the component but is responsible for painting its borders and its children. If my app doesn't change the way the borders or children are painting, and if I don't want to worry about ruining this part of the GUI, I am better off overriding the method that is specifically involved with the components painting and only its painting, paintComponent.

NetBeans OTOH, seems to turn this arrangement on it's head. You start by
defining the top level JFrame and then drag the JPanel onto it, which leaves
me with no way to add new methods for the JPanel -- that I can see, anyway.
Adding the JPanel seems to serve no purpose at all, in fact.

I believe that you can create a class that extends JPanel through NetBeans and use this JPanel as I do.


All of this confusion is caused entirely by the NetBeans IDE, not by the java
tutorials I've read. (And I *have* read them, but not often enough yet, it
seems. I think I need to understand UI delegates before any of this will
make sense to me.)

Don't feel bad. I'm still reading and rereading these same tutorials.

Do either of you use NetBeans for this kind of thing?

Since I'm still in the learning phase of Swing coding, I have chosen not to use NetBeans code-generation as I feel that it will only get in the way of my efforts to learn Swing. YMMV.
Walter Sheets
Greenhorn

Joined: Jan 23, 2009
Posts: 8
This came to me in my sleep just now (sitting in front of my computer :-)

The right way to do this in the NetBeans IDE is to subclass an existing class
like JPanel, make it do what you want in its own paint method -- then add
your new subclass to the "Palette Manager" right along side the JPanel form
so you can drag it into a JFrame just like you would a JPanel.

Brilliant! Why did it take me so long to figure out such a simple thing? :-/
Because I'd forgotten that you *can* add your own objects to the Palette
Manager, probably.

Now all I have to do is go do it. I sure hope it's easy....

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Walter Sheets wrote:This came to me in my sleep just now (sitting in front of my computer :-)

The right way to do this in the NetBeans IDE is to subclass an existing class
like JPanel, make it do what you want in its own paint method -- then add
your new subclass to the "Palette Manager" right along side the JPanel form
so you can drag it into a JFrame just like you would a JPanel.

Brilliant! Why did it take me so long to figure out such a simple thing? :-/
Because I'd forgotten that you *can* add your own objects to the Palette
Manager, probably.

Now all I have to do is go do it. I sure hope it's easy....


So how many non-reusable palette components are you going to end up with? I would add only resuable widgets to the palette.

It's not so difficult in NetBeans to create an anonymous JPanel subclass and override paintComponent. You use "Custom creation code" for that. Thie would go in the custom creation code dialog:
(Either use fully qaulified class names, or go the the "code" tab, right-click and select "Fix Imports")

And I'm shocked that after so much has been said about the correct method to override you're still talking about overriding paint. Please go through The Java™ Tutorials : Performing Custom Painting.


luck, db
There are no new questions, but there may be new answers.
Walter Sheets
Greenhorn

Joined: Jan 23, 2009
Posts: 8
Thanks for the suggestion. Sorry to be dense, but I keep getting stuck while
trying to do what you suggest.

If I use only a top-level JFrame, the Customize Code menu item is grayed out.

If I drag in an ordinary JPanel then the IDE insists on giving it a name, of course,
like jPanel1, and won't let me delete it.

To what class, then, were you adding the custom creation code for the new
anonymous jPanel subclass?

BTW, I misspoke when I mention overriding the paint method. I actually
used paintComponent even before I read your post.

Thanks, I really appreciate the help.
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Another thing..

if you dont want strange results when painting a Graphics2D
Dont use:


Use its instead


And at the end of paintining use

So..



Will be


Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Walter Sheets
Greenhorn

Joined: Jan 23, 2009
Posts: 8
Darryl Burke wrote:
Walter Sheets wrote:
The right way to do this in the NetBeans IDE is to subclass an existing class
like JPanel, make it do what you want in its own paint method -- then add
your new subclass to the "Palette Manager" right along side the JPanel form
so you can drag it into a JFrame just like you would a JPanel.

So how many non-reusable palette components are you going to end up with? I would add only resuable widgets to the palette...


Okay, Darryl was right, no surprise there. I read elsewhere the right way in NetBeans is to subclass the jPanel with your own paintComponent() code -- and then click and drag your subclass directly from the Project Explorer pane onto your toplevel jFrame form. No need to add it to the Palette.

And *that's* the trick that makes it trivial in NetBeans to do what I needed. I knew there had to be a trivial way to do something so common.
 
 
subject: Graphics2D window not being repaired?