Draw a triangle with a rounded corner

I would like to draw a triangle whose vertices are slightly anti-aliased in Java Swing. I learned how to draw a triangle with these lines of code

Polygon p=new Polygon (vertice_x, vertices_y, number_of_vertices);
g.drawPolygon(p);

      

but I didn't find anything about rounded corners. I read that Graphics2D has a method that allows you to draw a rounded rectangle, but for a general polygon? How should I do it?

+3


source to share


2 answers


For more control over the rounded corners (besides using Stroke ), you can combine 3 lines for the sides and 3 bezier curves for the rounded corners. Use linear interpolation to get the start / end point of lines and curves, with the corner points being the control point for the curves.

public Point interpolate(Point p1, Point p2, double t){
    return new Point((int)Math.round(p1.x * (1-t) + p2.x*t), 
            (int)Math.round(p1.y * (1-t) + p2.y*t));
}

Point p1 = new Point(50,10);
Point p2 = new Point(10,100);
Point p3 = new Point(100,100);

Point p1p2a = interpolate(p1, p2, 0.2);
Point p1p2b = interpolate(p1, p2, 0.8);

Point p2p3a = interpolate(p2, p3, 0.2);
Point p2p3b = interpolate(p2, p3, 0.8);

Point p3p1a = interpolate(p3, p1, 0.2);
Point p3p1b = interpolate(p3, p1, 0.8);
...

g.drawLine(p1p2a.x, p1p2a.y, p1p2b.x, p1p2b.y);
g.drawLine(p2p3a.x, p2p3a.y, p2p3b.x, p2p3b.y);
g.drawLine(p3p1a.x, p3p1a.y, p3p1b.x, p3p1b.y);
QuadCurve2D c1 = new QuadCurve2D.Double(p1p2b.x, p1p2b.y, p2.x, p2.y, p2p3a.x, p2p3a.y);
QuadCurve2D c2 = new QuadCurve2D.Double(p2p3b.x, p2p3b.y, p3.x, p3.y, p3p1a.x, p3p1a.y);
QuadCurve2D c3 = new QuadCurve2D.Double(p3p1b.x, p3p1b.y, p1.x, p1.y, p1p2a.x, p1p2a.y);
g.draw(c1);
g.draw(c2);
g.draw(c3);

      

In the above code, you can adjust the parameter t

passed to interpolate

to change the rounded corners.



You can add all of this to Path2D as well . Path2D

implements an interface Shape

that, among other things, allows you to pass an object in Graphics2D.fill

to fill out a form

Path2D path = new Path2D.Double();
AffineTransform at = new AffineTransform();
path.moveTo(p1p2a.x, p1p2a.y);
path.lineTo(p1p2b.x, p1p2b.y);
path.append(c1.getPathIterator(at), true);
path.lineTo(p2p3b.x, p2p3b.y);
path.append(c2.getPathIterator(at), true);
path.lineTo(p3p1b.x, p3p1b.y);
path.append(c3.getPathIterator(at), true);
path.closePath();
g.fill(path);

      

+5


source


If you want a relatively small amount of rounding, check out Stroke and BasicStroke , which lets you traverse the corners of any polygon. If you want a very rounded triangle, you will need to build a line shape for yourself. Draw circular arcs instead of corners, or use splines to create a shape.



Here is a tutorial for strokes .

+3


source







All Articles