home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enzykbad / everything / java / rotator.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.8 KB  |  131 lines

  1. /*----------------------------------------------------------------------*/
  2. /*  Rotator -- This object draws a solid tumbling and bouncing.  All of 
  3.         the faces will eventually be visable.
  4.         - the user can also rotate the solid by dragging
  5.                                                                         */
  6. /*----------------------------------------------------------------------*/
  7. /*             Jim Morey  -  morey@math.ubc.ca  -  Jan 28,1996          */
  8. /*----------------------------------------------------------------------*/
  9.   
  10. import java.awt.image.MemoryImageSource;
  11. import java.io.*;
  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.net.*;
  15. import java.lang.*;
  16. import Solid;
  17. import Omatrix;
  18.  
  19. /*----------------------------------------------------------------------*/
  20. public class Rotator extends Canvas implements Runnable {
  21.   private Solid solid;
  22.   private double angle,anot,bounce;
  23.   private Image im;
  24.   private Graphics offscreen;
  25.   private int W,H,delay,oldx,oldy;
  26.   private Color background,foreground;
  27.   private boolean solid_ready,keep_going;
  28.   private Applet applet;
  29.   private Omatrix tmp,tmp2,tmp3,Mnot;
  30.  
  31.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  32.   Rotator(Applet applet_,Solid solid_,int wid,int hei) {
  33.     /* .. setup window layout .. */
  34.     applet = applet_;
  35.     solid = solid_;
  36.     background = Color.lightGray;
  37.     setBackground(background);
  38.     foreground = Color.black;
  39.  
  40.     delay = 100;
  41.     resize(wid,hei);
  42.     W = size().width;
  43.     H = size().height;
  44.  
  45.     im = applet.createImage(W,H);
  46.  
  47.     Mnot = new Omatrix();
  48.     tmp = new Omatrix();
  49.     tmp2 = new Omatrix();
  50.     tmp3 = new Omatrix();
  51.     tmp.Rotation(2,0,Math.PI/50);
  52.     anot = 0;
  53.     
  54.     offscreen = im.getGraphics();
  55.     offscreen.setColor(Color.lightGray);
  56.     offscreen.fillRect(0, 0, W, H);
  57.     solid.Draw(offscreen,(int)(W/2),(int)(H/2),0);
  58.     solid_ready = true;
  59.     repaint();
  60.   }
  61.  
  62.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  63.   public void paint(Graphics g) {
  64.     if (solid_ready) {
  65.       solid_ready = false;
  66.       update(g);
  67.       solid_ready = true;
  68.     }
  69.   }
  70.   public void update(Graphics g) {
  71.     offscreen.setColor(background);
  72.     offscreen.fillRect(0, 0, W, H);
  73.     offscreen.setColor(foreground);
  74.  
  75.     solid.Draw(offscreen,(int)(W/2),(int)(H/2),bounce);
  76.  
  77.     g.drawImage(im, 0, 0, null);
  78.   }
  79.  
  80.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  81.   public void run(){
  82.     keep_going = true;
  83.     angle=0;
  84.     while(keep_going){
  85.       /* .. draw the stuff .. */
  86.  
  87.       angle += 0.1;
  88.       tmp3.Rotation(1,2,angle);
  89.       tmp2.Rotation(1,0,angle*Math.sqrt(2)/2);
  90.       tmp.Rotation(0,2,angle*Math.PI/4);
  91.       solid.orient = tmp3.Times(tmp2.Times(tmp.Times(Mnot)));
  92.       bounce = Math.abs(Math.cos(0.5*(angle+anot)))*2-1;
  93.       repaint();
  94.       try {Thread.sleep(delay);} catch (InterruptedException e){}
  95.     }
  96.   }
  97.  
  98.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  99.   public boolean mouseUp(java.awt.Event evt, int x, int y) {
  100.     new Thread(this).start();
  101.     return true;
  102.   }
  103.  
  104.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  105.   public boolean mouseDown(java.awt.Event evt, int x, int y) {
  106.     keep_going = false;
  107.     try {Thread.sleep(delay*2);} catch (InterruptedException e){}
  108.     Mnot = solid.orient;
  109.     anot += angle;
  110.     angle = 0;
  111.     oldx = x;
  112.     oldy = y;
  113.  
  114.     return true;
  115.   }
  116.  
  117.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  118.   public boolean mouseDrag(java.awt.Event evt, int x, int y) {
  119.     tmp2.Rotation(0,2,(x - oldx) * Math.PI/ solid.W);
  120.     tmp.Rotation(1,2,(oldy-y) * Math.PI/ solid.H);
  121.     Mnot = tmp.Times(tmp2.Times(Mnot));
  122.     solid.orient = Mnot;
  123.     oldx = x;
  124.     oldy = y;
  125.     
  126.     bounce = Math.abs(Math.cos(0.5*anot))*2-1;
  127.     repaint();
  128.     return true;
  129.   }
  130. }
  131.