/* * Copyright (c) 1998 Kevan Stannard. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and * without fee is hereby granted. * * Please note that this software comes with * NO WARRANTY * * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED * BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. * * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER * PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE * THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED * BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER * OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ import java.applet.*; import java.awt.*; /** * RotatingSphere is simply an animating 3d 'point' sphere. * Double buffering is used to smooth the animation a bit. * * @version 1.0 * @author Kevan Stannard */ public class JZOORotatingSphere extends Applet implements Runnable { Thread thread; Sphere sphere; int x; // x pos of sphere int y; // y pos of sphere int xSpeed; int ySpeed; Image buffer; Graphics bufferGraphics; int appletWidth; int appletHeight; int bgColorValue; int fgColorValue; Color bgColor; Color fgColor; int radius; int density; public void init() { // // initialise some stuff // appletWidth = this.size().width; appletHeight = this.size().height; x = appletWidth/2; y = appletWidth/2; xSpeed = 1; ySpeed = 2; buffer = createImage(appletWidth, appletHeight); bufferGraphics = buffer.getGraphics(); // // read parameters // // // background color // String param_bgColor = getParameter("bg"); try { bgColorValue = Integer.parseInt(param_bgColor, 16); } catch (NumberFormatException nfe) { bgColorValue = 0; } bgColor = new Color(bgColorValue); // // foreground color // String param_fgColor = getParameter("fg"); try { fgColorValue = Integer.parseInt(param_fgColor, 16); } catch (NumberFormatException nfe) { fgColorValue = 0x00ff00; } fgColor = new Color(fgColorValue); // // radius // String param_radius = getParameter("radius"); try { radius = Integer.parseInt(param_radius); } catch (NumberFormatException nfe) { radius = appletWidth/8; } // // density // String param_density = getParameter("density"); try { density = Integer.parseInt(param_density); } catch (NumberFormatException nfe) { density = 16; } System.out.println("Parameters:"); System.out.println(" bgcolor = " + bgColorValue); System.out.println(" fgcolor = " + fgColorValue); System.out.println(" radius = " + radius); System.out.println(" density = " + density); // // create a new sphere with the specified radius and density // sphere = new Sphere(radius, density); } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } public void update(Graphics g) { bufferGraphics.setColor(bgColor); bufferGraphics.fillRect(0,0,appletWidth, appletHeight); bufferGraphics.setColor(fgColor); sphere.show(bufferGraphics,x,y, this.getSize().width, this.getSize().height); paint(g); } public void paint(Graphics g) { g.drawImage(buffer,0,0,this); } public void run() { while (true) { // // move the sphere // x += xSpeed; y += ySpeed; if (x>appletWidth) { x = appletWidth; xSpeed = -xSpeed; } if (x<0) { x = 0; xSpeed = -xSpeed; } if (y>appletHeight) { y = appletHeight; ySpeed = -ySpeed; } if (y<0) { y = 0; ySpeed = -ySpeed; } try { thread.sleep(20); } catch (InterruptedException ie) { } repaint(); //sphere.rotateX(); //sphere.rotateY(); //sphere.rotateZ(); sphere.rotate(); } } }