使用鼠标在Java中设置弧的位置 [英] Setting arc position in Java using Mouse
问题描述
paintComponent
上创建了一个弧线。 public class Board extends Panel {
protected void paintComponent(Graphics g){
super.paintComponent方法(克);
Graphics2D graphics2d =(Graphics2D)g;
int x = MouseInfo.getPointerInfo()。getLocation()。x; //设置鼠标当前位置
int y = MouseInfo.getPointerInfo()。getLocation()。y;
graphics2d.setStroke(wideStroke);
graphics2d.draw(新的Arc2D.Double(200,200,100,100,?,180,Arc2D.OPEN));
$ b code
$ b $ p $在我的主要中,我正在使用线程
来更新图形。 ?
的位置是起始角度。每当我改变这个弧线就会像半个车轮一样移动。是否有可能使弧形运动跟随鼠标?例如? = 270
我会如何做到这一点? (对不起,我的坏涂料技能!)
解决方案
$ b <我们需要两件事。我们需要锚点(这将是弧的中心点)和目标点,这将是鼠标点。
使用 MouseMotionListener
,它可以监视组件内的鼠标移动。
//引用最后的已知的鼠标位置...
private point mousePoint;
// ....
addMouseMotionListener(new MouseAdapter(){
@Override
public void mouseMoved(MouseEvent e){
mousePoint = e.getPoint() ;
repaint();
}
});
接下来我们需要计算这两个点之间的角度......
if(mousePoint!= null){
//这表示锚点,在这种情况下,
//组件...
int x = width / 2;
int y = height / 2;
//这是定位点
//和鼠标之间的区别。在组件本地坐标空间内完成
//很重要,
//这意味着MouseMotionListener需要
//被注册到组件本身(最好)
//或者鼠标坐标需要转换为
//本地坐标空间
int deltaX = mousePoint.x - x;
int deltaY = mousePoint.y - y;
//计算角度...
//这是我们的0或者起始角度..
rotation = -Math.atan2(deltaX,deltaY);
rotation = Math.toDegrees(rotation)+ 180;
}
从这里开始,你需要减去90度,开始角度,然后使用180度的范围。
I am writing a 2D program. On my paintComponent
I created an arc.
public class Board extends Panel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D graphics2d = (Graphics2D)g;
int x = MouseInfo.getPointerInfo().getLocation().x;//set mouses current position
int y = MouseInfo.getPointerInfo().getLocation().y;
graphics2d.setStroke(wideStroke);
graphics2d.draw(new Arc2D.Double(200, 200, 100, 100, ?, 180, Arc2D.OPEN));
}
}
In my main I am using a Thread
to update the graph. The position of the ?
is the starting angle. Every time I change this the arc will move in a circle like half a car wheel. Is it possible to get the arc movement to follow the mouse? e.g. ? = 270
How will I do this? (Sorry for my bad paint skills!)
解决方案 So based on the information from Java 2d rotation in direction mouse point
We need two things. We need the anchor point (which would be the centre point of the arc) and the target point, which would be the mouse point.
Using a MouseMotionListener
, its possible to monitor the mouse movements within the component
// Reference to the last known position of the mouse...
private Point mousePoint;
//....
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mousePoint = e.getPoint();
repaint();
}
});
Next we need to calculate the angle between these two points...
if (mousePoint != null) {
// This represents the anchor point, in this case,
// the centre of the component...
int x = width / 2;
int y = height / 2;
// This is the difference between the anchor point
// and the mouse. Its important that this is done
// within the local coordinate space of the component,
// this means either the MouseMotionListener needs to
// be registered to the component itself (preferably)
// or the mouse coordinates need to be converted into
// local coordinate space
int deltaX = mousePoint.x - x;
int deltaY = mousePoint.y - y;
// Calculate the angle...
// This is our "0" or start angle..
rotation = -Math.atan2(deltaX, deltaY);
rotation = Math.toDegrees(rotation) + 180;
}
From here, you would need to subtract 90 degrees, which would give your arcs start angle and then use an extent of 180 degrees.
这篇关于使用鼠标在Java中设置弧的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!