Java Swing 2D Create a Rounded Corner Polygon

Incase you are wondering I couldn’t find a straightforward method. Thus I wrote the following functions. Given a set of connecting points or a polygon the following function will return a polygon with rounded corners.

public static GeneralPath getRoundedGeneralPath(Polygon polygon) {
   List<int[]> l = new ArrayList<int[]>();
   for(int i=0; i < polygon.npoints; i++){
      l.add(new int[]{polygon.xpoints[i],polygon.ypoints[i]});
   }
   return getRoundedGeneralPath(l);
}
 
public static GeneralPath getRoundedGeneralPath(List<int[]> l) {
   List<Point> list=new ArrayList<Point>();
   for (int[] point : l){
      list.add(new Point(point[0],point[1]));
   }
   return getRoundedGeneralPathFromPoints(list);
}
public static GeneralPath getRoundedGeneralPathFromPoints(List<Point> l) {
   l.add(l.get(0));
   l.add(l.get(1));
   GeneralPath p = new GeneralPath();
   p.moveTo(l.get(0).x,l.get(0).y);
   for(int pointIndex=1; pointIndex<l.size()-1;pointIndex++){
      Point p1=l.get(pointIndex-1);
      Point p2=l.get(pointIndex);
      Point p3=l.get(pointIndex+1);
      Point mPoint = calculatePoint(p1, p2);
      p.lineTo(mPoint.x, mPoint.y);
      mPoint = calculatePoint(p3, p2);
      p.curveTo(p2.x, p2.y, p2.x, p2.y, mPoint.x, mPoint.y);
   }
   return p;
}
private static Point calculatePoint(Point p1, Point p2) {
   float arcSize=10;
   double d1=math.sqrt(math.pow(p1.x-p2.x, 2)+math.pow(p1.y-p2.y, 2));
   double per=arcSize/d1;
   double d_x=(p1.x-p2.x)*per;
   double d_y=(p1.y-p2.y)*per;
   int xx=(int)(p2.x+d_x);
   int yy=(int)(p2.y+d_y);
   return new Point(xx,yy);
}
Example
   public static void main(String args[]) {
        JFrame f = new JFrame("Rounded Corner Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

                GradientPaint gp = new GradientPaint(0, 0,
                        getBackground().brighter().brighter(), 0, getHeight(),
                        getBackground().darker().darker());

                g2d.setPaint(gp);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                int[][] a={{50,0},{100,50},{50,100},{0,50}};
                GeneralPath p = getRoundedGeneralPath(Arrays.asList(a));
                g2d.setColor(Color.red);
                g2d.fill(p);
                super.paintComponent(grphcs);
            }
        };
        contentPane.setOpaque(false);
        f.setContentPane(contentPane);
        contentPane.add(new JLabel("test"));
        f.setSize(200, 200);
        f.setVisible(true);
    }

Have fun…

2 Responses to “Java Swing 2D Create a Rounded Corner Polygon”

  1. Saykho Says:

    Good job. Adding just a small correction:
    In getRoundedGeneralPathFromPoints(List l) there should be
    Point begin = calculatePoint(l.get(l.size()-1),l.get(0));
    p.moveTo(begin.x,begin.y);
    instead of
    p.moveTo(l.get(0).x,l.get(0).y);

    makes difference when drawing, instead of filling

  2. inosucre Says:

    need a continue of this code !

    public class TestPolygon {
    public static void main(String[] args) {
    Coord2D a = new Coord2D(9,2);
    Coord2D b = new Coord2D(19,2);
    Coord2D c = new Coord2D(9,20);
    Coord2D d = new Coord2D(4,23);
    Coord2D e = new Coord2D(4,99);
    Coord2D[] v = { a, b, c, d, e};
    // EQUIVALENT
    Polygon p1 = new Polygon(a, b, c, d, e);
    Polygon p2 = new Polygon(v);
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: