4

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

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

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

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

4

2 回答 2

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());
    }
}
于 2012-07-23T00:55:35.833 回答
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();
         }
      });
   }
}
于 2012-07-23T00:52:17.050 回答