Animating the Sprite

infamoustrey

Junior Member
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
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 ;
		        }
			
			
		}
	
	}
	
}
Paladin Class
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());}
		
	}
	
	
	

}
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Your question is really vague. Can you explain in more detail about the overall purpose of this program, what you have so far, and specifically where you are stuck?
 

cytg111

Lifer
Mar 17, 2008
25,719
15,200
136
Tried to do smooth animation with java once .. couldnt pull it off, garbage collection will kick in at random times and if not freeze then disrupt the 'smoothness' factor.
If it can be done, idk.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Tried to do smooth animation with java once .. couldnt pull it off, garbage collection will kick in at random times and if not freeze then disrupt the 'smoothness' factor.
If it can be done, idk.

Obviously it can be done. There are tons of Java games on Android.

You can adjust how the garbage collector behaves. Maybe that's how you deal with it?
 

cytg111

Lifer
Mar 17, 2008
25,719
15,200
136
Possibly .. I tried that too, but tuning an applet that runs on who-knows-what is pointless, you have no control over the jvm.
You could try to control it with the .gc(), but that is not guranteed to work, though I know there is real-time JVM initiatives outthere (effectively doing gc very very often), that might be an approach.
Dalvik is another story, and not the kind of "jvm"'s we usually deal with.