Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411273 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 01:03:56 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsAnimation in Java
Pages: [1]
Print
Author Topic: Animation in Java  (Read 1123 times)
Sgt. Pepper
Level 1
*


View Profile
« on: June 09, 2015, 07:33:15 AM »

Hello

I have been fooling around with animation in Java, and since I think I've got it working, I thought it'd be nice to share my way of doing this with the community Smiley This tutorials assumes you already have some knowledge in the Java programming language. Not anything fancy, just being able to create Objects and such. So, first off, you need to create a JFrame, which is more or less the window we will be drawing on. So, open up Notepad or the such and type this:

Code:
import javax.swing.JFrame;

public class Animation {

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
                //frame.add(new Render()); Ignore this line for now
frame.setTitle("Animation");
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}

Without going into too many details, this creates a frame, gives it a size, gives it a title, and sets it position. Right now though, the JFrame is just a blank canvas. We need to draw something on it. In a separate Notepad/Eclipse/TextEdit/Netbeans/whatever window, type this:

Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class RenderAnimation extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;

Image[] images = { new ImageIcon("res/1.png").getImage(),
new ImageIcon("res/2.png").getImage(),
new ImageIcon("res/3.png").getImage(),
new ImageIcon("res/4.png").getImage(),
new ImageIcon("res/5.png").getImage(),
new ImageIcon("res/6.png").getImage(),
new ImageIcon("res/7.png").getImage(),
new ImageIcon("res/8.png").getImage(),
new ImageIcon("res/9.png").getImage(),
new ImageIcon("res/10.png").getImage(),
new ImageIcon("res/11.png").getImage(),
new ImageIcon("res/12.png").getImage(),
new ImageIcon("res/13.png").getImage(),
new ImageIcon("res/14.png").getImage(),
new ImageIcon("res/15.png").getImage(),
new ImageIcon("res/16.png").getImage(),
new ImageIcon("res/17.png").getImage(),
new ImageIcon("res/18.png").getImage(),
new ImageIcon("res/19.png").getImage(),
new ImageIcon("res/20.png").getImage(),
new ImageIcon("res/21.png").getImage(),
new ImageIcon("res/22.png").getImage(),
new ImageIcon("res/23.png").getImage(),
new ImageIcon("res/24.png").getImage(),
new ImageIcon("res/25.png").getImage(),
new ImageIcon("res/26.png").getImage(),
new ImageIcon("res/27.png").getImage(),
new ImageIcon("res/28.png").getImage(),
new ImageIcon("res/29.png").getImage(),
new ImageIcon("res/30.png").getImage(),
new ImageIcon("res/31.png").getImage(),
new ImageIcon("res/32.png").getImage(),
new ImageIcon("res/33.png").getImage(),
new ImageIcon("res/34.png").getImage(),
new ImageIcon("res/35.png").getImage() };

int frame = 0;
private Timer t;

public RenderAnimation() {
t = new Timer(25, this);
t.start();
}

public void actionPerformed(ActionEvent e) {
repaint();

frame += 1;
if (frame >= images.length) {
frame = 0;
}
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}

public void paint(Graphics g) {
super.paint(g);
g.drawImage(images[frame], 200, 100, 400, 400, null);
}
}
This is pretty complicated, but all it really does is reads the 35 (or less) images that make up the animation from the hard drive, creates a Timer that loops a certain action over and over, and draws the image corresponding to the frame number, which gets incremented every "tick" of the timer. The Thread.sleep is just so everything doesn't go whizzing by. Also to prevent it from whizzing by, if the current frame number is greater than the array size, the frame variable automatically goes back to 0. In other words, it loops infinitely. Finally, and this step is nessicary, uncomment the frame.add(new RenderAnimation()); line or this will not work. The output of the code seen here is this:



That was probably too big of an information dump, but anyway, that's one way of doing Animation in Java. Hope you've enjoyed this tutorial, and if it's absolutely mind-boggling confusing, I'll clarify.

-Sgt. Pepper
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic