aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Applet refuses to repaint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Applet refuses to repaint" Watch "Applet refuses to repaint" New topic
Author

Applet refuses to repaint

ken swartwout
Greenhorn

Joined: Feb 23, 2011
Posts: 2
I have tried wrting this program using JFrames and plain old applets. I get the same results every time - it wont repaint when I call it. I put some debugging info in it and I can tell that it is doing what it is supposed to other than repaint(). It is supposed to loop through animations in response to arrow key input and repaint every time. However, repaint only gets called once at the end of the "cylce" of images stored in the array. The code is stripped down to barebones - can someone please proveide some insight? I have been slamming my head against a wall for hours on this one. I am sure there is some extra stuff in there as I stripped it down for clarity. I have tried a lot of things and they all yield the exact same result. Thank you.



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Welcome to the Ranch.
In future, while posting code, please UseCodeTags Since you are new, I have modified your post to include them. As you can it makes the code easier to read and understand.

For custom painting, it is recommended you override the paintComponent method. This article offers a good description with sample code http://download.oracle.com/javase/tutorial/uiswing/painting/


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Never override the paint() method of a top level container like a JFrame or JDialog.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

And even if you do, call super.paint(g) as the first statement of the overridden method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ken swartwout
Greenhorn

Joined: Feb 23, 2011
Posts: 2
Thanks for the quick responses. Unfortunately, I didnt really understand what to do with them. I read through the tutorial and I tried to add a super.updateComponent call in the right spot... It doesnt even compile (something about not recognizing what updateComponent is) - but that is what happens when I dont know why I am doing something. I dont know much about Swing in the first place. I first wrote a plain old applet for this but I converted it to see if I could get different behavior. If you compile and run my code it does paint fine when it actually invokes the paint method. On line 51, the repaint() is ignored repeatedly. It executed the loop but ignores the call to repaint. I have read that repaint() calls can be ignored but I know nothing about it and this seems to be what is happening. Is overriding paint just bad style or would it cause any of my problems. The applet does paint - it just ignores the repaints that are in while loops - otherwise known as the animation of the program. Thanks again.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I tried to add a super.updateComponent call in the right spot


No where has anybody suggested that you add "super.updateComponent()" in your code. Read the suggestions and don't make up code.

Is overriding paint just bad style or would it cause any of my problems.


It is bad style and will potentially cause problems if done incorrectly. You have implemented it incorrectly and are having problems.

You where given a link to the Swing tutorial that contains working example. If you can't understand the working examples I'm not sure how you expect us to help because we can't write the code for you. If you don't understand something from the tutorial then you need to ask a specific question.

The tutorial also has a section on "How to Use Swing Timers" which can help you out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet refuses to repaint