home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.8 KB | 131 lines |
- /*----------------------------------------------------------------------*/
- /* Rotator -- This object draws a solid tumbling and bouncing. All of
- the faces will eventually be visable.
- - the user can also rotate the solid by dragging
- */
- /*----------------------------------------------------------------------*/
- /* Jim Morey - morey@math.ubc.ca - Jan 28,1996 */
- /*----------------------------------------------------------------------*/
-
- import java.awt.image.MemoryImageSource;
- import java.io.*;
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
- import java.lang.*;
- import Solid;
- import Omatrix;
-
- /*----------------------------------------------------------------------*/
- public class Rotator extends Canvas implements Runnable {
- private Solid solid;
- private double angle,anot,bounce;
- private Image im;
- private Graphics offscreen;
- private int W,H,delay,oldx,oldy;
- private Color background,foreground;
- private boolean solid_ready,keep_going;
- private Applet applet;
- private Omatrix tmp,tmp2,tmp3,Mnot;
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- Rotator(Applet applet_,Solid solid_,int wid,int hei) {
- /* .. setup window layout .. */
- applet = applet_;
- solid = solid_;
- background = Color.lightGray;
- setBackground(background);
- foreground = Color.black;
-
- delay = 100;
- resize(wid,hei);
- W = size().width;
- H = size().height;
-
- im = applet.createImage(W,H);
-
- Mnot = new Omatrix();
- tmp = new Omatrix();
- tmp2 = new Omatrix();
- tmp3 = new Omatrix();
- tmp.Rotation(2,0,Math.PI/50);
- anot = 0;
-
- offscreen = im.getGraphics();
- offscreen.setColor(Color.lightGray);
- offscreen.fillRect(0, 0, W, H);
- solid.Draw(offscreen,(int)(W/2),(int)(H/2),0);
- solid_ready = true;
- repaint();
- }
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- public void paint(Graphics g) {
- if (solid_ready) {
- solid_ready = false;
- update(g);
- solid_ready = true;
- }
- }
- public void update(Graphics g) {
- offscreen.setColor(background);
- offscreen.fillRect(0, 0, W, H);
- offscreen.setColor(foreground);
-
- solid.Draw(offscreen,(int)(W/2),(int)(H/2),bounce);
-
- g.drawImage(im, 0, 0, null);
- }
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- public void run(){
- keep_going = true;
- angle=0;
- while(keep_going){
- /* .. draw the stuff .. */
-
- angle += 0.1;
- tmp3.Rotation(1,2,angle);
- tmp2.Rotation(1,0,angle*Math.sqrt(2)/2);
- tmp.Rotation(0,2,angle*Math.PI/4);
- solid.orient = tmp3.Times(tmp2.Times(tmp.Times(Mnot)));
- bounce = Math.abs(Math.cos(0.5*(angle+anot)))*2-1;
- repaint();
- try {Thread.sleep(delay);} catch (InterruptedException e){}
- }
- }
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- public boolean mouseUp(java.awt.Event evt, int x, int y) {
- new Thread(this).start();
- return true;
- }
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- public boolean mouseDown(java.awt.Event evt, int x, int y) {
- keep_going = false;
- try {Thread.sleep(delay*2);} catch (InterruptedException e){}
- Mnot = solid.orient;
- anot += angle;
- angle = 0;
- oldx = x;
- oldy = y;
-
- return true;
- }
-
- /* - - - - - - - - - - - - - - - - - - - - - - */
- public boolean mouseDrag(java.awt.Event evt, int x, int y) {
- tmp2.Rotation(0,2,(x - oldx) * Math.PI/ solid.W);
- tmp.Rotation(1,2,(oldy-y) * Math.PI/ solid.H);
- Mnot = tmp.Times(tmp2.Times(Mnot));
- solid.orient = Mnot;
- oldx = x;
- oldy = y;
-
- bounce = Math.abs(Math.cos(0.5*anot))*2-1;
- repaint();
- return true;
- }
- }
-