File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Beginner painting troubles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Beginner painting troubles" Watch "Beginner painting troubles" New topic

Beginner painting troubles

Noah Jones

Joined: Nov 23, 2012
Posts: 3

I am in the process of making a old school 8-bit Zelda-ish game. I have been able to get still frame tiles to paint fine, but my trouble comes when trying to paint sprites with animations. I have an animation class that I know works (I have used it in other projects) and also a sprite class with simple get and set methods. I cannot get the animation to update at all. Here is my code:

And the code for the main class:

What I get when I run this is two water tiles that are in the correct position but un-animated. Paint is only called twice and update is never called(according to my println statements). What am I doing wrong? I have considered doing a separate game loop calling update but I do not know how to implement that without hindering the normal painting of the component. I know I have hooked up all the pieces to my animation and sprite classes fine so don't worry about that.

Thanks in advance!
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
For a start your Runner class should extend JComponent and you should be overriding paintComponent() and not Paint().
I'm not clear on why you have overridden update() but if you want to do some animation you need something like a timer to trigger a redraw every n seconds/milliseconds, I suggest you look at javax.swing.Timer as the action event handlers run on the Event Dispatch Thread which is required when calling most Swing classes methods.
I agree. Here's the link:
subject: Beginner painting troubles
It's not a secret anymore!