/** Standard override -- draws all the dots. */ public void paintComponent(Graphics g) { // As a JPanel subclass we need call super.paintComponent() // so JPanel will draw the white background for us. super.paintComponent(g); Color origColor = g.getColor(); for (Line l : lines) { if (l.isPencil()) { g.setColor(Color.RED); } else { g.setColor(Color.BLACK); } g.drawLine((int) l.getX1(), (int) l.getY1(), (int) l.getX2(), (int) l.getY2()); } g.setColor(origColor); // Go through all the dots, drawing a circle for each for (ManModel dotModel : dots) { // g.drawImage(img, dotModel.getX() - MAN_WIDTH/2, dotModel.getY() - MAN_HEIGHT/2, MAN_WIDTH, // MAN_HEIGHT, null); g.drawImage( img, dotModel.getX() - Constants.MAN_WIDTH / 2, dotModel.getY() - Constants.MAN_HEIGHT / 2, null); } numberMen.setText("" + dots.size()); // Draw the "requested" clip rect in red // (this just shows off smart-repaint) if (redPaint) { Rectangle clip = g.getClipBounds(); if (clip != null) { g.setColor(Color.red); g.drawRect(clip.x, clip.y, clip.width - 1, clip.height - 1); g.setColor(origColor); } } g.setColor(Color.RED); for (Line l : circles) { g.drawLine((int) l.getX1(), (int) l.getY1(), (int) l.getX2(), (int) l.getY2()); } for (Arc l : arcs) { g.drawArc( (int) l.getX1(), (int) l.getY1(), (int) l.getWidth(), (int) l.getHeight(), (int) l.getStartAngle(), (int) l.getSweep()); // g.drawArc((int) l.getX1(), (int) l.getY1(), (int) l.getWidth()+1, (int) l.getHeight()+1, // l.getStartAngle()-1, l.getSweep()); } g.setColor(Color.BLACK); }
public static boolean linesIntersect(Line line1, Line line2) { if (!(line1.equals(line2))) { double x1 = line1.getX1(); double y1 = line1.getY1(); double x2 = line1.getX2(); double y2 = line1.getY2(); double x3 = line2.getX1(); double y3 = line2.getY1(); double x4 = line2.getX2(); double y4 = line2.getY2(); return Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4); } else { return false; } }
public static String printLine(Line segment) { return String.format( "(%.1f, %.1f) to (%.1f, %.1f)", segment.getX1(), segment.getY1(), segment.getX2(), segment.getY2()); }