如何在上一个下绘制下图?现在它结束了...它将是漏斗,所以我希望每个下一个椭圆都在多边形和弧形下。 我有这样的代码:
for (int i = 0; i < pdLen; i++) {
....
....
g2.fillPolygon(poly);
g2.fillOval(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight);
g2.fillArc(botLeft[0], botLeft[1] - arcHeight / 2, botRight[0] - botLeft[0], arcHeight, 0, -180);
}
由于 PS:跟随坐标是从之前计算的,所以为(int i = pdLen-1; i> 0; i - ){不起作用
UPD:
每个循环步骤计算topLeft和topRight。
int[] topLeft = {(int)Math.round( startX + ( procSum/onePixX ) ), (int)Math.round( startY + ( procSum/onePixY ) )};
int[] topRight = {(int)Math.round( topRightX - ( procSum/onePixX ) ), (int)Math.round( startY + ( procSum/onePixY ) )};
procSum += (pieceData[i] * pieceProc);
int[] botLeft = {(int)Math.round( startX + ( procSum/onePixX ) ), (int)Math.round( startY + ( procSum/onePixY ) )};
int[] botRight = {(int)Math.round( topRightX - ( procSum/onePixX ) ), (int)Math.round( startY + ( procSum/onePixY ) )};
procSum += padProc;
答案 0 :(得分:2)
你可以扭转循环。您可能需要考虑坐标的计算,但可以这样做。
一般情况下,在这种情况下,您应该发布MCVE。这将为每个人节省大量时间。
然而,便宜(即简单)的解决方案是存储必须涂漆的内容,然后以相反的顺序绘制这些形状:
List<Shape> ovals = new ArrayList<Shape>();
List<Shape> arcs = new ArrayList<Shape>();
for (int i = 0; i < pdLen; i++) {
//g2.fillOval(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight);
ovals.add(new Ellipse2D.Double(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight));
//g2.fillArc(botLeft[0], botLeft[1] - arcHeight / 2, botRight[0] - botLeft[0], arcHeight, 0, -180);
arcs.add(new Arc2D.Double(botLeft[0], botLeft[1] - arcHeight / 2, botRight[0] - botLeft[0], arcHeight, 0, -180));
}
for (int i=ovals.size()-1; i>=0; i--)
{
g2.fill(ovals.get(i));
g2.fill(arcs.get(i));
}