public void mouseDragged(MouseEvent e) { int x = e.getX(), y = e.getY(), dx = lastX - x, dy = lastY - y; lastX = x; lastY = y; if (first == null) { Line line = addLine(point1, new Point(x, y)); if (line != null) { canvas.add(line.getP2()); first = line.getP2(); canvas.add(line); } } else { first.move(-dx, -dy); } repaint(); }
private FigureElement findFigureElement(int x, int y) { Point2D p = new Point2D.Float((float) x, (float) y); for (Iterator i = canvas.members(); i.hasNext(); ) { FigureElement fe = (FigureElement) i.next(); if (fe.contains(p)) return fe; } return null; }
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setPaint(BACKGROUND); g2.fill( new Rectangle2D.Float( 0f, 0f, (float) g2.getClipBounds().width, (float) g2.getClipBounds().height)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); canvas.paint(g2); }
private Line addLine(Point p1, Point p2) { if (Math.abs(p1.getX() - p2.getX()) < 5 || Math.abs(p1.getY() - p2.getY()) < 5) { return null; } Line line = null; if (p1 != null && p2 != null) { line = new Line(p1, p2); canvas.add(line); } repaint(); return line; }
private Point addPoint(int x, int y) { Point p = new Point(x, y); canvas.add(p); repaint(); return p; }