【问题标题】:Drawing tetrahedron according to user input in Swing根据 Swing 中的用户输入绘制四面体
【发布时间】:2015-12-02 09:34:02
【问题描述】:

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

【问题讨论】:

  • 你能再描述一下吗?
  • 这是要求别人做你的作业吗?请提供有关您已尝试过的内容以及到目前为止您咨询过哪些资源的信息。
  • 到目前为止你做了什么吗?

标签: java swing


【解决方案1】:

我以前做过类似的程序, 我创建了两个数组调用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 how to program 5th edition,作者 Harvey Deitel,Paul Deitel

【讨论】:

  • 糟糕建议的好例子。不要覆盖像JFrame 这样的顶级容器容器的paint,使用JPanel 或其他东西并覆盖它的paintComponent 方法。 JFrame 不是双缓冲的,您冒着在框架装饰下方绘画的风险。您还应该在事件调度线程的上下文中创建您的 UI,以防止出现其他问题
  • 您似乎忘记从您的示例中删除行号,您曾经从哪里复制它
  • @MadProgrammer 引用自 JAVA 如何编程第 5 版,由 Harvey Deitel、Paul Deitel 编写,我在几个月前引用过
  • 为什么你不应该覆盖 paintJFrame exampleexampleexample
猜你喜欢
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多