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

时间:2015-08-19 14:28:35

标签: java mouseevent mouselistener

我是学习这个的初学者。通过绘制一个三角形进行测试,现在我想移动它。它没有工作所以我想问我的错误是什么。结果很长。     import javax.swing.JFrame;     import 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);
}
}

1 个答案:

答案 0 :(得分:0)

您的代码中存在很多问题。当您编码时,请始终先测试当前代码。

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

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

但您的图形机制已在move面板中编写,因此您需要创建一个移动面板而不是jpanel。

keylistners已添加到面板,但keylisners未被触发,因为密钥管理器在组件集中时工作。它适用于像jtextfiled这样的组件。但不是面板。你必须使用keybinding。我已经为rightarrow键添加了keybind ..你必须将它添加到其他键上,下等。

还可以从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);
        }
    }
}

输出>>

enter image description here