home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 45 / SuperCD45.iso / talleres / java / stage2 / HawkRay.java < prev    next >
Encoding:
Java Source  |  2000-01-30  |  4.2 KB  |  161 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.         while ((theThread != null) && m_tAnimate) {
  83.             if (viewA > 720)
  84.                 dViewA = -1;
  85.             else if (viewA < 360)
  86.                 dViewA = 1;
  87.             viewA += dViewA;
  88.  
  89.             renderImage();
  90.  
  91.             Thread.yield();
  92.             paint(myGraphics);
  93.  
  94.             if (garbageCnt++ > 120) {
  95.                 System.gc ();
  96.                 garbageCnt = 0;
  97.             }
  98.         }
  99.     }
  100.  
  101. // Painting section
  102.  
  103.     public void update(Graphics g) {
  104.         paint(g);
  105.     }
  106.  
  107.     public void paint(Graphics g) {
  108.         if (screenImage != null)
  109.             g.drawImage(screenImage, 0, 0, this);
  110.     }
  111.  
  112. //  Rendering section
  113.  
  114.     protected void renderImage() {
  115.         double x1, y1, x2, y2, x3, y3, x4, y4, va, xinc, yinc;
  116.         int p = 0;
  117.         m_alti = (int)(240.0 - 120.0 * Math.sin((double)viewA * 3.1415926 / 180.0));
  118.         m_sin1 = (int)((1 << 10) * Math.sin((double)viewA * 3.1415926 / 180.0));
  119.         m_cos1 = (int)((1 << 10) * Math.cos((double)viewA * 3.1415926 / 180.0));
  120.         va = (w * (viewA % 360) / 360);
  121.         dispz += m_cos1 << 3;
  122.         dispx += m_sin1 << 3;
  123.         for (int y = ((3 * m_sh) >> 2) + 1; y <= m_sh; y++) {
  124.             planePoint (0, y); x1 = xres; y1 = yres;
  125.             planePoint (m_sw - 1, y); x2 = xres; y2 = yres;
  126.             xinc = (x2 - x1) / (m_sw - 1);
  127.             yinc = (y2 - y1) / (m_sw - 1);
  128.             xinc = ((xinc % (w << 10)) + (w << 10)) % (w << 10);
  129.             yinc = ((yinc % (h << 10)) + (h << 10)) % (h << 10);
  130.             for (int x = 0; x < m_sw; x++) {
  131.                 setTexel(p++, (int)x1, (int)y1);
  132.                 x1 += xinc; y1 += yinc;
  133.             }
  134.         }
  135.         screenImage = createImage(screenMem);
  136.     }
  137.  
  138.     void setTexel(int p, int u, int v) {
  139.         int x3 = modIt((w - u) >> 10, w);
  140.         int y3 = modIt(v >> 10, h);
  141.         screenPixel[p] = texel[y3 * w + x3];
  142.     }
  143.  
  144.     int modIt(int a, int b) {
  145.         int result = (a) % b;
  146.         if (result < 0)
  147.             result += b;
  148.         return result;
  149.     }
  150.  
  151.     protected void planePoint (int i, int j) {
  152.         double x1, y1, y2, x2;
  153.         y1 = ((double)(j << 1) / (double)m_sh) - 1.0;
  154.         y2 = (double)(d * (m_alti - y1)) / y1;
  155.         x1 = (double)(i - (m_sw >> 1)) / (double)m_sw;
  156.         x2 = x1 * (y2 + d) / (double)d;
  157.         xres = (int)(m_cos1 * x2 + m_sin1 * y2) + dispx;
  158.         yres = (int)(-m_sin1 * x2 + m_cos1 * y2) + dispz;
  159.     }
  160. }
  161.