home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / CUECARDS.EXE / CueCards.java < prev    next >
Encoding:
Java Source  |  1997-02-21  |  4.8 KB  |  197 lines

  1. import java.awt.Graphics;
  2. import java.awt.Image;
  3. import java.awt.Color;
  4. import java.lang.Math;
  5. import java.awt.*;
  6.  
  7. public class CueCards extends java.applet.Applet implements Runnable {
  8.     static boolean  dispCopy = false;
  9.     Image       origImg, backImg, animImg;
  10.     int         w1, h1;
  11.     Graphics    backG, finG;
  12.     Thread      nick;
  13.     int         frameNo = 0;
  14.     int         frames = 12;
  15.     int         period = 50;
  16.     boolean     needAnim = false;
  17.     boolean     imLoaded = false;
  18. //    boolean     imReady = false;
  19.     boolean     needImage = false;
  20.     boolean     animReady = false;
  21.     double      angle1;
  22.     int         imageNo = 1;
  23.     Color       backCol;
  24.  
  25.     public void init () {
  26.         if (!dispCopy) {
  27.             System.out.println ("cueCards v 1.0 for Java");
  28.             System.out.println ("Copyright (c) "
  29.                 + "David Griffiths, 1996. "
  30.                 + "All rights reserved.");
  31.             System.out.println ("For more information visit "
  32.                 + "http://www.demon.co.uk/davidg,");
  33.             System.out.println ("or contact: "
  34.                 + "dgriffiths@msn.com");
  35.             dispCopy = true;
  36.         }
  37.         backCol = new Color (Integer.parseInt(
  38.             getParameter("bgcolor").substring(1),16));
  39.         setBackground (backCol);
  40.         loadImage ();
  41.     }
  42.  
  43.     public void start () {
  44.         if (nick == null) {
  45.             nick = new Thread (this);
  46.             nick.start ();
  47.         }
  48.     }
  49.  
  50.     public void stop () {
  51.         if (nick != null) {
  52.             nick.stop ();
  53.             nick = null;
  54.         }
  55.     }
  56.  
  57.     public void run () {
  58.         while (nick != null) {
  59.             if (needImage) {
  60.                 needImage = false;
  61.                 loadImage ();
  62.             }
  63.             if (needAnim) {
  64.                 needAnim = false;                    
  65.                 animReady = false;
  66.                 createAnim ();
  67.             }
  68.             repaint ();
  69. //            if (imReady && (frameNo < frames)) {
  70.             if (frameNo < frames) {
  71.                 if (frameNo == (frames - 1))
  72.                     needAnim = true;
  73.                 frameNo++;
  74.             }
  75.             else if (frameNo >= frames) {
  76.                 if (frameNo == frames)
  77.                     needImage = true;
  78.                 if (frameNo == (frames << 1) - 1) {
  79.                     frameNo = 0;
  80.                 }
  81.                 frameNo++;
  82.             }
  83.             try     {Thread.sleep (50);}
  84.             catch   (InterruptedException e) {}
  85.         }
  86.     }
  87.  
  88.     public void loadImage () {
  89.         MediaTracker tracker = new MediaTracker(this);
  90.         String nextImage = getParameter ("image" + imageNo);
  91.         if (nextImage == null) {
  92.             imageNo = 1;
  93.             nextImage = getParameter ("image1");
  94.         }
  95.         imageNo++;
  96.         imLoaded = false;
  97. //        imReady = false;
  98.         origImg = getImage (getDocumentBase(), nextImage);
  99.  
  100.         tracker.addImage(origImg, 0);
  101.  
  102.         // Wait until all images are fully loaded
  103.         //------------------------------------------------------------------
  104.         try tracker.waitForAll(); catch (InterruptedException e) {}
  105.  
  106.         w1 = origImg.getWidth (this);
  107.         h1 = origImg.getHeight (this);
  108. //        if ((w1 != -1) && (h1 != -1))
  109. //            imReady = true; 
  110.     }
  111.  
  112.     public void rotateImage (Graphics g, double angle) {
  113.         double twistAngle = angle;
  114.         double quart = (Math.PI / 2.0);
  115.         double oct = (Math.PI / 4.0);
  116.  
  117.         if (twistAngle > oct) {
  118.             shearY (g, 1);
  119.             shearX (g, 1);
  120.             shearY (g, 1);
  121.             twistAngle -= quart;
  122.         }
  123.             
  124.         shearX(g, Math.tan (twistAngle / 2.0));
  125.         shearY(g, Math.sin (twistAngle));
  126.         shearX(g, Math.tan (twistAngle / 2.0));
  127.     }
  128.  
  129.     public void shearX (Graphics g, double shearing) {
  130.         double l;
  131.         for (int i = 0; i < (h1<<1); i++) {
  132.             l = ((double)i * -shearing)
  133.                 + ((double)h1 * shearing);
  134.             g.copyArea (0, i, ((w1 * 3) >> 1), 1, (int)l, 0);
  135.             g.drawLine ((int)l, i, 0, i);
  136.             g.drawLine ((int)l + ((w1 * 3) >> 1),
  137.                 i, (w1 * 3) >> 1, i);
  138.         }
  139.     }
  140.  
  141.     public void shearY (Graphics g, double shearing) {
  142.         double l;
  143.         for (int i = 0; i < ((w1 * 3) >> 1); i++) {
  144.             l = ((double)i * shearing);
  145.             g.copyArea (i, 0, 1, h1<<1, 0, (int)l);
  146.             g.drawLine (i, (int)l, i, 0);
  147.             g.drawLine (i, (int)l + (h1<<1), i, h1<<1);
  148.         }
  149.     }
  150.  
  151.     public void update (Graphics g) {
  152.         paint (g);
  153.     }
  154.  
  155.     public void paint (Graphics g) {
  156. //        if ((frameNo < frames) && imReady) {
  157.         if (frameNo < frames) {
  158.             g.drawImage (origImg,
  159.                 0, (frameNo * frameNo * h1
  160.                 / ((frames - 1) * (frames - 1))) - h1,
  161.                 this);
  162.         }
  163.         else if ((frameNo == frames) && animReady) {
  164.             g.drawImage (origImg,
  165.                 0, 0, this);
  166.         }
  167.         else if ((frameNo > frames) && animReady) {
  168.             int i = frameNo - frames;
  169.             g.drawImage (animImg, 0, (-i * h1), this);
  170.         }
  171.         else   
  172.             if (origImg != null)
  173.                 g.drawImage (origImg, 0, 400, this); 
  174.     }
  175.  
  176.     public void createAnim () {
  177.         System.gc();
  178.         if (backImg == null) {
  179.             backImg = createImage ((w1 * 3) >> 1, h1<<1);
  180.             backG = backImg.getGraphics();
  181.             animImg = createImage (w1, frames * h1);
  182.             finG = animImg.getGraphics();
  183.             backG.setColor (backCol);
  184.         }
  185.         for (int phase = 0; phase < frames; phase++) {
  186.             backG.fillRect (0,0,w1,h1<<1);
  187.             backG.drawImage (origImg, 0, 0, this);
  188.             rotateImage (backG, 0.6 * Math.PI
  189.                     * (double) phase * (double) phase
  190.                     / (double) frames / (double) frames);
  191.             finG.drawImage (backImg,
  192.                 0, phase * h1, this);
  193.         }
  194.         animReady = true;
  195.     }
  196. }
  197.