gpt4 book ai didi

java - 如何在java中实现一个鼠标监听器来帮助拖动一个圆圈?

转载 作者:行者123 更新时间:2023-12-02 02:52:47 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何完成下面的作业

  1. 为 Circle 类编写一个方法 void move(Point p),该方法接受一个 Point 并移动圆,使其中心位于该点。
  2. 在 CirclePanel 构造函数中,创建一个 CirclesListener 对象并使其监听鼠标事件和鼠标移动事件。
  3. 除了 MouseListener 接口(interface)之外,还使 CirclesListener 类实现 MouseMotionListener 接口(interface)。这需要两个步骤: 请注意 header 中 CirclesListener 实现了 MouseMotionListener。为两个 MouseMotionListener 方法(mouseDragged 和 mouseMoved)添加主体。在 mouseDragged 中,只需将圆移动到 MouseEvent 的 getPoint 方法返回的点并重新绘制即可。为 mouseMoved 提供一个空主体。

我知道我需要做什么(大部分),但我只是不知道该怎么做。 (编程新手)。谢谢!

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);
}
}
}

这是圆圈类

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...这是 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;
}
}
}

最佳答案

所以,基本上,根据您的代码示例,您需要:

  • 实现 MouseMotionListener 的功能,这将包括 mouseDragged 方法
  • 您需要将 CirclesListener 注册到 circlePanel ( JPanel#addMouseMotionListener )
  • 当调用mouseDragged时,您需要从MouseEvent中获取Point并调用Circle#move重新绘制组件

如果您遇到困难,最好的起点是 How to Write a Mouse Listener

关于java - 如何在java中实现一个鼠标监听器来帮助拖动一个圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43555743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com