Dragged mouse coordinates are also counted while using mouseClicked()?

xorn

New Member
Something very weird is happening when I use the method :\[code\]public void mouseClicked(MouseEvent evt)\[/code\]I count the number of clicks , and each time that I have one click or two clicks , I grab the (x,y) coordinate and add that coordinate to my list . But when I check the given picture , I see that the added coordinates are also dragged coordinates , i.e. , locations where the user has passed with the mouse , but didn't hit a click or double click . How can that be ? This questions is associated with my previous questions regarding polygons , and this problems seems to be the cause of my problems . Here are the pictures : Closing a polygon :
SPQRt.gif
and when I start to draw something else :
lBvzp.gif
Meaning is , that's the same polygon , only this time the coordinates where the mouse traveled but DIDN'T hit a click / double click , were also counted . The code : \[code\]import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Point;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.ArrayList;import java.util.Iterator;import javax.swing.JFrame;import javax.swing.JPanel;/** * * @author X2 * */public class PolygonnerJframe{ public static void main (String[] args) { JFrame frame = new JFrame("Draw polygons"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new DrawingPanel()); frame.pack(); frame.setVisible(true); }}/** * Main class * @author X2 * */class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener{ /** * */ private static final long serialVersionUID = 1L; private static final Dimension MIN_DIM = new Dimension(300, 300); private static final Dimension PREF_DIM = new Dimension(500, 500); private boolean polygonDone = false; private final Point trackPoint = new Point(); // The 'dummy' point tracking the mouse private ArrayList<Point> points = new ArrayList<Point>(); // The list of points making up a polygon private ArrayList<Edges> edges = new ArrayList<Edges>(); // holds edges of current polygon private ArrayList<Polygon> polygons = new ArrayList<Polygon>(); // all the polygons /** * Setting the dimensions of the window */ public Dimension getMinimumSize() { return MIN_DIM; } public Dimension getPreferredSize() { return PREF_DIM; } /** * The only constructor needed for this class */ DrawingPanel() { super(); addMouseListener(this); addMouseMotionListener(this); } /** * The drawing itself */ public void paintComponent(Graphics g) { super.paintComponent(g); if (this.polygons.size() > 0) this.drawPreviousPolygons(g); // draw previous polygons int numPoints = points.size(); if (numPoints == 0) return; // nothing to draw Point prevPoint = points.get(0); // draw polygon Iterator<Point> it = points.iterator(); while (it.hasNext()) { Point curPoint = it.next(); draw(g, prevPoint, curPoint); prevPoint = curPoint; } // now draw tracking line or complete the polygon if (polygonDone == true) { Point point0 = points.get(0); // grab the starting point (x,y) int x1 = prevPoint.x; // grab the last point int y1 = prevPoint.y; int x2 = point0.x; int y2 = point0.y; // add another edge between the starting point and the last point Edges currentEdge = new Edges(new Point(x1,y1),new Point(x2,y2)); // put it in the list this.edges.add(currentEdge); // draw the last edge between the starting point & the last point draw(g, prevPoint, points.get(0)); printPoly(); } else // polygonDone == false draw(g, prevPoint, trackPoint); } public void printPoly() { int i = 0; while (i < this.polygons.size()) { System.out.println("Polygon number " + i); Polygon p = this.polygons.get(i); int j = 0; ArrayList<Edges> elist = p.getPolygonEdges(); System.out.println("List of edges size is :" + elist.size()); while (j < elist.size()) { Edges e = elist.get(j); System.out.println("the points are: " + e.getX1() + "," + e.getY1() + "," + e.getX2() + "," + e.getY2() ); j++; } i++; } } /** * MouseListener interface */ public void mouseClicked(MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); switch (evt.getClickCount()) { case 1: // single-click if (polygonDone == true) { // remove all the entries from the edges list - preparing for the next polygon // create the new polygon structure with the edges Polygon poly = new Polygon(this.edges); // add the polygon to the polygons array this.polygons.add(poly); edges.clear(); points.clear(); // ?? polygonDone = false; } points.add(new Point(x, y)); repaint(); break; case 2: // double-click polygonDone = true; points.add(new Point(x, y)); repaint(); break; default: // ignore anything else break; } } /** * MouseMotionListener interface */ public void mouseMoved(MouseEvent evt) { trackPoint.x = evt.getX(); trackPoint.y = evt.getY(); repaint(); } /** * draw points and lines * @param g * @param p1 * @param p2 */ private void draw(Graphics g, Point p1, Point p2) { int x1 = p1.x; int y1 = p1.y; int x2 = p2.x; int y2 = p2.y; // draw the line first so that the points appear on top of the line ends, not below g.setColor(Color.green.darker()); g.drawLine(x1 + 3, y1 + 3, x2 + 3, y2 + 3); g.drawLine(x1 + 4, y1 + 4, x2 + 4, y2 + 4); g.drawLine(x1 + 5, y1 + 5, x2 + 5, y2 + 5); g.drawLine(x1 + 6, y1 + 6, x2 + 6, y2 + 6); // create the current edge between the two points P1 and P2 Edges currentEdge = new Edges(new Point(x1,y1),new Point(x2,y2)); // add the edge to the edges list this.edges.add(currentEdge); // now just paint the edge between those two points g.setColor(Color.green); g.fillOval(x1, y1, 8, 8); g.setColor(Color.black); g.fillOval(x2, y2, 8, 8); } /** * Run on the arrayList of the Polygons , and draw the previous polygons * that we already made * @param g */ private void drawPreviousPolygons(Graphics g) { int i = 0; while (i < this.polygons.size()) { Polygon currentPoly = polygons.get(i); // grab current polygon int j = 0; ArrayList<Edges> edges = currentPoly.getPolygonEdges(); // draw the edges of the polygon while (j < edges.size()) // run on all the edges of the polygon { Edges edgeCurrent = edges.get(j); // grab current edge // drawing the edge // now draw it - grab the two points that create the edge int x1 = edgeCurrent.getX1(); int y1 = edgeCurrent.getY1(); int x2 = edgeCurrent.getX2(); int y2 = edgeCurrent.getY2(); // draw the line first so that the points appear on top of the line ends, not below g.setColor(Color.green.darker()); g.drawLine(x1 + 3, y1 + 3, x2 + 3, y2 + 3); g.drawLine(x1 + 4, y1 + 4, x2 + 4, y2 + 4); g.drawLine(x1 + 5, y1 + 5, x2 + 5, y2 + 5); g.drawLine(x1 + 6, y1 + 6, x2 + 6, y2 + 6); // now just paint the edge between those two points g.setColor(Color.green); g.fillOval(x1, y1, 8, 8); g.setColor(Color.black); g.fillOval(x2, y2, 8, 8); // proceed to next edge j++; } i++; // next polygon } } public void mouseDragged(MouseEvent evt) { /* EMPTY */ } public void mousePressed(MouseEvent evt) { /* EMPTY */ } public void mouseReleased(MouseEvent evt) { /* EMPTY */ } public void mouseEntered(MouseEvent evt) { /* EMPTY */ } public void mouseExited(MouseEvent evt) { /* EMPTY */ }}\[/code\]And that's my previous question .Any idea would to the source of the problem would be greatly appreciated . P.S. Classes \[code\]Edges\[/code\] and \[code\]Polygon\[/code\] are in the given link (if you need them , they are small classes) .Thanks
 
Back
Top