Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,069 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,368 people online right now. Registration is fast and FREE... Join Now!





3-Dimensional Square Demo

A flickering blue 3D square rotates on two axis on the XYZ plane. It's a really cool demo, check this out!

Submitted By: WolfCoder
Actions:
Rating:
Views: 10,184

Language: Java

Last Modified: March 17, 2008
Instructions: Just put the code in and build, then run. Don't forget to build it as an Applet.

Snippet


  1. // Rotating wireframe cube demo
  2. // The wireframe square rotates in the applet and it appears to be in 3D!
  3. // The wireframe square changes color to demonstrating creating your own colors!
  4. // Written by WolfCoder 1-03-2005
  5. import java.awt.*; // Use graphics
  6. import java.applet.*; // Use applets
  7. import javax.swing.*; // Use swing stuff (GUI)
  8. import java.util.*; // Use util
  9. public class Cube extends JApplet
  10. {
  11.      public class CubePanel extends JPanel
  12.      {
  13.           private double x_verts[] = {-48,48,48,-48}; // Four vertices for a cube
  14.           private double y_verts[] = {48,48,-48,-48};
  15.           private double shape_save[] = {-48,48,48,-48,48,48,-48,-48}; // Original shape before transformation
  16.           private double rotation_pos = 0; // Rotation position
  17.           private double chroma_pos = 64; // Color animation position
  18.           void CubePanel()
  19.           {
  20.                // Does nothing here
  21.           }
  22.           private void drawCube(Graphics page,int xpos,int ypos)
  23.           {
  24.                // Reset to original shape
  25.                for(int index = 0;index < 4;index++)
  26.                     x_verts[index] = shape_save[index]; // Copy X's
  27.                for(int index = 4;index < 8;index++)
  28.                     y_verts[index-4] = shape_save[index]; // Copy Y's
  29.                // Rotate all the points like so...
  30.                double temp_x,temp_y;
  31.                for(int index = 0;index < 4;index++)
  32.                {
  33.                     // Rotate the square normally over the y axis in the xyz plane (I think)
  34.                     temp_x = x_verts[index]*Math.cos(rotation_pos)-y_verts[index]*Math.sin(rotation_pos);
  35.                     temp_y = x_verts[index]*Math.sin(rotation_pos)+y_verts[index]*Math.cos(rotation_pos);
  36.                     x_verts[index] = temp_x;
  37.                     y_verts[index] = temp_y;
  38.                     // And the square rotates to look 3D 2x along the x axis in the xyz plane (I think)
  39.                     y_verts[index] = y_verts[index]*Math.sin(rotation_pos/2);
  40.                }
  41.                // Add a degree of rotation
  42.                rotation_pos += 0.001;
  43.                chroma_pos++;
  44.                if(chroma_pos > 255)
  45.                     chroma_pos = 64; // Reset to flash color
  46.                // Draws the cube in question
  47.                // Selects the color
  48.                Color tempcolor = new Color(0,0,(int)chroma_pos);
  49.                page.setColor(tempcolor);
  50.                for(int index = 0;index < 4;index++)
  51.                {
  52.                     if(index < 3)
  53.                     {
  54.                          page.drawLine((int)x_verts[index]+xpos,(int)y_verts[index]+ypos,(int)x_verts[index+1]+xpos,(int)y_verts[index+1]+ypos);
  55.                     }
  56.                     else
  57.                     {
  58.                          page.drawLine((int)x_verts[index]+xpos,(int)y_verts[index]+ypos,(int)x_verts[0]+xpos,(int)y_verts[0]+ypos);
  59.                     }
  60.                }
  61.           }
  62.           public void paintComponent(Graphics page)
  63.           {
  64.                super.paintComponent(page);
  65.                // Use a black background
  66.                setBackground(Color.black);
  67.                // Draw the cube
  68.                drawCube(page,128,128);
  69.                repaint();
  70.           }
  71.      }
  72.      public void init()
  73.      {
  74.           setSize(256,256);
  75.           getContentPane().add(new CubePanel());
  76.      }
  77. }

Copy & Paste


Comments


FlawlessAxis 2008-03-23 01:29:27

Wow, a very useful snippet. Thanks for the contribution.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month