【问题标题】:KeyPressed recognized but the Image is not moving on my screenKeyPressed 已识别,但图像未在我的屏幕上移动
【发布时间】:2014-01-10 01:29:40
【问题描述】:

我有一个显示在屏幕上的图像,并且在同一个类中我添加了一个 KeyListener,因此当我按下它们时会识别这些键,因为我添加了打印语句来说明它们是否被按下,所以这不是问题.问题是图像不会移动。我不应该在我的 Keypressed 方法的底部调用 repaint 方法吗?我认为应该有一种方法来更新图像,但我不确定如何做到这一点。这是我的代码(我有更多类,但我认为它们对问题没有必要):

打印机

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;
    }

}

【问题讨论】:

    标签: java keylistener


    【解决方案1】:

    你在 x, y 处绘制图像。但是你增加了 imageP.x 和 imageP.y。是哪一个?

    【讨论】:

    • 对不起,我只是搞砸了(我以为这早给了我一个错误)它们都是一样的,在我的构造函数中,我设置 x 等于 imageP.x 和 y 相同
    • 我不太确定你的意思。简单地设置一个变量等于另一个变量的值并不意味着当你更新第二个变量时第一个变量会改变。在您发布的代码中,您正在更新一个变量,但使用另一个变量进行绘制。如果这不是你实际运行的,我建议发布SSCCE
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多