views:

51

answers:

2

What I am looking to do is to draw an incomplete polygon using Java. I have figured out how to draw only the polygon in one shot, or even fill the polygon. I can also draw the incomplete polygon by using line segments but the problem with this is the BasicStroke.JOIN_BEVEL does not apply to line segments. Here is how I do it with the line segments:

//polygon is not Java's Polygon, my own implementation, and the methods do as
//they imply
for(int i = 0; i < polygon.getNumberOfPoints(); i++){
    Point2D.Double first = polygon.getPoint(i);
    Point2D.Double second = new Point2D.Double();
    if(polygon.getPoint(i+1) != null){
        second = polygon.getPoint(i+1);
        trans1 = /* some graphic translation of first */
        trans2 = /* some graphic translation of second */
        g.setColor(polygon.getColor());
        g.setStroke(new BasicStroke(polygon.getWeight(), BasicStroke.JOIN_BEVEL, BasicStroke.CAP_BUTT));
        g.draw(new Line2D.Double(trans1[0], trans1[1], trans2[0], trans2[1]));
    }
}

this works just fine, but it does not work exactly how I would like it to. The g.setStroke(/*stuff here*/); has no effect on the joints.

A: 

Create a Path2D.Double, but just don't call closePath().

Path2D.Double path = new Path2D.Double();
for (int i = 0; i < polygon.getNumberOfPoints(); i++) {
  Point2D.Double point = polygon.getPoint(i);
  trans1 = /* some graphic translation */;
  if (i == 0)
    path.moveTo(trans1[0], trans1[1]);
  else
    path.lineTo(trans1[0], trans2[0]);
}
g.setColor(polygon.getColor());
g.setStroke(new BasicStroke(polygon.getWeight(), BasicStroke.JOIN_BEVEL, BasicStroke.CAP_BUTT));
g.draw(path);
Erick Robertson
I tried using Path2D.Double, but the problem with that is the last end of the of polygon will follow my mouse from the last place i clicked, and even using the Path2D.Double it seemed to react the same way as drawing the polygon with the first and last points not equal, java will draw a ling between them anyway. I will try again and see if I over looked something.
heater
+1  A: 

Well I completely missed a method.

g.drawPolyline(int[] xCoords, int[] yCoords, int numPoints)

This solved my problem.

heater