This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Applet refuses to repaint

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never override the paint() method of a top level container like a JFrame or JDialog.
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even if you do, call super.paint(g) as the first statement of the overridden method.
 
ken swartwout
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic