如何使用Java Graphics绘制带边框的三角形

时间:2023-02-04 17:24:12

I'm trying to draw a triangle with a border using the Graphics.drawPolygon() method

我正在尝试使用Graphics.drawPolygon()方法绘制带边框的三角形

The triangle is properly drawn, but how can I calculate the 3 points of the border?

正确绘制三角形,但如何计算边界的3个点?

I already did it with a circle, but I can't seem to find a solution for triangle.

我已经用圆圈做了,但我似乎无法找到三角形的解决方案。

A requirement of the instructor as that it cannot use Graphics2D.

教师的要求是它不能使用Graphics2D。

My code:

if (xPoints != null && yPoints != null) {
    int[] nXPoints = new int[] { xPoints[0] - borderThickness, xPoints[1] - borderThickness,
            xPoints[2] - borderThickness };
    int[] nYPoints = new int[] { yPoints[0] - borderThickness, yPoints[1] - borderThickness,
            yPoints[2] - borderThickness };

    g.setColor(borderColor);
    g.fillPolygon(nXPoints, nYPoints, 3);

    g.setColor(fillColor);
    g.fillPolygon(xPoints, yPoints, 3);
}

Edit: Expected result

编辑:预期结果

如何使用Java Graphics绘制带边框的三角形

1 个解决方案

#1


1  

Use the Graphics methods drawPolygon() to render the outline and fillPolygon() to fill its interior; both have the required signature, as shown here.

使用Graphics方法drawPolygon()渲染轮廓,使用fillPolygon()填充其内部;两者都有所需的签名,如此处所示。

如何使用Java Graphics绘制带边框的三角形

Because "operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen," cast the graphics context to Graphics2D so that you can use draw() and fill() on the corresponding Shape. This will allow you to specify the outline using setStroke(), illustrated here.

因为“绘制图形轮廓的操作通过遍历带有像素大小笔的像素之间的无限细路径来操作”,所以将图形上下文强制转换为Graphics2D,以便您可以在相应的Shape上使用draw()和fill()。这将允许您使用setStroke()指定轮廓,如此处所示。

image2 http://i52.tinypic.com/ndo51u.png

I need it to have a custom thickness…I also don't want to use Graphics2D.

我需要它有自定义厚度......我也不想使用Graphics2D。

Custom thickness is not supported in the Graphics API. As suggested here, the actual graphics context received by paintComponent() is an instance of Graphics2D, which does support custom stroke geometry.

Graphics API不支持自定义粗细。正如这里所建议的,paintComponent()接收的实际图形上下文是Graphics2D的一个实例,它支持自定义笔划几何体。

The things is teacher haven't taught me Graphics2D, so I'm not supposed to use it.

事情是老师没有教我Graphics2D,所以我不应该使用它。

Then simply paint the larger triangle and then the smaller. If this isn't working, then you have an error in you calculation of the larger triangle, and you should edit your question to include a complete example.

然后简单地绘制较大的三角形,然后绘制较小如果这不起作用,那么在计算较大的三角形时会出错,您应该编辑问题以包含完整的示例。

#1


1  

Use the Graphics methods drawPolygon() to render the outline and fillPolygon() to fill its interior; both have the required signature, as shown here.

使用Graphics方法drawPolygon()渲染轮廓,使用fillPolygon()填充其内部;两者都有所需的签名,如此处所示。

如何使用Java Graphics绘制带边框的三角形

Because "operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen," cast the graphics context to Graphics2D so that you can use draw() and fill() on the corresponding Shape. This will allow you to specify the outline using setStroke(), illustrated here.

因为“绘制图形轮廓的操作通过遍历带有像素大小笔的像素之间的无限细路径来操作”,所以将图形上下文强制转换为Graphics2D,以便您可以在相应的Shape上使用draw()和fill()。这将允许您使用setStroke()指定轮廓,如此处所示。

image2 http://i52.tinypic.com/ndo51u.png

I need it to have a custom thickness…I also don't want to use Graphics2D.

我需要它有自定义厚度......我也不想使用Graphics2D。

Custom thickness is not supported in the Graphics API. As suggested here, the actual graphics context received by paintComponent() is an instance of Graphics2D, which does support custom stroke geometry.

Graphics API不支持自定义粗细。正如这里所建议的,paintComponent()接收的实际图形上下文是Graphics2D的一个实例,它支持自定义笔划几何体。

The things is teacher haven't taught me Graphics2D, so I'm not supposed to use it.

事情是老师没有教我Graphics2D,所以我不应该使用它。

Then simply paint the larger triangle and then the smaller. If this isn't working, then you have an error in you calculation of the larger triangle, and you should edit your question to include a complete example.

然后简单地绘制较大的三角形,然后绘制较小如果这不起作用,那么在计算较大的三角形时会出错,您应该编辑问题以包含完整的示例。