I'm creating a program for a school project. It is a fan based program of Pokémon and I am having a little trouble understanding how to change images based on the key strokes.
Here is the code so far for the Character Class
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.*; //the File class
import java.util.*; //the Scanner class
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.image.*;
import javax.swing.ImageIcon;
public class MainCharacter {
private Image up, up1, up2, down, down1, down2, left, left1, left2, right, right1, right2;
private void loadImages() {
up = new ImageIcon("Up.png").getImage();
up1 = new ImageIcon("Up1.png").getImage();
up2 = new ImageIcon("Up2.png").getImage();
down = new ImageIcon("Down.png").getImage();
down1 = new ImageIcon("Down1.png").getImage();
down2= new ImageIcon("Down2.png").getImage();
left = new ImageIcon("Left.png").getImage();
left1 = new ImageIcon("Left1.png").getImage();
left2 = new ImageIcon("Left2.png").getImage();
right = new ImageIcon("Right.png").getImage();
right1 = new ImageIcon("Right1.png").getImage();
right2 = new ImageIcon("Right2.png").getImage();
}
/*public Image getImage()
{
//dont know what to return here
}
*/
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
//dont know what to do
}
if (key == KeyEvent.VK_RIGHT) {
//dont know what to do
}
if (key == KeyEvent.VK_UP) {
//dont know what to do
}
if (key == KeyEvent.VK_DOWN) {
//dont know what to do
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
//dont know what to do
}
if (key == KeyEvent.VK_RIGHT) {
//dont know what to do
}
if (key == KeyEvent.VK_UP) {
//dont know what to do
}
if (key == KeyEvent.VK_DOWN) {
//dont know what to do
}
}
}
Here is the class where it moves along with the map:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MapMovement extends JPanel implements ActionListener {
private Timer timer;
private Map map;
private MainCharacter mainCharacter;
public MapMovement() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
map = new Map();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(map.getImage(), map.getX(), map.getY(), 1000, 1500, this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
map.move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
map.keyReleased(e);
//mainCharacter.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
map.keyPressed(e);
//mainCharacter.keyPressed(e);
}
}
}
UPDATE:
here is the Map class
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Map {
private String map = "Map-1stCity.png";
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public Map() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(map));
image = ii.getImage();
x = -100;
y = -100;
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = -1;
}
if (key == KeyEvent.VK_UP) {
dy = 1;
}
if (key == KeyEvent.VK_DOWN) {
dy = -1;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
Thank you for all your help in advance!
Overview
Basically, you're going to need to know the current key state and the current frame (based on the fact that you have two images per position).
With this information, you can then determine which character image you should be displaying.
Example
This example is very simple and it demonstrates the key bindings API over using
KeyListener
as it resolves issues dealing with focus. It also demonstrates how easy it is to modify the current state through a single baseAction
The example's
MainCharacter
has a single method (of interest) which is used to determine which image should be displayed based on the currentKeyState
(anenum
in this example) and the currentframe
.The
getCharacter
method switches the image every 10 frames in order to provide the animation between the current position...As I said in my comments, don't call
dispose
on aGraphics
context you did not create as this can affect not only what you are painting but what might be painted after you.