MouseMotionListener不会将坐标传输到画布

时间:2018-07-27 13:14:23

标签: java canvas awt mousemotionlistener

我希望Mouse类中的坐标转到paint方法中的Paint类。并且正方形随着鼠标的移动而移动。但是坐标不会传输。

g.drawRect(mouse.getX(), mouse.getY(), 10, 10);

mouse.getX(),mouse.getY()仅从开头开始。之后不会改变。

班级抽奖点

public class DrawPoints
{
    Frame f;
    KeyBoard key;
    Mouse mouse;
    Paint c;

    public void GUI()
    {
        f = new Frame("DrawPoints");
        f.setSize(300, 300);

        mouse = new Mouse(); // edit

        c = new Paint(mouse); // edit
        c.setSize(300, 300);
        c.setBackground(Color.WHITE);

        c.addMouseListener(mouse);
        c.addMouseMotionListener(mouse);

        key = new KeyBoard();
        c.addKeyListener(key);

        f.add(c);
        f.pack();
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}

Class Paint

class Paint extends Canvas
{
    Mouse mouse; // edit

    public Paint() // added
    {
        mouse = new Mouse();
    }

    @Override
    public void paint(Graphics g)
    {
        g.drawRect(mouse.getX(), mouse.getY(), 10, 10);
        repaint(); // added
    }
}

鼠标类

class Mouse implements MouseListener, MouseMotionListener
{   
    private int x;
    private int y;

    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;
    }

    // MouseMotionListener
    @Override
    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();
        //System.out.println(e.getX() + " " + e.getY());
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        System.out.println("mouseDragged");
    }

2 个答案:

答案 0 :(得分:0)

忘记了您不应该使用AWT库进行编码的事实,因为此GUI库已过期20多年了,这不是您进行事件驱动编程的方式,因为您需要通知程序中的更改鼠标状态,这是您的代码无法执行的操作。鼠标侦听器方法需要更改绘图Paint对象中的x和y属性,然后在同一对象上调用repaint()

答案 1 :(得分:0)

我不知道其他类,但是我认为问题是您创建了不同的鼠标对象。将新的Mouse()移到新的Paint()之前,然后将新的Paint更改为Paint(鼠标)。 DrawPoints类:

public class DrawPoints
{
    Frame f;
    KeyBoard key;
    Mouse mouse;
    Paint c;

    public void GUI()
    {
        f = new Frame("DrawPoints");
        f.setSize(300, 300);

        mouse = new Mouse();


        c = new Paint(mouse);
        c.setSize(300, 300);
        c.setBackground(Color.WHITE);
        c.addMouseListener(mouse);
        c.addMouseMotionListener(mouse);

        key = new KeyBoard();
        c.addKeyListener(key);



        f.add(c);
        f.pack();
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}}

Paint类:将Paint()更改为Paint(鼠标),并将新的Mouses()行更改为this.mouse = mouse;

class Paint extends Canvas
{
    Mouse mouse;

    public Paint(Mouse mouse)
    {
        this.mouse = mouse;
    }

    @Override
    public void paint(Graphics g)
    {
        g.drawRect(mouse.getX(), mouse.getY(), 10, 10);
    }
}