-1

Hello people of stackoverflow, I am currently creating a small game just to learn more about graphics and game creation in Java. However, I am stuck. I'm trying to create a "walking animation" by looping through several small images and then repainting them every image. I'm not sure if there is a particular way to do this or if the images are being repainted so quick that I cannot actually see the "animation". I've broken the game up currently into a Ninja, GameBoard, and Interface class. The interface does the basic GUI creation calls and adds the GameBoard to the GUI. The GameBoard has keyboard listeners and paints the images to the board. The Ninja class has keyboard events and basically is in charge of getting the correct images for the ninja animations.

INTERFACE CLASS

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.*;

public class Interface extends JFrame
{

int height;
int width;

public Interface()
{
    height = 600;
    width = 600;
    initInterface();
}

public Interface(int h, int w)
{
    height = h;
    width = w;
    initInterface();

}

private void initInterface()
{
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.add(new GameBoard());
    this.setTitle("Ninja Clash");
    this.setSize(height,width);
    this.setResizable(false);
}




}

GAMEBOARD CLASS

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GameBoard extends JPanel
{

public Ninja ninja;

public GameBoard()
{
    addKeyListener(new TAdapter());
    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);
    ninja = new Ninja();
}

public void paint(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    g2.drawImage(ninja.getImage(), 20,20,null);
}

private class TAdapter extends KeyAdapter
{
    public void keyPressed(KeyEvent e)
    {

       ninja.keyPressed(e, 1);
       repaint();
       ninja.keyPressed(e, 2);
       repaint();

    }


    public void keyReleased(KeyEvent e)
    {
        ninja.keyReleased(e);
        repaint();
    }
}
}

NINJA CLASS

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class Ninja
{
Image ninja;


public Ninja()
{
    ImageIcon iI = new ImageIcon(this.getClass().getResource("nin1.png"));
    ninja = iI.getImage();

}

public Image getImage()
{
    return ninja;
}

public void keyPressed(KeyEvent e, int count)
{
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
    {

        if (count == 1)
        {
            ImageIcon icon = new ImageIcon(this.getClass().getResource("nin2.png"));
            ninja = icon.getImage();
        }

        else
        {
            ImageIcon icon = new ImageIcon(this.getClass().getResource("nin3.png"));
            ninja = icon.getImage();
        }

    }


}


public void keyReleased(KeyEvent e)
{
    if (e.getKeyCode() == 39)
    {
        ImageIcon icon = new ImageIcon(this.getClass().getResource("nin1.png"));
        ninja = icon.getImage();
    }
}



}
Cory Smith
  • 27
  • 1
  • 7
  • What is your issue? Is listeners are not working? do you want to rotate images after some specific time interval? Please summarize you question. – Braj Mar 30 '14 at 15:02
  • Oh, my apologies. Basically, when I press the right arrow key, I want the first walking .png to be painted and then the second .png gets painted. However, I end up only with the second .png, at least that I can see which gets painted. So when I hit the right arrow I want two images to be painted creating an illusion of walking. Only one shows. – Cory Smith Mar 30 '14 at 15:06
  • Use Swing timer to put a delay between first and second image drawing. – Braj Mar 30 '14 at 15:08

1 Answers1

0

You should not use Thread.sleep(..) on Event Dispatch Thread (or any Thread where sleep will cause unwanted execution blocking), as this will cause the UI to seem frozen.

Try with any one:


A lot of ways are mentioned at below post along with some good suggestion

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • This actually does work thank you! There some timing issues that I need to work out, but this does work! – Cory Smith Mar 30 '14 at 15:43
  • 1
    @CorySmith You have to use a swing timer read more here [How to use Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) and you should override `paintComponent()` instead of `paint` and in first line call `super.paintComponent()` to follow graphic painting method chain. – nachokk Mar 30 '14 at 16:20
  • Oh thanks Nachokk, I'll override paintComponent(). Sadly, now I'm on to the next problem of receiving multiple key presses (holding down right arrow), which is some how resetting the timer and making the "character" walk in super speed. – Cory Smith Mar 30 '14 at 18:12
  • 1
    @CorySmith see this [site](http://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/) has examples about motion using the keyboard, using keyBindings and keylisteners – nachokk Mar 30 '14 at 19:27