如何绘制带有Java图形边框的三角形 [英] How to draw a triangle with border with Java Graphics

查看:121
本文介绍了如何绘制带有Java图形边框的三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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

由于它不能使用 Graphics2D



我的代码:

  if(xPoints!= null& amp ;& 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);
}

编辑
预期结果





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



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


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


自定义厚度不是支持 Graphics API。按照这里的建议,由 paintComponent()获得的实际图形上下文是 Graphics2D 的一个实例,它支持自定义笔划几何。


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




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


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

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

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.

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

解决方案

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

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.

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

I need it to have a custom thickness…I also don't want to use 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.

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

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.

这篇关于如何绘制带有Java图形边框的三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆