java 画圆角三角形

pkmbmrz7  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(418)

我想画一个三角形,它的顶点在Java Swing中有点平滑。我用这些代码学会了如何画三角形

Polygon p=new Polygon (vertice_x, vertices_y, number_of_vertices);
g.drawPolygon(p);

但我没发现圆角的痕迹我读到Graphics2D中有一个方法可以让你画一个有圆角边框的矩形,但是对于一般的多边形?我该怎么做?

mrwjdhj3

mrwjdhj31#

为了更好地控制圆角(除了使用Stroke),您可以合并3条线作为边,并组合3条贝塞尔曲线作为圆角。使用线性插值来获得直线和曲线的起点/终点,其中角点是曲线的控制点。

public Point interpolate(Point p1, Point p2, double t){
    return new Point((int)Math.round(p1.x * (1-t) + p2.x*t), 
            (int)Math.round(p1.y * (1-t) + p2.y*t));
}

Point p1 = new Point(50,10);
Point p2 = new Point(10,100);
Point p3 = new Point(100,100);

Point p1p2a = interpolate(p1, p2, 0.2);
Point p1p2b = interpolate(p1, p2, 0.8);

Point p2p3a = interpolate(p2, p3, 0.2);
Point p2p3b = interpolate(p2, p3, 0.8);

Point p3p1a = interpolate(p3, p1, 0.2);
Point p3p1b = interpolate(p3, p1, 0.8);
...

g.drawLine(p1p2a.x, p1p2a.y, p1p2b.x, p1p2b.y);
g.drawLine(p2p3a.x, p2p3a.y, p2p3b.x, p2p3b.y);
g.drawLine(p3p1a.x, p3p1a.y, p3p1b.x, p3p1b.y);
QuadCurve2D c1 = new QuadCurve2D.Double(p1p2b.x, p1p2b.y, p2.x, p2.y, p2p3a.x, p2p3a.y);
QuadCurve2D c2 = new QuadCurve2D.Double(p2p3b.x, p2p3b.y, p3.x, p3.y, p3p1a.x, p3p1a.y);
QuadCurve2D c3 = new QuadCurve2D.Double(p3p1b.x, p3p1b.y, p1.x, p1.y, p1p2a.x, p1p2a.y);
g.draw(c1);
g.draw(c2);
g.draw(c3);

在上面的代码中,您可以调整传递给interpolatet参数,以更改圆角的大小。
您也可以将所有这些附加到Path2D中。Path2D实现了Shape接口,该接口允许将对象传递给Graphics2D.fill以填充Shape

Path2D path = new Path2D.Double();
AffineTransform at = new AffineTransform();
path.moveTo(p1p2a.x, p1p2a.y);
path.lineTo(p1p2b.x, p1p2b.y);
path.append(c1.getPathIterator(at), true);
path.lineTo(p2p3b.x, p2p3b.y);
path.append(c2.getPathIterator(at), true);
path.lineTo(p3p1b.x, p3p1b.y);
path.append(c3.getPathIterator(at), true);
path.closePath();
g.fill(path);
0mkxixxg

0mkxixxg2#

如果你想要一个相对较小的圆角,看看StrokeBasicStroke,它允许你圆角任何多边形的角。如果你想要一个非常圆的三角形,你必须为自己构建线条形状。绘制圆弧而不是角,或使用样条线创建形状。
这里是a tutorial for Strokes

pb3s4cty

pb3s4cty3#

protected Path createShapePath() {
    Path path = new Path();
    float cornerRadius = 10.0f; // Adjust the radius to control the roundness of the corners
    // Starting point at the bottom-left corner
    path.moveTo(shapeBox.left , shapeBox.bottom - cornerRadius );
    // Top point with rounded edge
    path.lineTo(shapeBox.centerX() - cornerRadius, shapeBox.top + cornerRadius);
    path.arcTo(new RectF(shapeBox.centerX() - cornerRadius, shapeBox.top, shapeBox.centerX() + cornerRadius, shapeBox.top + 2 * cornerRadius), 180, 180);
    // Bottom-right point with rounded edge
    path.lineTo(shapeBox.right, shapeBox.bottom - cornerRadius);
    path.arcTo(new RectF(shapeBox.right- 2 * cornerRadius , shapeBox.bottom - 2 * cornerRadius, shapeBox.right, shapeBox.bottom), 0, 90);
    // Bottom-left point with rounded edge
    path.lineTo(shapeBox.left + cornerRadius , shapeBox.bottom );
    path.arcTo(new RectF(shapeBox.left, shapeBox.bottom - 2 * cornerRadius, shapeBox.left + 2 * cornerRadius, shapeBox.bottom), 90, 90);
    // Close the path
    path.close();
    return path;
}

这里的“shapeBox”是java中的一个“F”,它有四个角值,你可以用圆角绘制你的Traingle。这很简单,对我很有效。[在这里输入图片描述][2]

相关问题