This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginner painting troubles

 
Noah Jones
Greenhorn
Posts: 3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic