- Apr 15, 2012
- 20
- 0
- 66
So I have two classes here for a game I'm making and I would like to know how I can get the rows and cols to change, wait and then change again to give the animated affect.
Frame Class
Paladin Class
Frame Class
Code:
package frame;
//Imports
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import javax.swing.JFrame;
import sprites.Paladin;
//frame
public class Frame extends JFrame implements Runnable{
//Initializing variables
public static int width = 600;
public static int height = 400;
public static String title = "Game";
//changes the position
public int xDirection;
public int yDirection;
//some more for dbing
private Image dbImage;
private Graphics dbg;
int x = 0;
int y = 0;
static //objects
Paladin guy = new Paladin();
//Start the frame and set properties
public Frame() {
setVisible(true);
setResizable(false);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(title);
setLocationRelativeTo(null);
//adds key adapter
addKeyListener(new AL());
}
//MAIN CLASS
public static void main(String[] args) {
Frame frame = new Frame();
//CREATE THREAD
Thread run = new Thread(frame);
Thread paladin = new Thread(guy);
// INTIALIZE IT
run.start();
paladin.start();
}
// PAINT GRAPHICS
//DOBLE BUFFER FOR SMOOTH IMAGES
@Override
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.drawImage(guy.sprites[guy.row][guy.col], x, y, null);
repaint();
}
// MOVING METHOD
public void move() {
x = x + xDirection;
y= y + yDirection;
}
//THREAD BEGINS
public void run(){
try{
while(true){
move();
Thread.sleep(7);
}
}catch(Exception e){System.err.println(e.getMessage());}
}
// KEY ADAPTER
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
xDirection = -1;
guy.walkDirection = 0 ;
}
if(keyCode == e.VK_RIGHT){
xDirection = 1;
guy.walkDirection = 1 ;
}
if(keyCode == e.VK_UP){
yDirection = -1;
guy.walkDirection = 2 ;
}
if(keyCode == e.VK_DOWN){
yDirection = 1;
guy.walkDirection = 3 ;
}
}
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
xDirection = 0;
guy.walkDirection = 4 ;
}
if(keyCode == e.VK_RIGHT){
xDirection = 0;
guy.walkDirection = 4 ;
}
if(keyCode == e.VK_UP){
yDirection = 0;
guy.walkDirection = 4 ;
}
if(keyCode == e.VK_DOWN){
yDirection = 0;
guy.walkDirection = 4 ;
}
}
}
}
Code:
package sprites;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import frame.Frame;
public class Paladin implements Runnable{
Frame dude;
//animating [row] [col]
public int row = 4;
public int col = 6;
public int sleepTime = 900;
public int walkDirection = 4;
BufferedImage AA = null; {
try {
AA = ImageIO.read(new File("C:/Users/B Dillon Photography/Desktop/Paladin/walking/pw.png"));
} catch (IOException e) {
}
}
final int width = 96;
final int height = 95;
final int rows = 7;
final int cols = 9;
public BufferedImage[][] sprites = new BufferedImage[rows][cols]; {
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[i][j] = AA.getSubimage(i * width, j * height, width, height);
}
}
}
@Override
public void run() {
try{
while(walkDirection == 1) {
col = 2;
if(row > 7) {
row = 0;
}
Thread.sleep(sleepTime);
row++;
}
while(walkDirection == 4) {
col = 4;
row = 6;
}
}catch(Exception e){System.err.println(e.getMessage());}
}
}