2017-04-22 17 views
0

我试图找出如何完成以下如何实现一个鼠标监听器,这将有助于在java中拖动一个圆圈?

  1. 转让写你的Circle类的方法无效移动(点P)采用一个点和移动圆圈,它的中心是在这一点上。
  2. 在CirclePanel构造函数中,创建一个CirclesListener对象,并使其侦听鼠标事件和鼠标移动事件。
  3. 除了MouseListener接口以外,使CirclesListener类实现MouseMotionListener接口。这需要两个步骤:在标题中注意CirclesListener实现MouseMotionListener。为两个MouseMotionListener方法添加body,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; 
    } 
} 
} 
+0

Circle类是哪里?你也没有实现'MouseListener'和'MouseMotionListener'的大部分需求。您可能会发现[如何编写鼠标侦听程序](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html)有帮助 – MadProgrammer

+0

我已更新该帖子以包含所有三个类。 – sdotriki

回答

2

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

  • 实现的功能,这将包括mouseDragged方法
  • 您需要注册CirclesListenercirclePanelJPanel#addMouseMotionListener
  • mouseDragged被调用时,你需要从MouseEvent采取Point并调用Circle#moverepaint组件

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