三角形绘制方法 [英] Triangle Draw Method
问题描述
我有麻烦绘制在Java中的 Draw(绘图G)
方法的三角形。
我可以画一个矩形,像这样:
I have trouble drawing a triangle with the draw(Graphics g)
method in Java.
I can draw a rectangle like so:
public void draw(Graphics g) {
g.setColor(colorFill);
g.fillRect(p.x, p.y, width, height);
g.setColor(colorBorder);
g.drawRect(p.x, p.y, width, height);
drawHandles(g);
其中p重新presents形状的左上角。我将如何绘制三角形以同样的方式?
Where p represents "the top left corner of the shapes". How would I draw the triangle in the same manner?
可能有人给我一个例子为一个标准的三角形吗?
Could someone give me an example for a standard triangle?
推荐答案
有没有既不图形也不Graphics2D的一个drawTriangle方法。你需要自己去做。您可以使用的drawLine
方法画三条线,或使用一个下列方法:
There is not a drawTriangle method neither in Graphics nor Graphics2D. You need to do it by yourself. You can draw three lines using the drawLine
method or use one these methods:
- drawPolygon(int[] xPoints,INT [] ypoints中,诠释nPoints)
- drawPolygon(多边形 P)
- drawPolyline(int[] xPoints,INT [] ypoints中,诠释nPoints)
- drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
- drawPolygon(Polygon p)
- drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
这些方法与多边形工作。您可以更改preFIX 画
到填写时要填写由点集定义的多边形
。我插入文档链接。看看,学习如何使用它们。
These methods work with polygons. You may change the prefix draw
to fill
when you want to fill the polygon defined by the point set. I inserted the documentation links. Take a look to learn how to use them.
有是的GeneralPath 类了。它可以与Graphics2D的,其能够绘制形状的使用。请看下图:
There is the GeneralPath class too. It can be used with Graphics2D, which is capable to draw Shapes. Take a look:
这篇关于三角形绘制方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!