Graphics2D.drawArc 中的高度参数
Height parameter in Graphics2D.drawArc
我正在尝试创建一个圆弧,但是当我使用相同的宽度和高度参数时,圆弧几乎是平的,不是应该更高吗?我本以为高度看起来与屏幕上的宽度长度相同。
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawArc(0, 0, 100, 100, 45, 90);
}
我也对 drawArc 感到沮丧。 Here's some code I wrote 至 "improve"
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;
}
g2d.drawArc(0, 0, 100, 100, 45, 90, Arc2D.CHORD);
I would have expected that the height would appear to be the same length as the width on the screen.
因为你只画了一个90度的圆弧。将值更改为 180,width/height 将有所不同。即使你绘制 180,width/height 也不会相同。 width/height 仅对 360 度弧相同。
另外,API你用的是什么? Graphics.drawArc(...) 只支持 6 个参数,不支持 7 个。
Post 当您提问时适当的 SSCCE 以便我们可以看到行为。
我正在尝试创建一个圆弧,但是当我使用相同的宽度和高度参数时,圆弧几乎是平的,不是应该更高吗?我本以为高度看起来与屏幕上的宽度长度相同。
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawArc(0, 0, 100, 100, 45, 90);
}
我也对 drawArc 感到沮丧。 Here's some code I wrote 至 "improve"
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;
}
g2d.drawArc(0, 0, 100, 100, 45, 90, Arc2D.CHORD);
I would have expected that the height would appear to be the same length as the width on the screen.
因为你只画了一个90度的圆弧。将值更改为 180,width/height 将有所不同。即使你绘制 180,width/height 也不会相同。 width/height 仅对 360 度弧相同。
另外,API你用的是什么? Graphics.drawArc(...) 只支持 6 个参数,不支持 7 个。
Post 当您提问时适当的 SSCCE 以便我们可以看到行为。