
-----------------------------------
poll262
Tue Jun 04, 2013 10:03 am

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.


import java.awt.Image;

public class Animation {
	Image images 

I need to somehow tell my main program to draw the image from the array but I can't figure it out.

-----------------------------------
poll262
Tue Jun 04, 2013 7:34 pm

Re: Animations in Java
-----------------------------------
Anybody?

-----------------------------------
Zren
Tue Jun 04, 2013 9:02 pm

RE:Animations in Java
-----------------------------------
Is this for an applet (embedded on a web page) or as a standard application window?

-----------------------------------
poll262
Tue Jun 04, 2013 9:05 pm

RE:Animations in Java
-----------------------------------
It will be in an application window, not an applet.

-----------------------------------
Zren
Tue Jun 04, 2013 9:10 pm

RE:Animations in Java
-----------------------------------
http://docs.oracle.com/javase/tutorial/uiswing/index.html
http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel

I'd suggest the second option on the stackoverflow link (http://stackoverflow.com/a/2706730).

To update the image to the next keyframe of the animation, just set the icon on the JLabel.

Edit: You need to create the window (JFrame) and add a JPanel into the JFrame. Then add a JLabel with an icon into the panel. The JPanel might be an unnecessary step. I forget.

-----------------------------------
poll262
Tue Jun 04, 2013 9:15 pm

Re: Animations in Java
-----------------------------------
But I want more control over the location of the animation and would like to use java's 2d graphics classes. I want to draw images like this:

 http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

-----------------------------------
Zren
Tue Jun 04, 2013 9:56 pm

RE:Animations in Java
-----------------------------------
Ah. Well then make a new class that subclasses JPanel. Then overload it's paintComponent method.

http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)

Make sure to call super.paintComponent(g).

-----------------------------------
poll262
Tue Jun 04, 2013 10:14 pm

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.

-----------------------------------
Zren
Tue Jun 04, 2013 10:24 pm

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:

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 ...);
    }
}


-----------------------------------
poll262
Wed Jun 05, 2013 5:48 am

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.
