application not cooperating with arrow keys

时间:2018-03-25 18:53:08

标签: java keylistener

I'm attempting to use KeyListener to interact with the arrow keys, when I run it, it doesn't give me an error, but the rectangle I am trying to move left or right does not move. Am I missing something? I'm trying to simulate the game for bricks, so My only issue right now is getting the panel to move across the screen

 import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

import org.w3c.dom.css.Rect;

public class tools extends JPanel implements KeyListener, ActionListener {
    Timer t = new Timer(5,this);
    double x = 350, y=920,velx=0,vely=0;

    public tools() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }


        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            Graphics2D g2 = (Graphics2D)g;
            g.setColor(Color.BLUE);
            g2.fill(new Ellipse2D.Double(x, y, 300, 10));




            //ball
            /*
            g.setColor(Color.WHITE);
            g.fillOval(350, 700, 20, 20);
            */


        }




        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            x+= velx;
            y+= vely;
        }

        public void left() {
    velx = -1.5;
    vely=0;
}
        public void right() {
    velx=1.5;
    vely=0;

}

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            if(keyCode == KeyEvent.VK_RIGHT)
            {
                right();
            }
            else if(keyCode == KeyEvent.VK_LEFT) 
            {
                left();
            }


        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

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

        }






}

1 个答案:

答案 0 :(得分:0)

You need to call repaint() to redraw the canvas. Have a look at the example below.

public class Bricks extends JPanel implements KeyListener, ActionListener {

    Timer t = new Timer(5, this);
    double x = 150, y = 320, velx = 0, vely = 0;

    public Bricks() {
        t.start();
        setFocusable(true);
        requestFocus();
        addKeyListener(this);
        //setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLUE);
        g2.fill(new Ellipse2D.Double(x, y, 300, 10));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        x += velx;
        y += vely;
    }

    public void left() {
        velx = -1.5;
        vely = 0;
    }

    public void right() {
        velx = 1.5;
        vely = 0;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();

        if (keyCode == KeyEvent.VK_RIGHT) {

            System.out.println("RIGHT");

            right();

            repaint();
        } else if (keyCode == KeyEvent.VK_LEFT) {

            System.out.println("LEFT");
            left();

            repaint();
        }

    }

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

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

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Bricks bricks = new Bricks();

                JFrame frame = new JFrame();
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setSize(1078, 800);
                frame.getContentPane().add(bricks, BorderLayout.CENTER);

            }
        });
    }
}