-3

I need to write a program which can draws a tetrahedron (a three-dimensional shape with four triangular faces). And we have to use class s GeneralPath and method draw of class Graphics2D.Any ideas on how to do that,

Babu Ammo
  • 3
  • 3

1 Answers1

0

I've done similar program like this before, I have created two arrays calling baseX and baseY and set coordinates to it.Then added two for loops to make the corners. This way you can draw even a cube as well

 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 );    }  }

Referenced from: JAVA how to program 5th edition,written by Harvey Deitel,Paul Deitel

AVI
  • 5,516
  • 5
  • 29
  • 38
  • Nice example of bad advice. Don't override `paint` of a top level container container like `JFrame`, use a `JPanel` or something and override it's `paintComponent` method instead. `JFrame` is not double buffered and you run the risk of painting beneath the frame decorations. You should also be creating your UI's from within the context of the Event Dispatching Thread to prevent other issues – MadProgrammer Dec 02 '15 at 11:05
  • You seem to have forgotten to remove the line numbers from your example, where ever you copied it from – MadProgrammer Dec 02 '15 at 11:06
  • @MadProgrammer Referenced from JAVA how to program 5th edition,written by Harvey Deitel,Paul Deitel which i referenced about few months ago – AVI Dec 02 '15 at 11:14
  • 1
    [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Dec 02 '15 at 11:44
  • 1
    Why you shouldn't override `paint` of `JFrame` [example](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319), [example](http://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) and [example](http://stackoverflow.com/questions/17157956/why-is-overriding-paint-in-a-top-level-container-so-bad/17158482#17158482) – MadProgrammer Dec 02 '15 at 11:46