Java Draw Arc drawArc(Graphics2D g, int x, int y, int width, int height, double start, double end, int innerXOffset, int innerYOffset)

Here you can find the source of drawArc(Graphics2D g, int x, int y, int width, int height, double start, double end, int innerXOffset, int innerYOffset)

Description

draw Arc

License

Open Source License

Declaration

synchronized public static void drawArc(Graphics2D g, int x, int y, int width, int height, double start,
            double end, int innerXOffset, int innerYOffset) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.Shape;

import java.awt.geom.*;

public class Main {
    synchronized public static void drawArc(Graphics2D g, int x, int y, int width, int height, double start,
            double end, int innerXOffset, int innerYOffset) {
        Area a = createArc(x, y, width, height, start, end, innerXOffset, innerYOffset);
        g.draw(a);/*w  w w .j  av  a  2  s.  c  om*/
    }

    synchronized public static Area createArc(int x, int y, int width, int height, double start, double end,
            int innerXOffset, int innerYOffset) {
        Shape s = new Ellipse2D.Double(x, y, width, height);
        Area a = new Area(s);
        int center_x = x + width / 2;
        int center_y = y + height / 2;
        int xs[] = new int[6];
        int ys[] = new int[6];
        xs[0] = center_x;
        ys[0] = center_y;
        double middle = start + (end - start) / 2;
        double quarter1 = start + (middle - start) / 2; //new point in the polygon between start and middle
        double quarter2 = middle + (end - middle) / 2; //new point in the polygon between middle and end

        int pt1_x = (int) (center_x + width * Math.cos(start));
        int pt1_y = (int) (center_y + height * Math.sin(start));
        int pt2_x = (int) (center_x + width * Math.cos(end));
        int pt2_y = (int) (center_y + height * Math.sin(end));
        int mid_x = (int) (center_x + width * Math.cos(middle)); //now there is no need to *2 because with a polygon with 6 points the worst case (360 degrees) is guaranteed
        int mid_y = (int) (center_y + height * Math.sin(middle));
        int quar1_x = (int) (center_x + height * Math.cos(quarter1)); //calculates the x and y for the new points
        int quar1_y = (int) (center_y + height * Math.sin(quarter1));
        int quar2_x = (int) (center_x + height * Math.cos(quarter2));
        int quar2_y = (int) (center_y + height * Math.sin(quarter2));
        //inserts the new points in the polygon' array in the rigth order
        xs[1] = pt1_x;
        ys[1] = pt1_y;
        xs[2] = quar1_x;
        ys[2] = quar1_y;
        xs[3] = mid_x;
        ys[3] = mid_y;
        xs[4] = quar2_x;
        ys[4] = quar2_y;
        xs[5] = pt2_x;
        ys[5] = pt2_y;

        Polygon p = new Polygon(xs, ys, 6); // create the new polygon with the 6 points
        Area clip = new Area(p);
        a.intersect(clip);
        Ellipse2D.Double inner = new Ellipse2D.Double(x + innerXOffset, y + innerYOffset, width - innerXOffset * 2,
                height - innerYOffset * 2);
        a.subtract(new Area(inner));
        return a;
    }
}

Related

  1. drawArc(Graphics g, int left, int top, int width, int height, int startAngle, int deltaAngle, int lineWidth)
  2. drawCenteredArc(Graphics g, int x, int y, int r, int start, int dist)