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