Computer Science Canada Animations in Java |
Author: | poll262 [ 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.
I need to somehow tell my main program to draw the image from the array but I can't figure it out. |
Author: | poll262 [ Tue Jun 04, 2013 7:34 pm ] |
Post subject: | Re: Animations in Java |
Anybody? |
Author: | Zren [ 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? |
Author: | poll262 [ Tue Jun 04, 2013 9:05 pm ] |
Post subject: | RE:Animations in Java |
It will be in an application window, not an applet. |
Author: | Zren [ 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. |
Author: | poll262 [ 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 |
Author: | Zren [ 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). |
Author: | poll262 [ 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. |
Author: | Zren [ 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:
|
Author: | poll262 [ 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. |