Import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>title: Square block Diagram </p>
* <p>description: Draws a bounding rectangle, gets the mouse event to rotate the cube according to the mouse position. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:Gr3d1m.java</p>
* @version 1.0
*/
public class Gr3d1m extends JFrame
implements Mouselistener,mousemotionlistener {
int Dox,doy;
int angx=30,angy=30;
cube data = new cube ();
Color [] col={color.gray,color.cyan,color.green,
Color.red,color.white,color.orange,
Color.magenta,color.pink};
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
gr3d1m G3 = new gr3d1m ();
}
/**
*<br> Method Description: Constructor
*<br> input Parameters:
*<br> return type:
*/
public gr3d1m () {
settitle ("3D cube Frame");
setdefaultcloseoperation (jframe.dispose_on_close);
Addmouselistener (this);
Addmousemotionlistener (this);
SetBackground (New Color (128,128,255));
setSize (350,350);
Show ();
}
/**
*<br> Method Description: Mouse down event, get mouse position
*<br> input Parameters:
*<br> return type:
*/
public void mousepressed (MouseEvent e) {
Dox=e.getx ();
doy=e.gety ();
}
/**
*<br> Method Description: Mouse Drag event, change the mouse position, repaint screen
*<br> input Parameters:
*<br> return type:
*/
public void mousedragged (MouseEvent e) {
angx=angx+e.gety ()-doy;
angy=angy+e.getx ()-dox;
Dox=e.getx (); Doy=e.gety ();
repaint ();
}
/* Below is a way to implement MouseListener and Mousemotionlistener must be overloaded * * *
public void mousereleased (MouseEvent e) {}
public void mouseclicked (MouseEvent e) {}
public void mouseentered (MouseEvent e) {}
public void mouseexited (MouseEvent e) {}
public void mousemoved (MouseEvent e) {}
/**
*<br> Method Description: Drawing graphics
*<br> input Parameters:
*<br> return type:
*/
public void Paint (Graphics g) {
Deldeaw (g);
for (int i=0; i<data.x.length; i++) {
DRAWPG (g, data.x[i], data.y[i], data.z[i],
150,150,col[i]);
}
G.setcolor (Color.yellow);
g.drawstring ("x=" +angx%360+ "y=" +angy%360,
10,getsize (). height-30);
}
/**
*<br> Method Description: Wipe the screen, using the background color to cover the original graphics
*<br> input Parameters:
*<br> return type:
*/
public void Deldeaw (Graphics g) {
Insets Insets = Getinsets ();
int L0 = insets.left, T0 = insets.top;
G.setcolor (New Color (128,128,255));
G.fillrect (l0,t0,l0+350,t0+350);
}
/**
*<br> Method Description: Draw the square body
*<br> input Parameters:
*<br> return type:
*/
public void DRAWPG (Graphics g,double []x,double []y,
double []z,int xp,int Yp,color Co) {
double x1,y1,z0;
int len=x.length;
int [] xx=new int [Len];
int [] yy=new int [Len];
final double rad=math.pi/180.0;
double A=angx*rad;
double B=angy*rad;
Double Sina=math.sin (a), Sinb=math.sin (b);
Double Cosa=math.cos (a), Cosb=math.cos (b);
for (int i=0; i<len; i++) {
x1= x[i]*cosb+z[i]*sinb;
Z0=-X[I]*SINB+Z[I]*COSB;
y1= Y[i]*cosa-z0*sina;
xx[i]=xp+ (int) math.rint (x1);
yy[i]=yp-(int) math.rint (y1);
}
G.setcolor (CO);
G.drawpolygon (Xx,yy,len);//Draw Polygon
}
}