我尝试使用Graphics.drawPolygon()
方法
正确绘制三角形,但如何计算边界的3个点?
我已经用圆圈做过,但我似乎无法找到三角形的解决方案。
教师要求不能使用Graphics2D
。
我的代码:
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);
}
修改 预期结果
答案 0 :(得分:1)
使用Graphics
方法drawPolygon()
渲染轮廓,fillPolygon()
填充其内部;两者都有所需的签名,如here所示。
因为"绘制图形轮廓的操作通过遍历像素大小的笔在像素之间的无限细路径来操作,"将图形上下文转换为Graphics2D
,以便您可以在相应的draw()
上使用fill()
和Shape
。这样您就可以使用setStroke()
指定大纲,图示为here。
image2 http://i52.tinypic.com/ndo51u.png
我需要它来定制厚度......我也不想使用
Graphics2D
。
Graphics
API支持自定义厚度 。根据建议here,paintComponent()
收到的实际图形上下文是Graphics2D
的一个实例, 支持自定义笔触几何体。
事情是老师的天堂教会我
Graphics2D
,所以我不应该使用它。
然后简单地绘制较大的三角形,然后绘制较小的三角形。如果这不起作用,那么在计算较大的三角形时会出错,您应该编辑问题以包含complete example。