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 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; }