That worked. I was getting the picture at 32 * 4 when that was the bottom corner. This is my current animation code

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Animation implements Runnable{

	int animationNumber = 1;
	Image character;
	boolean up,down,left,right;
	Painting painting;
	
	public void startAnimation (boolean u, boolean d, boolean l, boolean r){
		up = u;
		down = d;
		left = l;
		right = r;
		System.out.println ("In animation");
		this.run();
	}
	
	public Image getCharacter (){
		return character;
	}
	
	public void run (){
		//32 x 32 images
		System.out.println ("Getting image");
		if (up){
			System.out.println ("Up pressed");
			BufferedImage img;
			try {
				System.out.println ("Getting image");
				img = ImageIO.read(new File ("Images/image.png"));
				System.out.println ((32 * animationNumber) + ", " + (32 * 3) + ", " + 32 + ", " + 32);
				character = img.getSubimage ((32 * (animationNumber-1)), (32 * 3), 32, 32);
				
				animationNumber ++;
				try {
					System.out.println ("Waiting 0.5 seconds");
					Thread.sleep(50);
					System.out.println ("Done waiting");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println ("Repainting");
			painting.redraw(character);
			System.out.println ("Repainted");
			if (animationNumber == 4){
				animationNumber = 1;
			}
			
		}
		if (down){
			BufferedImage img;
			try {
				img = ImageIO.read(new File ("Images/image.png"));
				character = img.getSubimage ((32 * (animationNumber-1)), (32 * 0), 32, 32);
				animationNumber ++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			painting.redraw(character);
			if (animationNumber == 4){
				animationNumber = 1;
			}
			
		}
		if (left){
			BufferedImage img;
			try {
				img = ImageIO.read(new File ("Images/image.png"));
				character = img.getSubimage ((32 * (animationNumber-1)), (32 * 1), 32, 32);
				animationNumber ++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			painting.redraw(character);
			if (animationNumber == 4){
				animationNumber = 1;
			}
			
		}
		if (right){
			BufferedImage img;
			try {
				img = ImageIO.read(new File ("Images/image.png"));
				character = img.getSubimage ((32 * (animationNumber-1)), (32 * 2), 32, 32);
				animationNumber ++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			painting.redraw(character);
			if (animationNumber == 4){
				animationNumber = 1;
			}
			
		}
		
		
		
	}

	public void initAnimation(Painting p) {
		// TODO Auto-generated method stub
		painting = p;
		BufferedImage bi;
		try {
			bi = ImageIO.read(new File ("Images/image.png"));
			character = bi.getSubimage(0, 0, 32, 32);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
}

and here is my painting code

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;



public class Painting extends JPanel implements KeyListener{
	
	Animation animate = new Animation ();
	boolean up, down, left, right, animating = false;
	boolean firstStart = true;
	
	Image character;
	
	public void redraw (Image person){
		character = person;
		repaint();
	}
	public void startGraphics() {
		// TODO Auto-generated method stub
		
		BufferedImage bi;
		try {
			bi = ImageIO.read(new File ("Images/image.png"));
			character = bi.getSubimage(0, 0, 32, 32);
			System.out.println ("Image: " + character);
			repaint();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		animate.initAnimation (this);
		
		System.out.println ("startGraphics method");
		addKeyListener (this);
		this.grabFocus();
		
	}

	public void paintComponent (java.awt.Graphics g){
		super.paintComponent(g);
		System.out.println ("Paint");
		
			g.drawImage (character, 0, 0, 32, 32, null);
		
	}
	
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		int key = arg0.getKeyCode();
		up = false;
		down = false;
		left = false;
		right = false;
		System.out.println ("Key Pressed");
		if (key == KeyEvent.VK_UP){
			up = true;
			System.out.println ("Up pressed");
		}
		if (key == KeyEvent.VK_DOWN){
			down = true;	
		}
		if (key == KeyEvent.VK_LEFT){
			left = true;		
		}
		if (key == KeyEvent.VK_RIGHT){
			right = true;			
		}
		if ((right != false || left != false || up != false || down != false)){
			animating = true;
			System.out.println ("Going to animation");
			animate.startAnimation(up, down, left, right);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		repaint();
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		up = false;
		down = false;
		left = false;
		right = false;
		animating = false;
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

now my problem is that when I hold down the button to animate it, the key pressed button lags the animation. If I tap one of the buttons it works correctly.

What are the times when the keyPressed method receives events?
The OS controls when the events are sent.
Perhaps you want to start a timer that runs until the key is released and sends events to the animation at the period that you want.

The timer fixed it. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.