• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel not repainting properly.

 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JPanel with and Image img, and a Rectangle2D rec.
Simply, I have methods to change the image, and to slowly change the rectangle size (in a while loop which increments the co-ordinates).
The rectangle does everything it should, but the image does not always change (unless I resize the JFrame which contains it).
Can anyone help? I have spent three days at it and I'm all out of ideas.
By the way, in my main app, if I use solely either the rectangle or the image, everything is fine, it' when they are used together I have problems.
Thanks for any help,
merlin_bar
[ April 15, 2003: Message edited by: merlin bar ]
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try calling validate() or update() after your modifications.
Ive been programming in C for a while now, so my memory is fuzzy, but I think that these functions are available in your Frame, which calls the update function for all the elements in your gui, and they then redraw themselves.
hope that helped..
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I've tried validate(), but it doesn't help.
update(Graphics) takes a Graphics argument so I don't see how I can call it.
However, I have just realized that the problem also exists for just one JPanel, so I think I'd better explain a little further (It was late last night
I have a class called Tank which extends JPanel. On a repaint() this class displays an Image of a tank (.png) and performs a resize on a Rectangle2D.
I have methods to resize the rectangle and methods to change the image.
The changeImage() is a simple method.
The changeRectangle() has a while loop which may loop from 0 to 70 times, calling repaint() on each iteration.
In my main app, I call these methods but I'm not getting the visual results unless I resize the JFrame which contains the Tank object.
I hope this is a little clearer. Thanks again to anyone who's been patient enough to read all this!
Regards,
merlin_bar
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the rectangle resizing the image or the panel? Or does it have nothing to do with either of these?

If everything seems to magically fix itself after a resize, then it is somehow involved with layout, which invalidate() and/or validate() should fix. Be sure you are calling these methods on the parent component of this panel, though.
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rectangle is a Rectangle2d.double and it is drawn over the image. So The Tank class extends JPanel, and has a Rectangle and an Image.
When you say the parent component, do you mean the Tank class, or the JFrame which contains it?
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unless you are concerned with performance, just call it on the highest level container you have. doesnt sound like youre doing anything too intensive, so just call it on the JFrame.
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already tried that but it doesn't work. The image doesn't get updated unless I resize the JFrame. However if I call one of the methods that modify the rectangle, then the image change is visible.
The program is over 400 lines long so I'll just show the important bits. Maybe you can see some sort of rule I am breaking.

[ April 16, 2003: Message edited by: merlin bar ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the pause() method do? Well, other than the obvious... How does the pause() method make the GUI pause?
[ April 16, 2003: Message edited by: Nathan Pruett ]
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes. It's just a Thread.sleep(int millis). But all the try and catch take up a lot of space so I put it in a method. You'd be surprised what a big difference a 1 millisecond delay made to the smoothness of the animation!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you also post the actual paint method where the image and rectangle are being painted? And does your class do anything to set its own size in any way?
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand your question, but if you mean the constructor, here it is...

Thank you again. I can't tell you how much I appreciate your' time.
Kind regards,
merlin
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrgh! I still don't see a specific problem... If you want to email me the whole code I will look it over (mail to nate@javaranch.com, and put "Code from JavaRanch" in the header...). Following are a few more suggestions...

Make sure that each of your methods: fill(), empty(), closeTopValve(), openTopValve(), closeBottomValve(), openBottomValve(), etc. all have a repaint() before they return, so matter what the state is... Maybe this is a logic problem where the gui thinks the tank is empty and doesn't need to be repainted, but the gui still shows the last state before the tank was empty?

Another thing to look out for is that the repaint() command doesn't really force the panel to repaint right away... it just kind of says that the panel should be repainted at the next earliest opportunity. Multiple repaint requests right after another may be merged into a single actual repaint. I don't think this is a problem, because you noticed how putting a 1 ms pause made the animation smoother... without the pause all the repaint requests were merged into 1 and the animation just jumped to the last state.
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate,
I have sent you the full monty! As I said in my mail, I doubt it's a logic problem, as the update does occur if the JFram is resized, or another window moves across it.
Thanks again,
merlin_bar
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got your files and am looking at them now... hopefully I will have a solution for you soon! ( Sorry I didn't look at them over the weekend... I had a really busy one! )
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK... this is weird...

If I run the program from the classes you sent me, I get the same problem you have with the last frame not painting...

However, if I recompile the source you sent me and run it, the problem disappears!

What version of the JDK are you using? I'm using 1.4.1_01...
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is strange. I came up with a fix for the problem. The rectangle (blue liquid) that resizes is very responsive. Also, it obviously has to repaint whatever is behind it, but my vavles aren't getting repainted!
This means that only the parts that the VM thinks are necessary are getting repainted!
So, I just made a big rectangle the size of the entire JPanel, and I flash it for 1 millisecond everytime the image is changed. This forces a full repaint! - Problem solved.
I can't thank you enough for your' help. It's good to have another brain involved. Even writing the problem down gives ideas!
Thanks again Nate, I hope I get the chance to return the favour in the future.
Merlin_bar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!