Results 1 to 2 of 2
- 09-27-2009, 10:05 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
change the mouse cursor - crosshair cursor
I developed a program to draw polygon triangles. The triangles were drawn using mouse drag. The coordinate of the triangles were stored in array list. Every times the mouse cursor, mouse over on the existing drawn triangles(within the area of triangles), the mouse cursor should turns to "CROSSHAIR_CURSOR", however this was not happened. Help :-(
Java Code:... 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 //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 });//end this.addMouseMotionListener }//end paintSurface private void paintBackground(Graphics2D g2){ ... } public void paint(Graphics g) { ... } }//end private class PaintSurface }//end public class DrawingBoardMatrix
- 09-28-2009, 01:57 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
Never mind, the problem has been solved.
Java Code: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); }
Similar Threads
-
Need to change cursor to hourglass at server side
By bgupta in forum Java ServletReplies: 0Last Post: 02-16-2009, 07:35 AM -
Cursor Size
By ScottVal in forum AWT / SwingReplies: 1Last Post: 01-03-2009, 06:11 AM -
Cursor
By serfster in forum New To JavaReplies: 2Last Post: 06-15-2008, 05:49 AM -
GUI cursor change problem
By ludragon in forum Advanced JavaReplies: 2Last Post: 01-09-2008, 10:03 PM -
Using a cursor
By notnumber6 in forum New To JavaReplies: 3Last Post: 11-26-2007, 10:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks