KeyListener和Paint不起作用

时间:2016-06-27 07:21:00

标签: java swing paint runnable

我正在制作一款游戏,而我正在努力让自己动起来。这是我到目前为止所拥有的。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class ForgottenMain extends JFrame implements KeyListener,MouseListener{
    /**
     * 
     */
    private static final long serialVersionUID = -4926251405849574401L;
    public static BufferedImage attic,flashlight,player;
    public static boolean up,down,left,right;
    public static int px,py;
    public static void main(String[] args){
        try{    
            player = ImageIO.read(new File("char.png"));
        } catch (Exception e){
            e.printStackTrace();
        }
        new ForgottenMain();


    }
    public ForgottenMain(){
        init();
    }
    public void init(){
        setSize(1200,800);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setTitle("Forgotten");

          java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    if(up == true){
                        System.out.println("Increasing the coordinate");
                        py--;
                        repaint();

                    }
                    if(down == true){
                        py++;
                        repaint();
                    }
                    if(left == true){
                        px--;
                        repaint();
                    }
                    if(right == true){
                        px++;
                        repaint();
                    }

                }
            });


    }
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(attic,0,0,this);
        g2.drawImage(player, px, py, this);
        g2.drawImage(flashlight, px, py, this);
    }
    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == 87){
            up = true;
            System.out.println("Up = true");
        }
        if(e.getKeyCode() == 83){
            down = true;
        }
        if(e.getKeyCode() == 65){
            left = true;
        }
        if(e.getKeyCode() == 68){
            right = true;
        }

    }
    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == 87){
            up = false;
        }
        if(e.getKeyCode() == 83){
            down = false;
        }
        if(e.getKeyCode() == 65){
            left = false;
        }
        if(e.getKeyCode() == 68){
            right = false;
        }

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

关键监听器正在工作,但我认为我在循环/调用后面做错了。我之前从未使用它,所以有人可能会解释我做错了吗?

0 个答案:

没有答案