Author |
Message |
poll262
|
Posted: Tue Jun 04, 2013 10:03 am Post subject: Animations in Java |
|
|
I am currently working on my final task and am making a game. I am trying to create an object to be used to animate a character but am having difficulty with drawing the image. Here is the code I have so far.
Java: |
import java.awt.Image;
public class Animation {
Image images [];
long delay;
public Animation (Image images [], int length ) {
this. images = images;
delay = images. length / length;
}
public void playAnimation () {
for (int i= 0; i < images. length; i++ ) {
try {
//draw images[i]
Thread. sleep(delay );
} catch (InterruptedException e ) {
e. printStackTrace();
}
}
}
}
|
I need to somehow tell my main program to draw the image from the array but I can't figure it out. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
poll262
|
Posted: Tue Jun 04, 2013 7:34 pm Post subject: Re: Animations in Java |
|
|
Anybody? |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Jun 04, 2013 9:02 pm Post subject: RE:Animations in Java |
|
|
Is this for an applet (embedded on a web page) or as a standard application window? |
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Tue Jun 04, 2013 9:05 pm Post subject: RE:Animations in Java |
|
|
It will be in an application window, not an applet. |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
|
|
|
![](images/spacer.gif) |
poll262
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Tue Jun 04, 2013 10:14 pm Post subject: RE:Animations in Java |
|
|
I understand that already. The problem that I am having is getting my animation object to draw the image to the panel which would be in a different class. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Jun 04, 2013 10:24 pm Post subject: RE:Animations in Java |
|
|
Either pass all variables from the Game class into the Animation class (like the images) in the constructor/setters. OR pass the Game class instance into the Animation object. Then have it call the images.
Eg:
Java: |
class Game {
GameCanvas canvas = new GameCanvas (this);
}
class GameCanvas extends JPanel {
Game game;
public GameCanvas (Game game ) {
this. game = game;
}
public void paintComponent (Graphics g ) {
game. draw(g );
// or g.drawImage(... game.image ...);
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
poll262
|
Posted: Wed Jun 05, 2013 5:48 am Post subject: RE:Animations in Java |
|
|
I don't understand what passing the game class instance does. Could you explain?
edit: I would also like it so that the main class is drawing the images. |
|
|
|
|
![](images/spacer.gif) |
|