2014-11-24 40 views
0

我目前正在研究类中的一个程序,但被卡在一个点,我必须使用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 

回答

0

的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中的点绘制一条线。最后,一定要关闭路径。

找出绘制点的正确顺序,你应该能够弄清楚如何迭代点。

相关问题