Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411498 Posts in 69373 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 08:21:57 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsJava2D Animation Tutorial
Pages: [1]
Print
Author Topic: Java2D Animation Tutorial  (Read 2660 times)
DarkCart
Level 0
**



View Profile WWW
« on: June 15, 2014, 01:22:18 PM »

Hey TIGForums,

Today I will give you a brief tutorial on how to do animation in Java2D. First of all, you'll need a class file and the main() method.

Code:
package Draw;

import javax.swing.JFrame;

public class Draw {
public static void main(String[] args) {

}
}

Next, you'll need to put a JFrame inside the main method. Think of the JFrame as a blank canvas. (Not to be confused with the 'java.awt.Canvas' class, which I will not be using)

Code:
package Draw;

import javax.swing.JFrame;

public class Draw {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle("Draw");
//frame.add(new GFX());
}
}

This puts a frame, 500x600 pixels, on your screen called "Draw" that cannot be resized.
Next, you need to add a class that will actually draw the graphics. You notice that I have that last 'frame.' line commented out, because that constructor doesn't exist. You can un-comment this line once you add this following code:
Code:
package Draw;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GFX extends JPanel {
private static final long serialVersionUID = 1L;
int x, y;

public GFX() {
x = 1;
y = 1;
}

public void paint(Graphics g) {

g.setColor(Color.green);
for (int i = 0; i < 124; i++) {
g.fillRect(x, y, 32, 32);
x++;
y++;
                        repaint();
                try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

Then, uncomment the line that refers to the GFX class, and you should see a green square going in a diagonal pattern down the frame.

That's all for this tutorial, if you have any questions post them below or PM me.

Cheers!  Beer!

-DarkCart
Logged

Try out EasyJava
Impmaster
Level 10
*****


Scary, isn't it?


View Profile WWW
« Reply #1 on: June 15, 2014, 07:17:50 PM »

What do you think of Java2d for making games? When I was trying to make a game in java, generally people told me that it was too slow and that I should use like Slick2d or something. Is it working out fine for you?
Logged

Do I need a signature? Wait, now that I have a Twitter I do: https://twitter.com/theimpmaster
DarkCart
Level 0
**



View Profile WWW
« Reply #2 on: June 16, 2014, 03:59:31 AM »

What do you think of Java2d for making games? When I was trying to make a game in java, generally people told me that it was too slow and that I should use like Slick2d or something. Is it working out fine for you?

It works for small games, but if you try hard enough, you can make something like this:

Logged

Try out EasyJava
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic