home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 45 / SuperCD45.iso / talleres / java / stage4 / HawkRay.java < prev    next >
Encoding:
Java Source  |  2000-01-30  |  4.4 KB  |  179 lines

  1. /*
  2.  * @(#)HawkRay.java    1.0 98/04/12
  3.  * 
  4.  * Copyright (c) 1998, 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.*;
  20. import java.awt.event.*;
  21. import java.applet.Applet;
  22. import java.awt.image.PixelGrabber;
  23. import java.awt.image.MemoryImageSource;
  24.  
  25. public class HawkRay extends Applet implements Runnable {
  26.     protected int w, h, dispx = 0, dispz = 0, m_sw, m_sh, texel[], m_cos1, m_sin1, xres, yres;
  27.     protected int m_alti = 120, d = 1, viewA = 180, dViewA = 3;
  28.     protected int screenPixel[];
  29.     protected MemoryImageSource screenMem;
  30.     protected Image screenImage;
  31.  
  32.     private int garbageCnt = 0;
  33.     private Thread theThread;
  34.     private boolean m_tAnimate = true;
  35.  
  36. //  Initialisation code
  37.  
  38.     public void init() {
  39.         addMouseListener(new MouseAdapter() {
  40.             public void mousePressed(MouseEvent e){
  41.                 m_tAnimate = !m_tAnimate;
  42.             }
  43.         });
  44.  
  45.         MediaTracker tracker = new MediaTracker(this);
  46.         Image piccy = getImage(getDocumentBase(), getParameter("image"));
  47.         m_sh = getSize().height << 2; m_sw = getSize().width;
  48.         screenPixel = new int[m_sw * m_sh >> 2];
  49.         screenMem = new MemoryImageSource(m_sw, m_sh >> 2, screenPixel, 0, m_sw);
  50.         tracker.addImage(piccy, 0);
  51.         try {
  52.             tracker.waitForID(0);
  53.         }
  54.         catch(Exception e) {}
  55.         w = piccy.getWidth(this); h = piccy.getHeight(this);
  56.         texel = new int[w * h];
  57.         PixelGrabber pixelGrabber = new PixelGrabber(piccy.getSource(),
  58.             0, 0, w, h, texel, 0, w);
  59.         try {
  60.             pixelGrabber.grabPixels();
  61.         }
  62.         catch(Exception e) {
  63.             return;
  64.         }
  65.         piccy = null;
  66.     }
  67.  
  68. // Running section
  69.  
  70.     public void start() {
  71.         (theThread = new Thread(this)).start();
  72.     }
  73.  
  74.     public void stop() {
  75.         if (theThread != null) {
  76.             theThread = null;
  77.         }
  78.     }
  79.  
  80.     public void run() {
  81.         Graphics myGraphics = getGraphics();
  82.  
  83.         // Initialize the timer
  84.         Timer.sampleStart();
  85.  
  86.         while ((theThread != null) && m_tAnimate) {
  87.  
  88.             // Start of timed section
  89.             Timer.start();
  90.  
  91.             if (viewA > 720)
  92.                 dViewA = -1;
  93.             else if (viewA < 360)
  94.                 dViewA = 1;
  95.             viewA += dViewA;
  96.  
  97.             renderImage();
  98.  
  99.             Thread.yield();
  100.             paint(myGraphics);
  101.  
  102.             if (garbageCnt++ > 120) {
  103.                 System.gc ();
  104.                 garbageCnt = 0;
  105.             }
  106.  
  107.             // End of timed section
  108.             Timer.stop();
  109.         }
  110.  
  111.         // Stop timing session and report results
  112.         Timer.sampleEnd();
  113.         Timer.report();
  114.     }
  115.  
  116. // Painting section
  117.  
  118.     public void update(Graphics g) {
  119.         paint(g);
  120.     }
  121.  
  122.     public void paint(Graphics g) {
  123.         if (screenImage != null)
  124.             g.drawImage(screenImage, 0, 0, this);
  125.     }
  126.  
  127. //  Rendering section
  128.  
  129.     protected void renderImage() {
  130.         int x1, y1, x2, y2, x3, y3, p = 0, xinc, yinc;
  131.         int localW = w;
  132.         int localH = h;
  133.         int localSWM1 = m_sw - 1;
  134.         int localSW = m_sw;
  135.         int localDispX = dispx;
  136.         int localDispZ = dispz;
  137.         int ly = ((3 * m_sh) >> 2) + 1;
  138.         int uy = m_sh;
  139.         double localSH = (double)m_sh;
  140.         int wb = w << 10;
  141.         int hb = h << 10;
  142.         int m_alti = (int)(240.0 - 120.0 * Math.sin((double)viewA * 0.0174432925));
  143.         int m_sin1 = (int)((1 << 10) * Math.sin((double)viewA * 0.0174432925));
  144.         int m_cos1 = (int)((1 << 10) * Math.cos((double)viewA * 0.0174432925));
  145.         dispz += m_cos1 << 3;
  146.         dispx += m_sin1 << 3;
  147.         for (int y = ly; y <= uy; y++) {
  148.  
  149.             double xd1, yd1, yd2, xd2;
  150.             yd1 = ((double)(y << 1) / localSH) - 1.0;
  151.             yd2 = (double)((m_alti - yd1)) / yd1;
  152.             xd2 = -(yd2 + 1) / 2;
  153.  
  154.             x1 = (int)(m_cos1 * xd2 + m_sin1 * yd2) + localDispX;
  155.             y1 = (int)(-m_sin1 * xd2 + m_cos1 * yd2) + localDispZ;
  156.  
  157.             x2 = (int)(-m_cos1 * xd2 + m_sin1 * yd2) + localDispX;
  158.             y2 = (int)(m_sin1 * xd2 + m_cos1 * yd2) + localDispZ;
  159.  
  160.             xinc = (x2 - x1) / localSWM1;
  161.             yinc = (y2 - y1) / localSWM1;
  162.             xinc = ((xinc % wb) + wb) % wb;
  163.             yinc = ((yinc % hb) + hb) % hb;
  164.  
  165.             for (int x = 0; x < localSW; x++) {
  166.                 x3 = ((localW - x1) >> 10) % localW;
  167.                 if (x3 < 0)
  168.                     x3 += localW;
  169.                 y3 = (y1 >> 10) % localH;
  170.                 if (y3 < 0)
  171.                     y3 += localH;
  172.                 screenPixel[p++] = texel[y3 * localW + x3];
  173.                 x1 += xinc; y1 += yinc;
  174.             }
  175.         }
  176.         screenImage = createImage(screenMem);
  177.     }
  178. }
  179.