我开发了一个绘制多边形三角形的程序。三角形是使用鼠标拖动绘制的。三角形的坐标存储在数组列表中。每次鼠标光标停留在现有绘制的三角形上(在三角形区域内),鼠标光标都会变成"CROSSHAIR_CURSOR",但这并没有发生。帮助:-(
...
public class DrawingBoardWithMatrix extends JFrame {
public static void main(String[] args) {
new DrawingBoardWithMatrix();
}
public DrawingBoardWithMatrix(){
this.add(new PaintSurface(), BorderLayout.CENTER);
...
}
private class PaintSurface extends JComponent {
java.util.List<Polygon> triangles = new LinkedList<Polygon>();
Point startDrag, endDrag, midPoint;
Polygon triangle;
public PaintSurface() {
...
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}//end mousePressed
public void mouseReleased(MouseEvent e) {
if (startDrag.x > endDrag.x)
midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
else
midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y };
triangles.add( new Polygon(xs, ys, 3));
startDrag = null;
endDrag = null;
repaint();
}//end mouseReleased
});//end addMouseListener
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}//end mouseDragged
});//end this.addMouseMotionListener
}//end paintSurface
//THIS CODE DOESNT WORK - AND I AM STUCK :-(
public void mouseMoved(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
if (triangles.contains(startDrag))
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
else
setCursor(Cursor.getDefaultCursor());
}// end mouseMoved
private void paintBackground(Graphics2D g2){
...
}
public void paint(Graphics g) {
...
}
}//end private class PaintSurface
}//end public class DrawingBoardMatrix
发布于 2009-09-27 19:15:55
您看到mouseMoved
方法被调用了吗?按照这种编写方式,mouseMoved方法是PaintSurface的成员,但PaintSurface不是MouseMotionListener。实现'MouseMotionListener‘将强制它实现mouseMoved
和mouseDragged
。完成此操作后,您可以将PaintSurface作为MouseMotionListener
添加到自身。或者,也可以将mouseMoved
方法移动到已经定义的MouseMotionAdapter
匿名类中:
//paintSurface constructor
....
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}//end mouseDragged
//TRY THIS CODE :-)
public void mouseMoved(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
if (triangles.contains(startDrag))
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
else
setCursor(Cursor.getDefaultCursor());
}// end mouseMoved
});//end this.addMouseMotionListener
}//end paintSurface
编辑(回应您的评论):
看起来您的条件if (triangles.contains(startDrag))
依赖于List<Polygon>
找到一个认为自己等于传入的Point
的Point
。据我所知,通过查看Polygon
中的代码(它没有覆盖equals
方法,因此它采用Object
的实现),您将无法“成功”执行此测试。您将需要遍历triangles
集合中的Polygon
,并依次对它们执行contains
操作。
编辑2:
你可能想得有点过头了。为了实现“遍历triangles
集合中的Polygon
”这个建议……您可以执行类似以下操作:
public void mouseMoved(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
Cursor cursor = Cursor.getDefaultCursor();
//you have a List<Polygon>, so you can use this enhanced for loop
for (Polygon p : triangles) {
if (p.contains(startDrag)) {//Polygon has a 'contains(Point)' method
cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
break; //you've found a hit, break from the loop
}
}
setCursor(cursor);
}// end mouseMoved
您还可以考虑不在每次移动鼠标时都设置光标。为此,您可以进行一个测试,以检查当前光标的类型和您的鼠标移动要设置的光标类型,并仅在发生更改时设置它:
if (cursor.getType() != getCursor().getType()) {
setCursor(cursor);
}
https://stackoverflow.com/questions/1484168
复制相似问题