将鼠标保持在Ellipse2D中

时间:2012-07-23 00:13:56

标签: java cursor position mouse

我知道如何将我的鼠标(我的g.draw(mouseX,mouseY)光标)保持在Ellipse2D / Shape ...

@Override
public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    if(shape.contains(x, y)) {
        mouseMoveX = e.getX();
        mouseMoveY = e.getY();
    }
}

...但是当鼠标离开所述形状时(直到它返回),这将完全锁定运动。 IE即使实际光标移动,它仍保持在相同的位置。我希望鼠标能够在椭圆周围移动,即使实际光标已经出来。你们中的许多人可能仍然感到困惑,对不起,如果需要更多的解释,我很乐意帮忙。另外,这里的第一个问题,如果我违反任何规则,请告诉我!感谢。

PS:对于任何迟到的回复感到抱歉,目前正在拨号上网:(

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用java.awt.Robot类,它允许您直接控制鼠标和键盘:

import java.awt.Robot;

...

Robot robot = new Robot(<your GraphicsDevice>);

...

@Override
public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    if(shape.contains(x, y)) {
        mouseMoveX = e.getX();
        mouseMoveY = e.getY();
    }
    else {
        robot.mouseMove(mouseMoveX,mouseMoveY); // Assuming these are the previous coordinates.
    }
}

编辑:好的,请改为尝试:

@Override
public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    if (shape.contains(x, y)) {
        mouseMoveX = e.getX();
        mouseMoveY = e.getY();
    }
    else {
        // get angle of rotation
        double r = Math.atan2(y-shape.getCenterY(),x-shape.getCenterX());
        mouseMoveX = (int) (shape.getWidth()/2 * Math.cos(r) + shape.getCenterX());
        mouseMoveY = (int) (shape.getHeight()/2 * Math.sin(r) + shape.getCenterY());
    }
}

答案 1 :(得分:0)

根据位置设置光标的示例:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

public class CursorMagic extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 400;
   private static final Color ELLIPSE_COLOR = Color.red;
   private static final Color ELLIPSE_FILL_COLOR = Color.pink;
   private static final Stroke ELLIPSE_STROKE = new BasicStroke(3f);
   private Ellipse2D ellipse = new Ellipse2D.Double(PREF_W / 4, PREF_H / 4, PREF_W / 2, PREF_H / 2);

   public CursorMagic() {
      MyMouseAdapter mouseAdapter = new MyMouseAdapter();
      addMouseMotionListener(mouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(ELLIPSE_FILL_COLOR);
      g2.fill(ellipse);
      g2.setColor(ELLIPSE_COLOR);
      g2.setStroke(ELLIPSE_STROKE);
      g2.draw(ellipse);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mouseMoved(MouseEvent mEvt) {
         if (ellipse.contains(mEvt.getPoint())) {
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
         } else {
            setCursor(null);
         }
      }
   }

   private static void createAndShowGui() {
      CursorMagic mainPanel = new CursorMagic();

      JFrame frame = new JFrame("CursorMagic");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}