Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Animations in Java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
poll262




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
poll262




PostPosted: Tue Jun 04, 2013 7:34 pm   Post subject: Re: Animations in Java

Anybody?
Zren




PostPosted: 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?
poll262




PostPosted: Tue Jun 04, 2013 9:05 pm   Post subject: RE:Animations in Java

It will be in an application window, not an applet.
Zren




PostPosted: Tue Jun 04, 2013 9:10 pm   Post subject: 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




PostPosted: Tue Jun 04, 2013 9:15 pm   Post subject: 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




PostPosted: Tue Jun 04, 2013 9:56 pm   Post subject: 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




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: 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 ...);
    }
}
poll262




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: