home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue153 / java / copyjava.exe / Rotate.java < prev    next >
Encoding:
Java Source  |  1999-04-04  |  3.3 KB  |  130 lines

  1. /*
  2.  * @(#)Rotate.java    1.0 1999/04/03
  3.  * 
  4.  * Copyright (c) 1999, David Griffiths. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of David Griffiths.
  7.  * This source code may not be published or redistributed without the 
  8.  * express permission of the author. 
  9.  * 
  10.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  11.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  13.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  14.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  15.  * THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  */
  18.  
  19. import java.awt.Graphics;
  20. import java.awt.Image;
  21. import java.awt.Color;
  22. import java.lang.Math;
  23. import java.awt.*;
  24.  
  25. public class Rotate extends java.applet.Applet implements Runnable {
  26.     Image origImg, backImg;
  27.     int w1, h1;
  28.     Thread thrRotate;
  29.  
  30.     public void start () {
  31.         if (thrRotate == null) {
  32.             thrRotate = new Thread (this);
  33.             thrRotate.start ();
  34.         }
  35.     }
  36.  
  37.     public void stop () {
  38.         if (thrRotate != null) {
  39.             thrRotate.stop ();
  40.             thrRotate = null;
  41.         }
  42.     }
  43.  
  44.     public void run () {
  45.         loadImage ();
  46.         Image rotateImg = createImage (w1 << 1, h1 << 1);
  47.         Graphics rotateG = rotateImg.getGraphics();
  48.         rotateG.setColor(Color.white);
  49.         backImg = createImage (w1 << 1, h1 << 1);
  50.         Graphics backG = backImg.getGraphics();
  51.         int angle = 0;
  52.         while (thrRotate != null) {
  53.             rotateG.fillRect(0, 0, w1 << 1, h1 << 1);
  54.             rotateG.drawImage(origImg, 0, 0, this);
  55.             rotateImage (rotateG, (double)angle * Math.PI / 180.0);
  56.             if ((angle += 5) > 90)
  57.                 angle = 0;
  58.             backG.drawImage(rotateImg, 0, 0, this);
  59.             try {
  60.                  Thread.sleep (50);
  61.             }
  62.             catch (InterruptedException e) {}
  63.             repaint ();
  64.         }
  65.     }
  66.  
  67.     public void loadImage () {
  68.         MediaTracker tracker = new MediaTracker(this);
  69.         String nextImage = getParameter ("image");
  70.         origImg = getImage (getDocumentBase(), nextImage);
  71.  
  72.         tracker.addImage(origImg, 0);
  73.  
  74.         try {
  75.             tracker.waitForID(0);
  76.         }
  77.         catch(InterruptedException e) {}
  78.  
  79.         w1 = origImg.getWidth (this);
  80.         h1 = origImg.getHeight (this);
  81.     }
  82.  
  83.     private final static double QUART = (Math.PI / 2.0);
  84.     private final static double OCT = (Math.PI / 4.0);
  85.  
  86.     public void rotateImage (Graphics g, double angle) {
  87.         if (angle > OCT) {
  88.             skewY (g, 1);
  89.             skewX (g, 1);
  90.             skewY (g, 1);
  91.             angle -= QUART;
  92.         }
  93.             
  94.         skewX(g, Math.tan (angle / 2.0));
  95.         skewY(g, Math.sin (angle));
  96.         skewX(g, Math.tan (angle / 2.0));
  97.     }
  98.  
  99.     public void skewX (Graphics g, double skewing) {
  100.         double l;
  101.         for (int i = 0; i < (h1<<1); i++) {
  102.             l = ((double)i * -skewing)
  103.                 + ((double)h1 * skewing);
  104.             g.copyArea (0, i, ((w1 * 3) >> 1), 1, (int)l, 0);
  105.             g.drawLine ((int)l, i, 0, i);
  106.             g.drawLine ((int)l + ((w1 * 3) >> 1),
  107.                 i, (w1 * 3) >> 1, i);
  108.         }
  109.     }
  110.  
  111.     public void skewY (Graphics g, double skewing) {
  112.         double l;
  113.         for (int i = 0; i < ((w1 * 3) >> 1); i++) {
  114.             l = ((double)i * skewing);
  115.             g.copyArea (i, 0, 1, h1<<1, 0, (int)l);
  116.             g.drawLine (i, (int)l, i, 0);
  117.             g.drawLine (i, (int)l + (h1<<1), i, h1<<1);
  118.         }
  119.     }
  120.  
  121.     public void update (Graphics g) {
  122.         paint (g);
  123.     }
  124.  
  125.     public void paint (Graphics g) {
  126.         if (backImg != null)
  127.             g.drawImage (backImg, 0, 0, this); 
  128.     }
  129. }
  130.