• Post Reply Bookmark Topic Watch Topic
  • New Topic

having a small issue with drawing images that are gifs.  RSS feed

 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really sure how to phrase the question. I created a small applet in java.. it allows me to move a toon around a blank applet window. I am using four gif files that have a walking animation in them. The issue i am having is after moving around the gif just flashes in and out really quickly.. when i start the program, moving up and down seems to be working perfectly fine, but the moment i move left or right it starts messing up for all directions. here is my code.


Can anyone tell me why this is happening? have I forgotten some small element?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two potential issues (although I'm not certain that those are causing what you describe).

Firstly, in Swing you should override paintComponent rather than paint (which is what you'd use in AWT).

Secondly, I would download all images and store them locally (in the init method) rather than re-downloading them each time they're needed.
 
Carey Brown
Saloon Keeper
Posts: 2849
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 18 you clear x-speed and y-speed but you are not taking into account the actual last position of the gif. Need something like lastX and lastY.
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code looks ok apart from the issues raised by Ulf and Carey. Fixing those points should fix the problem. In case you aren't sure what Carey meant, you are clearing a rectangle based on the speed values rather than the x and y position of the image before it was moved. You actually don't need that line, if you implement the paintComponent() method rather than paint() and call super.paintComponent() as the first line it will clear the whole panel area for you.

Another issue you have is when you stop pressing a key your image should go back to the standing image - Hint: you could use the keyReleased() method to do this.
 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer, i took you advance and did as you said with the gifs, now they are loaded and just called.. I'm not sure I know what a paintComponent is or does.. i'll look it up..

Carey Brown, Not sure what you mean. the last position would be x, y - speed (speed being how many pixels the object moved) for example.. object starts at 0, 0 if I move it down one, the coordinates would be 10, 0. so my clearRect would be x (which is 10) - speed (which is 10) so 0,0 is clear and my object is at 10, 0. Correct me if I'm wrong..

Tony Docherty, i took your advice and used the depress to make the object stand.. still not sure what or how to use the super.paintCompnent(), but i'll look it up.. if you could give me an example that would be really helpful..

Thanks everyone for taking the time to respond.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be as simple as renaming "paint" to "paintComponent", and calling its super method. You may want to read the "Custom Painting" section of the Swing Tutorial.
 
Carey Brown
Saloon Keeper
Posts: 2849
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Richardson wrote:Carey Brown, Not sure what you mean. the last position would be x, y - speed (speed being how many pixels the object moved) for example.. object starts at 0, 0 if I move it down one, the coordinates would be 10, 0. so my clearRect would be x (which is 10) - speed (which is 10) so 0,0 is clear and my object is at 10, 0. Correct me if I'm wrong..
.

Yes, speed tells you how far it moved but it doesn't tell you which direction it moved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!