[英]Creating a cube using arrays and for loops
我目前正在課堂上研究程序,但被困在必須使用for循環繪制立方體線條的地方。 有人可以幫我一下嗎? 我一直在網上尋求幫助,但無法使用FOR循環獲得有關此程序的幫助。
原始問題:編寫一個繪制多維數據集的應用程序。 使用GeneralPath類和Graphics2D類的方法繪制。
到目前為止,這是我的缺點:
import java.awt.Color;
import java.awt.geom.GeneralPath;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class CubeJPanel extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// base one: coordinates for front of the cube, point 0, 1, 2, 3, 4
int base1X[] = { 100, 100, 200, 200, 100 };
int base1Y[] = { 100, 200, 200, 100, 100 };
// base two: coordinates for back of the cube, point 0, 1, 2, 3, 4
int base2X[] = { 75, 75, 175, 175, 75 };
int base2Y[] = { 75, 175, 175 ,75, 75 };
Graphics2D g2d = ( Graphics2D ) g;
g2d.setColor( Color.red );
GeneralPath cube = new GeneralPath();
// this is where i'm having trouble. I know i'm suppose to for loop and arrays to draw out the lines of the cube.
g2d.draw( cube );
} // end method paintComponent
} // end class CubeJPanel
base1X,base1Y是沿着繪圖區域的x坐標,其中(0,0)是面板的左上角。 要使用GeneralPath,您需要執行以下操作:
GeneralPath cube = new GeneralPath();
cube.moveTo(base1x[0], base1y[0]);
for(int i=1; i<base1x.length(); i++)
{
cube.lineTo(base1x[i], base1y[i]);
}
cube.closePath();
上面的代碼段將繪制一個正方形,這些正方形是base1中的所有點。 基本上將GeneralPath視為點點滴滴。 您首先必須將路徑移動到起始位置。 moveTo將抽屜移動到該點,而不畫一條線。 lineTo將從當前點到moveTo中的點畫一條線。 最后,請務必關閉路徑。
找出繪制點的正確順序,您應該能夠弄清楚如何遍歷這些點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.