简体   繁体   English

如何实现将有助于在Java中拖动圆圈的鼠标侦听器?

[英]How to implement a mouse listener that will help drag a circle in java?

I am trying to figure out how to complete the assignment below 我正在尝试找出如何完成以下任务

  1. Write a method void move(Point p) for your Circle class that takes a Point and moves the circle so its center is at that point. 为您的Circle类编写一个方法void move(Point p),该方法获取一个Point并移动圆,使其圆心位于该点。
  2. In the CirclePanel constructor, create a CirclesListener object and make it listen for both mouse events and mouse motion events. 在CirclePanel构造函数中,创建一个CirclesListener对象,并使它侦听鼠标事件和鼠标运动事件。
  3. Make the CirclesListener class implement the MouseMotionListener interface in addition to the MouseListener interface. 使CirclesListener类除了MouseListener接口之外,还实现MouseMotionListener接口。 This requires two steps: Note in the header that CirclesListener implements MouseMotionListener. 这需要两个步骤:在标头中注意CirclesListener实现MouseMotionListener。 Add bodies for the two MouseMotionListener methods, mouseDragged and mouseMoved. 为两个MouseMotionListener方法,mouseDragged和mouseMoved添加主体。 In mouseDragged, simply move the circle to the point returned by the getPoint method of the MouseEvent and repaint. 在mouseDragged中,只需将圆移动到MouseEvent的getPoint方法返回的点并重新绘制即可。 Provide an empty body for mouseMoved. 为mouseMoved提供一个空的主体。

I know what I need to do (for the most part), I just can't figure out how to do it. 我知道我需要做的事情(大部分情况下),我只是不知道该怎么做。 (New to programming). (编程新手)。 Thank you! 谢谢!

public class circlePanel extends JPanel {
private final int WIDTH = 600, HEIGHT = 400;
private Circle circle;

// -------------------------------------------------------------------
// Sets up this panel to listen for mouse events.
// -------------------------------------------------------------------
public circlePanel() {
    addMouseListener(new CirclesListener());
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
}

// -------------------------------------------------------------------
// Draws the current circle, if any.
// -------------------------------------------------------------------
public void paintComponent(Graphics page) {
    super.paintComponent(page);
    if (circle != null)
        circle.draw(page);
}

// ******************************************************************
// Represents the listener for mouse events.
// ******************************************************************
private class CirclesListener implements MouseListener, MouseMotionListener {
    // ---------------------------------------------------------------
    // Creates a new circle at the current location whenever the
    // mouse button is pressed and repaints.
    // ---------------------------------------------------------------
    public void mousePressed(MouseEvent event) {
        if (circle == null) {
            circle = new Circle(event.getPoint());
        } else if (circle.isInside(event.getPoint())) {
            circle = null;
        } else {
            circle.move(getMousePosition());
        }
        repaint();
    }

    // -----------------------------------------------------------------
    // Provide empty definitions for unused event methods.
    // -----------------------------------------------------------------
    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
        setBackground(Color.white);
    }

    public void mouseExited(MouseEvent event) {
        setBackground(Color.blue);
    }
}
}

Here is the circles class 这是圈子课

public class Circles {
// ----------------------------------------------------------------
// Creates and displays the application frame.
// ----------------------------------------------------------------
public static void main(String[] args) {
    JFrame circlesFrame = new JFrame("Circles");
    circlesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    circlesFrame.getContentPane().add(new circlePanel());
    circlesFrame.pack();
    circlesFrame.setVisible(true);
}
}

aannnddddd...here is the Circle class aannnddddd ...这是Circle类

public class Circle {
private int centerX, centerY;
private int radius;
private Color color;
static Random generator = new Random();

// ---------------------------------------------------------
// Creates a circle with center at point given, random radius and color
// -- radius 25..74
// -- color RGB value 0..16777215 (24-bit)
// ---------------------------------------------------------
public Circle(Point point) {
    radius = Math.abs(generator.nextInt()) % 50 + 25;
    color = new Color(Math.abs(generator.nextInt()) % 16777216);
    centerX = point.x;
    centerY = point.y;
}

// ---------------------------------------------------------
// Draws circle on the graphics object given
// ---------------------------------------------------------
public void draw(Graphics page) {
    page.setColor(color);
    page.fillOval(centerX - radius, centerY - radius, radius * 2,     
radius * 2);
}

public void move(Point p) {
    centerX = p.x;
    centerY = p.y;
}

public boolean isInside(Point p) {
    if (Math.sqrt(Math.pow(p.x - this.centerX, 2) + Math.pow(p.y - 
this.centerY, 2)) < this.radius) {
        return true;
    } else {
        return false;
    }
}
}

So, basically, based on you code example, you need: 因此,基本上,根据您的代码示例,您需要:

  • Implement the functionality of the MouseMotionListener , this will include the mouseDragged method 实现MouseMotionListener的功能,其中将包括mouseDragged方法
  • You need to register the CirclesListener to the circlePanel ( JPanel#addMouseMotionListener ) 您需要将CirclesListener注册到circlePanelJPanel#addMouseMotionListener
  • When mouseDragged is called, you need to take the Point from the MouseEvent and call Circle#move and repaint the component 调用mouseDragged ,您需要从MouseEvent获取Point并调用Circle#moverepaint组件

If you get stuck, best place to start is How to Write a Mouse Listener 如果遇到困难,最好的起点是如何编写鼠标侦听器

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

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