我试图创建一个弧,但是当我使用相同的宽度和高度参数时,弧几乎是平的,不应该更高?我原以为高度看起来与屏幕上的宽度相同。
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawArc(0, 0, 100, 100, 45, 90);
}
答案 0 :(得分:2)
g2d.drawArc(0, 0, 100, 100, 45, 90, Arc2D.CHORD);
我原本预计高度看起来与屏幕上的宽度相同。
这是因为你只绘制了90度的弧度。将值更改为180,宽度/高度将不同。即使你画180,宽度/高度也不会相同。宽度/高度仅对360度弧度相同。
另外,您使用的API是什么? Graphics.drawArc(...)仅支持6个参数,而不是7个。
当您提出问题时发布适当的SSCCE,以便我们可以看到该行为。
答案 1 :(得分:0)
我也对drawArc感到沮丧。 Here's some code I wrote改善"
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;
}