KeyPressed已识别但图像未在我的屏幕上移动

时间:2014-01-10 01:29:41

标签: java keylistener

我有一个显示在屏幕上的图像,并且在同一个类中我添加了一个KeyListener,所以当我按下它们时会识别键,因为我添加了print语句来说明是否按下它们以免这是问题。问题是图像不会移动。我不应该在我的Keypressed方法的底部调用重绘方法。我认为应该有一种更新图像的方法,但我不确定如何做到这一点。这是我的代码(我有更多的课程,但我认为他们不需要这个问题):

打印机

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

}

1 个答案:

答案 0 :(得分:0)

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