简体   繁体   English

使用MouseListener Java小程序拖动对象

[英]dragging object using MouseListener Java applet

I'm trying to create an applet that draws a circle (defined as an object) to the screen then this circle can be dragged across the screen using the mouse. 我正在尝试创建一个在屏幕上绘制一个圆(定义为对象)的小程序,然后可以使用鼠标在屏幕上拖动该圆。 So far when the mouse is pressed the object is drawn and can be dragged, but what I want it to do is draw the object when the applet is started then allow the user to click on the object and drag it. 到目前为止,当按下鼠标时,对象已绘制并且可以拖动,但是我要它做的是在applet启动时绘制对象,然后允许用户单击对象并将其拖动。 Any help or clues would be much appreciated. 任何帮助或线索将不胜感激。 here is the code: 这是代码:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class sheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    manAndDog dog;
    int xposR;
    int yposR;

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);

    }
    public void paint(Graphics g)
    {
        dog.display(g);

    }
    public void actionPerformed(ActionEvent ev)
    {}
    public void mousePressed(MouseEvent e)
    {

    }
    public void mouseReleased(MouseEvent e)
    {


    }
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mouseMoved(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseDragged(MouseEvent e)
    {
        dog = new manAndDog(xposR, yposR);
        xposR = e.getX();
        yposR = e.getY();
        repaint();

    }
}

class manAndDog implements MouseListener, MouseMotionListener
{
    int xpos;
    int ypos;
    int circleWidth = 30;
    int circleHeight = 30;
    Boolean mouseClick;

    public manAndDog(int x, int y)
    {
        xpos = x;
        ypos = y;
        mouseClick = true;
        if (!mouseClick){
            xpos = 50;
            ypos = 50;
        }

    }

    public void display(Graphics g)
    {
        g.setColor(Color.blue);
        g.fillOval(xpos, ypos, circleWidth, circleHeight);
    }

    public void mousePressed(MouseEvent e)
    {
        mouseClick = true;
    }
    public void mouseReleased(MouseEvent e)
    {

    }
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mouseMoved(MouseEvent e)
    {}
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseDragged(MouseEvent e)
    {
        if (mouseClick){
            xpos = e.getX();
            ypos = e.getY();
        }


    }
}

Thanks 谢谢

In the start method of your applet, assign a location for the manAndDog object and call repaint 在applet的start方法中,为manAndDog对象分配一个位置,然后调用repaint

Reimeus is more correct, the init method is a better place to initalise the manAndDog. Reimeus更为正确, init方法是init manAndDog的更好位置。

Hope you don't mind some feedback ;) 希望你不介意一些反馈;)

  1. You should be calling super.paint(g) in your paint method. 您应该在paint方法中调用super.paint(g) In fact, I'd encourage you to use JApplet and override paintComponent , but that's just me 实际上,我鼓励您使用JApplet并重写paintComponent ,但这仅是我自己
  2. I don't see the need to continuously recreate the manAndDog object. 我看不到需要连续重新创建manAndDog对象。

For example. 例如。 If you added a method setLocation , you could simply call 'setLocation` when the mouse is dragged. 如果添加了setLocation方法,则可以在拖动鼠标时简单地调用“ setLocation”。

public void mouseDragged(MouseEvent e) {
    dog.setLocation(xposR, yposR);
    xposR = e.getX();
    yposR = e.getY();
    repaint();
}

This is more efficient as it's not continuously creating short lived objects. 由于它不会连续创建寿命短的对象,因此效率更高。 It also means you can do more with the manAndDog object, such as apply animation. 这也意味着您可以使用manAndDog对象做更多的manAndDog ,例如应用动画。 IMHO 恕我直言

最简单的方法是在init()方法中创建ManAndDog对象,如下所示:

dog = new ManAndDog(0, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM