java的幾何映像支援並不是很好,java後面提供的2D、3D我試試了下,感覺效果不好,立體感不強,我沒有仔細研究了,不知道到底能不能畫出這樣的幾何圖形來,於是我就自己查了下API,查到這個方法drawPolygon(int[] xPoints,int[] yPoints,int nPoints),這個方法可以畫出任意的多行,於是我就用這個方法配合其他的畫圖方法寫了個立體長方體的demo。
這個方法API上的解釋:
public abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
-
繪製一個由
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;</p><p>import java.awt.Graphics;</p><p>/**<br /> *<br /> * @author lazy_p<br /> * @date 2010-3-18<br /> *<br /> * 立體圖形demo<br /> *<br /> */<br />public class ThreeDrec extends java.applet.Applet {</p><p> private static final long serialVersionUID = -2427989261965982016L;</p><p> public void init() {<br /> resize(200, 200);<br /> } // 初 始 化 作 圖 地區</p><p> public void paint(Graphics g) {</p><p> g.setColor(Color.red);<br /> g.drawPolygon(new int[] { 30, 90, 100, 40 },<br /> new int[] { 30, 30, 10, 10 }, 4); // 畫 一個長方形<br /> g.setColor(new Color(1).GRAY);</p><p> g.fillPolygon(new int[] { 30, 90, 100, 40 },<br /> new int[] { 30, 30, 10, 10 }, 4);// 填充 一個長方形<br /> g.setColor(Color.red);<br /> g.drawRect(30, 30, 60, 100);<br /> g.setColor(new Color(1).CYAN);<br /> g.fillRect(30, 30, 60, 100);<br /> g.setColor(Color.red);<br /> g.drawPolygon(new int[] { 90, 100, 100, 90 }, new int[] { 30, 10, 110,<br /> 130 }, 4);<br /> g.setColor(new Color(1).green);<br /> g.fillPolygon(new int[] { 90, 100, 100, 90 }, new int[] { 30, 10, 110,<br /> 130 }, 4);</p><p> }<br />}<br />
-
效果:
-