Java 按下识别键,但图像在我的屏幕上没有移动
我有一个显示在屏幕上的图像,在同一个类中,我添加了一个KeyListener,因此当我按下这些键时,它们会被识别,因为我添加了打印语句来说明它们是否被按下,所以这不是问题所在。问题是图像不会移动。难道我不应该调用按键方法底部的repaint方法吗。我认为应该有一个方法来更新图像,但我不确定如何做到这一点。这是我的代码(我有更多的类,但我认为它们对问题是不必要的): 打印机Java 按下识别键,但图像在我的屏幕上没有移动,java,keylistener,Java,Keylistener,我有一个显示在屏幕上的图像,在同一个类中,我添加了一个KeyListener,因此当我按下这些键时,它们会被识别,因为我添加了打印语句来说明它们是否被按下,所以这不是问题所在。问题是图像不会移动。难道我不应该调用按键方法底部的repaint方法吗。我认为应该有一个方法来更新图像,但我不确定如何做到这一点。这是我的代码(我有更多的类,但我认为它们对问题是不必要的): 打印机 在x,y处绘制图像。但是你增加了imageP.x和imageP.y。这是哪一个?很抱歉,我只是在胡闹(我之前以为这会给我一个
在x,y处绘制图像。但是你增加了imageP.x和imageP.y。这是哪一个?很抱歉,我只是在胡闹(我之前以为这会给我一个错误),它们都是相同的东西,在我的构造函数中,我将x设置为等于imageP.x,并且为yI设置了相同的值,我不太确定你的意思。简单地将其中一个变量的值设置为另一个变量的值并不意味着第一个变量将在更新第二个变量时发生更改。在您发布的代码中,您正在更新一个变量,但正在使用另一个变量绘制图形。如果这不是你实际运行的,我建议发布一个。
package Game;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* This class is used to paint all the graphics onto the screen
*/
public class Printer extends JPanel implements KeyListener, ActionListener{
private Map map;
private Character ch;
private int scale;
private Point imageP;
private BufferedImage chImage;
private ArrayList<Rectangle> part;
private int x;
private int y;
private int width;
private int height;
private int speed;
/**
* Constructor it initializes all the variables and all the keyListeners
* @param scale1 takes in a scale factor so it can multiply and scale everything else
*/
public Printer(int scale1){
scale = scale1;
map = new Map();
ch = new Character();
imageP = ch.getPoint();
chImage = ch.getImage();
part = map.getPart();
x = imageP.x * scale;
y = imageP.y * scale;
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
width = ch.getWidth();
height = ch.getHeight();
speed = ch.getSpeed();
}
/**
* used to paint all the rectangles and the character
* @param takes in the graphics component to draw objects
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=0; i<part.size(); i++){
Rectangle temp = new Rectangle(part.get(i));
g.drawRect(temp.x, temp.y, temp.width, temp.height);
g.fillRect(temp.x, temp.y, temp.width, temp.height);
}
g.drawImage(chImage, imageP.x*scale, imageP.y*scale, null);
}
/**
* Moves the character left right up and down based on what keys you press
* @param listens for keyevents to happen from the user
*/
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_S){
imageP.y += 1*speed;
}
switch(e.getKeyCode()){
case KeyEvent.VK_S:
if(imageP.y <= height-33)
imageP.y += 1*speed;
break;
case KeyEvent.VK_W:
if(imageP.y >=0+5)
imageP.y -= 1*speed;
break;
case KeyEvent.VK_A:
if(imageP.x >=0+5)
imageP.x -= 1*speed;
break;
case KeyEvent.VK_D:
if(imageP.x <= width-30)
imageP.x += 1*speed;
break;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
package Game;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* Creates a image for a character and allows him to walk around the screen
*/
public class Character extends JPanel{
private BufferedImage image;
private Point imageP;
private int speed;
private int scale;
private int width;
private int height;
/**
* Constructs the basics of a Character with minimal info just mainly used to access these class methods
*/
public Character(){
this(50,50,3,300,169);
}
/**
* Creates a more complex Character that takes in more specific information about placement and scaling
* @param x is the x position on the screen
* @param y is the y position on the screen
* @param scale1 is the scale factor that everything gets multiplied by
* @param w is the width of the characters image
* @param h is the height of the characters image
*/
public Character(int x, int y, int scale1, int w, int h){
super();
try {
image = ImageIO.read(new File("F:\\Programming\\Final Project\\Top_down\\narwhal.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
scale = scale1;
imageP = new Point(x,y);
speed = 10;
width = w;
height = h;
}
/**
* lets other classes access the Point variable
* @return the xy points
*/
public Point getPoint(){
return this.imageP;
}
/**
* lets other classes access the Image variable
* @return the image that is viewed
*/
public BufferedImage getImage(){
return this.image;
}
/**
* changes the image that is sent to other classes
*/
public void setImage(BufferedImage img){
this.image = img;
}
/**
* lets other classes access the speed variable at which the character moves around
* @param the speed that is changed in other classes
*/
public void setSpeed(int speed){
this.speed = speed;
}
/**
* lets other classes access the speed variable
* @return the xy points
*/
public int getSpeed(){
return this.speed;
}
/**
* lets other classes access the width variable
* @return the width of the image
*/
public int getWidth(){
return width;
}
/**
* lets other classes access the height variable
* @return the height of the image
*/
public int getHeight(){
return height;
}
}