Results 1 to 5 of 5
Thread: drawing points with mouse
- 03-02-2009, 06:54 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
drawing points with mouse
I need to be able to draw a point with the mouse every time a user clicks in the applet window. It needs to display the Point X (1, 2, 3) and the coordinates. I can't figure how to how to do that. Here is my code, it is currently setup to draw 2 points and connect a line between them. It displays all the information, I just can't figure out how to do it an unlimited amount of times. Thanks
Java Code:import java.awt.*; import java.applet.*; import javax.swing.JFrame; import javax.swing.JApplet; import javax.swing.JLabel; import java.awt.BorderLayout; import javax.swing.JPanel; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseMotionAdapter; public class polygon extends JApplet { private int pointCount = 0; // count number of points // array of 2 java.awt.Point references private Point points[] = new Point[ 2 ]; private boolean p0Exists = false; private boolean p1Exists = false; private int r = 8; JLabel label1; JLabel label2; public void init() { JPanel p = new JPanel(); label1 = new JLabel("Click several times to create a polygon. Finish by clicking on Point 0."); label1.setBackground(Color.CYAN); label1.setOpaque(true); label1.setVisible(true); label1.setPreferredSize(new Dimension(400,20)); p.setLayout(new FlowLayout()); p.add(label1); Container content = getContentPane(); content.setLayout(new BorderLayout()); // Used to center the panel content.add(p, BorderLayout.SOUTH); PaintPanel paintPanel = new PaintPanel(); content.add(paintPanel, BorderLayout.CENTER); } public class PaintPanel extends JPanel { // set up GUI and register mouse event handler public PaintPanel() { super(); this.setBackground( Color.LIGHT_GRAY ); // handle frame mouse event addMouseListener( new MouseAdapter() // anonymous inner class { // define a point and repaint public void mousePressed( MouseEvent event ) { if ( !p0Exists ) { // create point0 points[ 0 ] = new Point( event.getX(), event.getY() ); System.out.println("Point 0 created: " + points[ 0 ] ); p0Exists = true; repaint(); } else if ( !p1Exists ) { // create point1 points[ 1 ] = new Point( event.getX(), event.getY() ); System.out.println("Point 1 created: " + points[ 1 ] ); p1Exists = true; repaint(); } } // end method mousePressed } // end anonymous inner class ); // end call to addMouseMotionListener } // end PaintPanel constructor public void paint( Graphics g ) { super.paint( g ); // clears drawing area if ( p0Exists ) { g.drawOval( points[ 0 ].x - r/2, points[ 0 ].y - r/2, r, r ); g.drawString("Point0", points[ 0 ].x, points[ 0 ].y - 3*r ); g.drawString("x=" + points[ 0 ].x + " y=" + points[ 0 ].y, points[ 0 ].x, points[ 0 ].y - r ); //g.drawLine(points[ 0 ].x - r/2, points[ 0 ].y - r/2, r, r); } if ( p1Exists ) { g.drawOval( points[ 1 ].x - r/2, points[ 1 ].y - r/2, r, r ); g.drawString("Point1", points[ 1 ].x, points[ 1 ].y - 3*r ); g.drawString("x=" + points[ 1 ].x + " y=" + points[ 1 ].y, points[ 1 ].x, points[ 1 ].y - r ); } if ( p0Exists && p1Exists) { g.drawLine(points[ 0 ].x, points[ 0 ].y, points[ 1 ].x, points[ 1 ].y); } } // end method paint } }
- 03-02-2009, 11:32 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The code you posted is similar (down to the r=8) to code on teh internet and in D&D etc. Do you understand it? I don't mean to be rude, but if you don't then that's where to start: by figuring out how this code works. Then you can start writing your own.
You want to draw a polygon, so why do you have
which initialises a points array capable of describing at most two points?Java Code:private Point points[] = new Point[ 2 ];
- 03-02-2009, 11:45 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
Yes, I understand it. I know what it does. The points array has since been removed as part of the requirements. I am using two arrays of 100 to get the x and y values. I just need to know how to create a point every time the mouse is pressed.
- 03-03-2009, 12:01 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The array thing was - of course - just an example. Your code (or at least the code you posted, as you say your code is now something else) also has the p0/1Exists variables which determine what the painting does.
Again having a pair of such variables might make sense for some other problem, but seems beside the point in drawing a polygon.
Anyway, I'm glad you understood the code you posted. The point is now to write your own and post questions about it if they arise.
- 03-03-2009, 12:39 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Drawing points on a JPanel
By josephdcoleman in forum New To JavaReplies: 6Last Post: 02-25-2009, 03:47 PM -
Percentage/decimal points?
By Exhonour in forum New To JavaReplies: 6Last Post: 01-16-2009, 10:35 PM -
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08-24-2008, 12:30 AM -
Demonstration of drawing points. It draws a sine wave
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:25 PM -
To get the points on the perimeter of a Rounded Rectangle
By rashibahal in forum New To JavaReplies: 6Last Post: 06-12-2008, 09:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks