我有一个用GeneralPath制作的多边形,我做了一个基本的笔触,线条粗细为8,并将连接类型设置为JOIN_MITER。当我尝试绘制这样的形状时:
g2d.setStroke(stroke);
g2d.draw(generalPath);
它使用正确的线条粗细绘制形状,但它会像处理自己的线条一样处理每条线条,给出它在笔划中定义的任何类型的端盖。 CAP_ROUND看起来最好,但我更喜欢if行与JOIN_MITER正确连接。可以这样做吗?我使用GeneralPath时是否使用了正确的类?任何帮助或建议将不胜感激。
对于上下文,这里的类被剥离到相关信息。 paintComponent方法是从我正在绘制的JPanel的paintComponent方法调用的,它有一个这样的arrayList,它迭代通过并一次调用它们的paintComponent方法:
public class RShape extends RComponent{
GeneralPath linkedLines;
Stroke stroke;
public RShape(int x, int y, int sides, Stroke stroke, int defaultLineLength) {
this.stroke = stroke;
linkedLines = new GeneralPath();
double angle = ((double)360)/sides;//find what the angles would need to be to make a shape with that many sides
double dStartX = x;
double dStartY = y;
double nextAngle;
for(int i=0;i<sides;i++) {
nextAngle = angle*i;
nextAngle = nextAngle * Math.PI / 180;//convert to radians
double dEndX = dStartX + defaultLineLength * Math.sin(nextAngle);//find where the end of the line should be for the given angle and line length
double dEndY = dStartY + defaultLineLength * Math.cos(nextAngle);
int endX = (int) Math.round(dEndX);//round to the nearest int
int endY = (int) Math.round(dEndY);
int startX = (int) Math.round(dStartX);
int startY = (int) Math.round(dStartY);
linkedLines.moveTo(startX, startY);//add the next segment of the GeneralPath
linkedLines.lineTo(endX, endY);
dStartX = dEndX;//move the starting point to the end point so it's ready for the next loop
dStartY = dEndY;
}
linkedLines.closePath();//close the last gap.
}
public void paintComponent(Graphics2D g2d) {
g2d.setStroke(stroke);
g2d.draw(linkedLines);
}
}
这就是这样的:
ComponentList components = new ArrayList<RComponent>();
components.add(new RShape(50,50,5,new BasicStroke(8,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER),60));