java的几何图像支持并不是很好,java后面提供的2D、3D我试试了下,感觉效果不好,立体感不强,我没有仔细研究了,不知道到底能不能画出这样的几何图形来,于是我就自己查了下API,查到这个方法drawPolygon(int[] xPoints,int[] yPoints,int nPoints),这个方法可以画出任意的多行,于是我就用这个方法配合其他的画图方法写了个立体长方体的demo。
这个方法API上的解释:
public abstract void drawPolygon(int[]xPoints, int[]yPoints, intnPoints)
- 绘制一个由 x 和 y 坐标数组定义的闭合多边形。每对 ( x, y) 坐标定义了一个点。
- 此方法绘制由 nPoint个线段定义的多边形,其中前面的
-
nPoint - 1个线段是当 1 ≤ i ≤ nPoints时,从
-
(xPoints[i - 1], yPoints[i - 1])到 (xPoints[i], yPoints[i])的线段。
-
如果最后一个点和第一个点不同,则图形会通过在这两点间绘制一条线段来自动闭合。
-
-
参数:
-
xPoints
-
x
坐标数组。
-
yPoints
-
y
坐标数组。
-
nPoints
- 点的总数。
下面是我的demo程序:
-
import java.awt.Color; import java.awt.Graphics; /** * * @author lazy_p * @date 2010-3-18 * * 立体图形demo * */ public class ThreeDrec extends java.applet.Applet { private static final long serialVersionUID = -2427989261965982016L; public void init() { resize(200, 200); } // 初 始 化 作 图 区域 public void paint(Graphics g) { g.setColor(Color.red); g.drawPolygon(new int[] { 30, 90, 100, 40 }, new int[] { 30, 30, 10, 10 }, 4); // 画 一个长方形 g.setColor(new Color(1).GRAY); g.fillPolygon(new int[] { 30, 90, 100, 40 }, new int[] { 30, 30, 10, 10 }, 4);// 填充 一个长方形 g.setColor(Color.red); g.drawRect(30, 30, 60, 100); g.setColor(new Color(1).CYAN); g.fillRect(30, 30, 60, 100); g.setColor(Color.red); g.drawPolygon(new int[] { 90, 100, 100, 90 }, new int[] { 30, 10, 110, 130 }, 4); g.setColor(new Color(1).green); g.fillPolygon(new int[] { 90, 100, 100, 90 }, new int[] { 30, 10, 110, 130 }, 4); } }
-
-
效果如图: