File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Basic swing application; class extending JPanel won't update graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Basic swing application; class extending JPanel won Watch "Basic swing application; class extending JPanel won New topic
Author

Basic swing application; class extending JPanel won't update graphics

C. Lind
Greenhorn

Joined: Mar 02, 2010
Posts: 4
Hi, I've been spying on you guys for a while as I was taking a very basic programming course last semester and found lots of help here. However I can't seem to find anything to help me with this, and it really bugs me as it is such a simple program and I just can't seem to make it work properly.

The program is meant to take input in the form of RGB-values and draw a circle in that colour by using a class extending JPanel. I define an object of this class as a class variable in my frame, initiate it in the constructor and also in actionPerformed to run the custom JPanel constructor with the new colour when I click the button. Now, while I do get the circle drawn once with the starting values it doesn't seem to update when I click the button. For testing purposes I added a JOptionPane in the paintComponent() method of my custom JPanel class and it seems to run hundreds of times per second. I also tried surrounding the lines inside actionPerformed() with if (ae.getSource().equals(paintButton) ) { ... } though I can't see any need for it, but still no difference. I've tried basically everything; I've been reading through the examples in my book tens of times and probably stared myself blind to any errors over the last two days. This is my most desperate hour. Help me, Java Ranch. You're my only hope.

So here goes code:



Second class extends:


Apologize for the long post. I'll be quiet and go hide in a corner now.


And that's all I have to say about that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

Your CirclePanel class doesn't provide any way to change the variables named "red", "green", and "blue" which are what it uses when it draws the circle.

Since you never change those variables, the colour of the circle it draws never changes either.

Perhaps methods named "setRed" and so on would help?

Edit: you also seem to be creating more CirclePanel objects and adding them to the display. Perhaps if you resized the window you would be able to see all of them... but do you really want to have more than one?
C. Lind
Greenhorn

Joined: Mar 02, 2010
Posts: 4
I can see your point, but I'm getting a bit confused as the example in the book I try to follow seems to be doing it this way. To clear things out:

I'm not creating more than one CirclePanel object (at least I think so) but rather reconstructing the same one. "CirclePanel c" is defined at class level and initiated once in the ColoredCircle constructor and once in the actionPerformed method. This, I believe, would recreate CirclePanel c with the new colour. As I'm using BorderLayout, using getContentPane().add(c, "South") a second time would also replace the old panel rather than adding another one. Please correct me if I'm wrong.

I suppose I'm not supposed to do things a certain way just cause I'm supposed to do them this way according to the book, but I just know that even if I could make it work with set-method(s) I won't be able to let this go. It should work and yet it doesn't.

Also, resizing the window doesn't reveal any additional, hidden panels.

The only real differences I can find between the example and my code is that they're using JSlider instead of JTextField and defining their own listener as an inner class, but I can't see how it would make any difference. The book is also keeping the main method in a separate class.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

C. Lind wrote:I'm not creating more than one CirclePanel object (at least I think so) but rather reconstructing the same one. "CirclePanel c" is defined at class level and initiated once in the ColoredCircle constructor and once in the actionPerformed method. This, I believe, would recreate CirclePanel c with the new colour. As I'm using BorderLayout, using getContentPane().add(c, "South") a second time would also replace the old panel rather than adding another one. Please correct me if I'm wrong.


No, every time you execute "new CirclePanel" you create a new CirclePanel object. The word "recreate" isn't helpful there; what you're doing is creating a new object and changing that variable to refer to the new object instead of the old one.

As for the question about BorderLayout, I don't know. That's a Swing question. Usually if a method is named "add" it adds something rather than replacing something, but perhaps there's something different about this "add" method. What does the API documentation say about that?

As for your book, there are several things in that program that are kind of out of date. The idea of having a single JFrame which also implements listeners isn't really good practice now, and changing the GUI by adding new components rather than by modifying existing components is generally a bad idea. But I don't want to write a better book for you so I will just leave it at that now.
C. Lind
Greenhorn

Joined: Mar 02, 2010
Posts: 4
Well, that makes sense. As far as I can make out from the API BorderLayout creates five regions (north, south, west, east, center) and you can't really add any regions to it, though it's possible it works a bit in the way you pointed out with my object "reconstruction". But then again, I'm the greenhorn hehe.

As for the part about not implementing listeners in the JFrame, would that mean the proper way is to create an inner class for listening? And what about anonumous inner classes? e.g.



Sorry if I'm going a bit off topic with this one, I'll try to keep myself from coming up with any more follow-up questions.
Anyway, I'll probably drop this approach then as I'd rather learn good practice than bad. But that will be left for tomorrow, bed time is long due.

Thanks a lot for your help. I've almost learned more from these posts than I did in class, sad as it might be.
(it still bugs me though)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
C. Lind wrote:Well, that makes sense. As far as I can make out from the API BorderLayout creates five regions (north, south, west, east, center) and you can't really add any regions to it, though it's possible it works a bit in the way you pointed out with my object "reconstruction". But then again, I'm the greenhorn hehe.
if you add something to a BorderLayout region, it will cover over what was displayed previously, but I agree with the other poster, I don't think you want to do this, but rather give your CirclePanel a setColor(int, int, int) method.

As for the part about not implementing listeners in the JFrame, would that mean the proper way is to create an inner class for listening? And what about anonumous inner classes? e.g.


Yes, anonymous inner classes could work fine here, such as an anonymous ActionListener-implementing class. I agree that this is cleaner than having your while GUI implement ActionListener. Also, it is not necessary and in fact frowned upon to extend JFrame, since you program really doesn't extend a JFrame but rather uses one. You do need to extend JPanel in your CirclePanel class since you override there JPanel's paintComponent method.

Other suggestion: make your variable names more meaningful. A JPanel called "p" is somewhat confusing.
C. Lind
Greenhorn

Joined: Mar 02, 2010
Posts: 4
Thanks for the input, I'll rewrite it and feel much more confident that I can make it work now.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

This would be more suited on the UI forums. Moving.

C.Lind,
Please read this.
CarefullyChooseOneForum


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic swing application; class extending JPanel won't update graphics