根据Swing中的用户输入绘制四面体

时间:2015-12-02 09:34:02

标签: java swing

我需要编写一个可以绘制四面体(具有四个三角形面的三维形状)的程序。  我们必须使用类的GeneralPath和类Graphics2D的方法绘制。如何做到这一点的想法,

1 个答案:

答案 0 :(得分:0)

我以前做过这样的类似程序,  我创建了两个调用baseXbaseY并设置的数组  坐标到它。然后添加两个for循环来制作角落。  这样你甚至可以绘制一个立方体

 import java.awt.*; 
  import java.awt.geom.*;  
  import java.awt.event.*; 
  import javax.swing.*; 
  public class Tetrahedron extends JFrame {
  // constructor 
  public Tetrahedron() 
  {super( "Tetrahedron" );
  setSize( 275, 150 );
  setVisible( true );  } 
  // draw tetrahedron 
  public void paint( Graphics g )
  {
  super.paint( g );     
  int baseX[] = { 110, 150, 50, 110 };
  int baseY[] = { 90, 130, 130, 90 };
  int x = 110, y = 40; 
  Graphics2D g2d = ( Graphics2D ) g;
  g2d.setColor( Color.red );
  GeneralPath tetrahedron = new GeneralPath();
  tetrahedron.moveTo( baseX[ 0 ], baseY[ 0 ] ); 
  for ( int i = 1; i < 4; i++ ) {
  tetrahedron.lineTo( x, y ); 
  tetrahedron.moveTo( baseX[ i - 1 ], baseY[ i - 1 ] );
  tetrahedron.lineTo( baseX[ i ], baseY[ i ] ); 
  }tetrahedron.closePath(); g2d.draw( tetrahedron ); 
  }
  public static void main( String args[] )
  {Tetrahedron application = new Tetrahedron();
  application.setDefaultCloseOperation( EXIT_ON_CLOSE );    }  }

参考:JAVA如何编写第5版,由Harvey Deitel,Paul Deitel撰写

相关问题