//-------------------------------------------------------------------------- // star field simulator Version 1.1 // // (C) COPYRIGHT International Business Machines Corp. 1996 // // by John Henckel, henckel@vnet.ibm.com Aug 1996 // // This is a little java program that simulates flying through a field of // stars. Have fun with it! // // If you have any comments about this, or if you make any modifications // to the program, please send me a note. // //--------------------------------------------------------------------------- // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. // // This program has not been thoroughly tested under all conditions. IBM, // therefore, cannot guarantee or imply reliability, serviceability, or // function of this program. The Program contained herein is provided // 'AS IS'. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A // PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. // IBM shall not be liable for any lost revenue, lost profits or other // consequential damages caused by the Program even if advised of the // possibility of such damages. Furthermore, IBM shall not be liable for // any delays, losses or any other damages which may result from // furnishing the Program, even if advised of the possibility of such // damages. import java.awt.*; //------------------------------------------------------------------- // This is the main applet class public class stars extends java.applet.Applet implements Runnable { Thread t; int num,shading; float color, speed, wander; boolean hyper,dbuf; star[] s; int xm,ym; // middle of canvas Image img; // used for dbuf Graphics g2; // used for dbuf public void init() { String st; color = 0.3F; speed = 1.0F; wander = 1.0F; num = 50; shading = 8; st = getParameter("dbuf"); dbuf = st==null || st.equals("yes"); st = getParameter("hyper"); hyper = st!=null && !st.equals("no"); st = getParameter("speed"); if (st!=null) speed = Float.valueOf(st).floatValue(); st = getParameter("wander"); if (st!=null) wander = Float.valueOf(st).floatValue(); st = getParameter("color"); if (st!=null) color = Float.valueOf(st).floatValue(); st = getParameter("num"); if (st!=null) num = Integer.valueOf(st).intValue(); st = getParameter("shading"); if (st!=null) shading = Integer.valueOf(st).intValue(); if (color<0 || color>1) color = 0.0F; if (wander>10) wander = 10F; // max wander wander /= 1000; if (speed>10) speed = 10F; // max speed speed /= 100; if (num<1) num=1; s = new star[num]; for (int i=0; i=num) i-=num; if (s[i].move(speed,wander)) s[i].reset(xm,ym); sx = s[i].x; sy = s[i].y; sz = s[i].z; h = s[i].h; x = (int)(sx / sz); y = (int)(sy / sz); if (x > xm || x < -xm || y > ym || y < -ym) s[i].reset(xm,ym); r = 0.3 / (sz*sz + 0.003); // note 0.3 < r < 100 if (r > shading) { r *= 1.33; for (int j=0; j 3.4) { // simple disk int j = (int)r; g.setColor(Color.getHSBColor(h,color,1.0F)); g.fillOval(xm + x - j/2,ym + y - j/2,j+1,j+1); } else if (r > 1) { // simple box g.setColor(Color.getHSBColor(h,color,1.0F)); g.fillRect(xm + x,ym + y,(int)(r+0.9),(int)(r+0.4)); } else { // simple dot x += xm; y += ym; g.setColor(Color.getHSBColor(h,color,(float)r)); g.drawLine(x,y,x,y); } } } public void update(Graphics g) { if (hyper) paint(g); else if (dbuf) { // double buffering if (img==null) { img = createImage(size().width,size().height); g2 = img.getGraphics(); } g2.setColor(Color.black); g2.fillRect(0,0,size().width,size().height); // this is because of a bug in Netscape paint(g2); g.drawImage(img,0,0,this); } else super.update(g); } public static void main(String args[]) { stars app = new stars(); app.init(); app.run(); } } class star { double x,y,z; // location double vx,vy,ax,ay; // velocity and acceleration float h; // hue static Random rand = new Random(); star() { z=0; } star(double zz) { z=zz; } void reset(int mx, int my) { x = Math.random()*2*mx - mx; y = Math.random()*2*my - my; z = 1.0; vx=vy=ax=ay=0.0; h = (float)Math.random(); } boolean move(float spd,float wan) { if (wan>0) { ax += (Math.random()-0.5)*wan; ay += (Math.random()-0.5)*wan; vx += ax; vy += ay; x += vx; y += vy; } return (z-=spd)< 2*spd; // min distance } }