使用键盘箭头和 mouseEvent 在 Java 中移动图形

Move a Graphic in Java,using keyboard arrows and mouseEvent

我是初学者 this.Doing 一个测试画三角形,现在我想移动 it.it 不行所以我想问一下我的 mistake.Sorry它的长。 导入 javax.swing.JFrame; 导入 javax.swing.JPanel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;

public class Test extends JFrame {
private Triangle triangle;
private final int step = 10;
private Triangle keyboardPanel = new Triangle();

public static void main(String[] args)
{
    Test t = new Test();

}

public Test()
{

    setTitle("TRY TRY TRY");
    setLocationRelativeTo(null); // Center the frame
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 500, 500);

    JPanel tripanel = new JPanel();

    add(tripanel);

    triangle = new Triangle(); 

    tripanel.addKeyListener(null);
    tripanel.addMouseListener(null);
    tripanel.addMouseMotionListener(null);

    setVisible(true);
}

   static class move extends JPanel implements KeyListener,MouseListener,        MouseMotionListener{

    private int x = 210;
    private int y = 210;
    private Color color = Color.BLACK;


     move()
    {

    addKeyListener(this);
    addMouseListener(this);
    addMouseMotionListener(this);
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        int KeyCode = ke.getKeyCode();
        System.out.println("key code is" +KeyCode);


        /*switch (KeyCode)
        {
        case KeyEvent.VK_UP:
            triangle.moveTriangle(-10, 0);
            break;
        case KeyEvent.VK_DOWN:
            triangle.moveTriangle(10, 0);
            break;
        case KeyEvent.VK_LEFT:
            triangle.moveTriangle(0, -10);
            break;
        case KeyEvent.VK_RIGHT:
            triangle.moveTriangle(0, 10);
            break; 
        }
        repaint();*/

    }

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

    }

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

    }

    @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 e) {
        System.out.println("hello");
        x = e.getX();
        y = e.getY();
        repaint();
    }

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

    @Override
    public void mouseDragged(MouseEvent e) {

        x = e.getX();
        y = e.getY();
        System.out.println("hello123");
        if(e.isControlDown())
            color = Color.RED;

        else
            color = Color.BLACK;
        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println(e.getPoint());
    }

}

public void paint(Graphics g)
{
    super.paint(g);
    triangle.drawTriangle(g);
}

public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    triangle.drawTriangle(g);
}


}

import java.awt.Graphics;

import javax.swing.JPanel;


public class Triangle {
private Point p1;
private Point p2;
private Point p3;

int numX;
int numY;

public Triangle()
{

    p1 = new Point(200,200);
    p2 = new Point(170,230);
    p3 = new Point(230,230); 
}

public void moveTriangle(int dx, int dy)
{
    p1.move(dx, dy);
    p2.move(dx, dy);
    p3.move(dx, dy);

}

public void drawTriangle(Graphics g)
{
    g.drawLine(p1.getX(), p1.getY(),p2.getX(),p2.getY());
    g.drawLine(p2.getX(),p2.getY(),p3.getX(),p3.getY());
    g.drawLine(p3.getX(), p3.getY(),p1.getX(),p1.getY());
}
}
public class Point {
private int x;
private int y;

public Point(int X, int Y)
{
    x = X;
    y = Y;
}

public void setX(int X)
{
    x = X;
}

public void setY(int Y)
{
    x = Y;
}

public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

public void move(int dx, int dy)
{
    x +=dx;
    y +=dy;
}

public String toString()
{
    return("X = "+x+" Y= "+y);
}
}

你的代码中有很多问题。当你编码时,在继续之前总是测试当前代码。

您创建一个面板并添加到 jframe 中。

JPanel tripanel = new JPanel();
add(tripanel);

但是你的图形机制已经写在move面板中所以你需要制作一个移动面板而不是jpanel。

keylistners 已添加到面板,但 keylisners 未被触发,因为当组件获得焦点时,keylistners 工作。它适用于像 jtextfiled 这样的组件。但不适用于面板。你必须使用keybinding。我已经为右箭头键添加了键绑定。你必须将它添加到其他键上,下等..

还将您的绘画方法从 jframe 移动到移动 jpanel 。

示例代码(运行此代码并按向右箭头->

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Test extends JFrame {

    private Triangle triangle;
    private final int step = 10;
    private Triangle keyboardPanel = new Triangle();

    public static void main(String[] args) {
        Test t = new Test();

    }

    public Test() {

        setTitle("TRY TRY TRY");
        setLocationRelativeTo(null); // Center the frame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 500);

        JPanel tripanel = new move();

        add(tripanel);

        triangle = new Triangle();

        setVisible(true);
    }

    class move extends JPanel implements MouseListener, MouseMotionListener {

        private int x = 210;
        private int y = 210;
        private Color color = Color.BLACK;

        move() {

            addMouseListener(move.this);
            addMouseMotionListener(move.this);

            this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "moveright");
            this.getActionMap().put("moveright", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    triangle.moveTriangle(10, 0);
                    repaint();
                }
            });

        }

        @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 e) {
            System.out.println("hello");
            x = e.getX();
            y = e.getY();
            repaint();
        }

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

        @Override
        public void mouseDragged(MouseEvent e) {

            x = e.getX();
            y = e.getY();
            System.out.println("hello123");
            if (e.isControlDown()) {
                color = Color.RED;
            } else {
                color = Color.BLACK;
            }
            repaint();

        }

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub
            //System.out.println(e.getPoint());
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            triangle.drawTriangle(g);
        }

    }

    class Triangle {

        private Point p1;
        private Point p2;
        private Point p3;

        int numX;
        int numY;

        public Triangle() {

            p1 = new Point(200, 200);
            p2 = new Point(170, 230);
            p3 = new Point(230, 230);
        }

        public void moveTriangle(int dx, int dy) {
            p1.move(dx, dy);
            p2.move(dx, dy);
            p3.move(dx, dy);

        }

        public void drawTriangle(Graphics g) {
            g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
            g.drawLine(p2.getX(), p2.getY(), p3.getX(), p3.getY());
            g.drawLine(p3.getX(), p3.getY(), p1.getX(), p1.getY());
        }
    }

    class Point {

        private int x;
        private int y;

        public Point(int X, int Y) {
            x = X;
            y = Y;
        }

        public void setX(int X) {
            x = X;
        }

        public void setY(int Y) {
            x = Y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public void move(int dx, int dy) {
            x += dx;
            y += dy;
        }

        public String toString() {
            return ("X = " + x + " Y= " + y);
        }
    }
}

输出>>