Question: I cant figure the logic to my code using java, everytime I use the arrow keys to change the positon of my player in the

I cant figure the logic to my code using java, everytime I use the arrow keys to change the positon of my player in the screen I want to be able to change the image of my player when import it. If I press left of right then my player should switch to the running image,it should also use values from the constant class, that way my code can figure out how many frames are on each images. if dont move then my character should go back to the idle image. I have keyboard input class and gamepanel class. Please check my code
package main;
/* Step 1: lets import an image
*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import inputs.KeyboardInputs;
import static utilz.Constants.PlayerConstants.*;
import static utilz.Constants.Directions.*;
public class GamePanel extends JPanel{
private float xDelta =100, yDelta =100;
private BufferedImage img;
private BufferedImage[] animations;
private int aniTick, aniIndex, aniSpeed =15;
private int playerAction = IDLE;
private int playerDir =-1;
private boolean moving = false;
private String actions []={"idle", "run"};
private int currentAction =0;
public GamePanel(){
setPanelSize();
importImg();
loadAnimations();
}
public void setPanelSize(){
Dimension size = new Dimension(1200,800);
setPreferredSize(size);
}
private void loadAnimations(){
animations = new BufferedImage[9];
// int x_positions []={0,120,240,360,480,600,720,840,960,1080,};
for(int i =0; i< animations.length; i++){
//i times the width of the sprite
//the stupid x in the sub image, is the start of the x not the width
animations[i]= img.getSubimage(i *120,0,40,39);
}
}
private void importImg(){
InputStream is = getClass().getResourceAsStream("/Knight_"+playerAction+".png");
try {
img = ImageIO.read(is);
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setDirection(int direction){
this.playerDir = direction;
moving = true;
}
public void setMoving(boolean moving){
this.moving = moving;
}
private void setAnimation(){
if (moving)
playerAction = RUNNING;
else
playerAction = IDLE;
}
private void updatePos(){
if (moving){
switch (playerDir){
case LEFT:
xDelta -=5;
playerAction = RUNNING;
break;
case UP:
yDelta -=5;
break;
case RIGHT:
xDelta +=5;
playerAction = RUNNING;
break;
case DOWN:
yDelta +=5;
break;
}
}
}
private void updateAnimationTick(){
aniTick++;
if(aniTick >= aniSpeed){
aniTick =0;
aniIndex ++;
if(aniIndex >= animations.length){
aniIndex =0;
}
}
}
public void paint(Graphics pen){
super.paintComponent(pen);
updateAnimationTick();
updatePos();
pen.drawImage(animations[aniIndex],0,0,100,100, null);
}
}
package inputs;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import main.GamePanel;
import static utilz.Constants.Directions.*;
public class KeyboardInputs implements KeyListener {
private GamePanel gamePanel;
public KeyboardInputs(GamePanel gamePanel){
this.gamePanel = gamePanel;
}
@Override
public void keyTyped(KeyEvent e){
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_W:
case KeyEvent.VK_A:
case KeyEvent.VK_S:
case KeyEvent.VK_D:
gamePanel.setMoving(false);
break;
}
}
@Override
public void keyPressed(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_W:
gamePanel.setDirection(UP);
break;
case KeyEvent.VK_A:
gamePanel.setDirection(LEFT);
break;
case KeyEvent.VK_S:
gamePanel.setDirection(DOWN);
break;
case KeyEvent.VK_D:
gamePanel.setDirection(RIGHT);
break;
}
}
}
package utilz;
public class Constants {
public static class Directions {
public static final int LEFT =0;
public static final int UP =1;
public static final int RIGHT =2;
public static final int DOWN =3;
}
public static class PlayerConstants {
public static final int IDLE =0;
public static final int RUNNING =1;
public static final int JUMP =2;
public static final int HIT =3;
public static int GetSpriteAmount(int player_action){
switch (player_action){
case RUNNING:
return 9;
case IDLE:
return 9;
case HIT:
r

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!