import java.applet.Applet; import java.awt.*; public class starsim extends Applet implements Runnable { int STARS = 200; int[] x, y, xv, xi; Color c[]; Thread runner; // Double Buffer variables Image DB_Image; Graphics DB_Graphics; Dimension DB_Dimension; public void init(){ int i, t; String p; p = getParameter("stars"); if (p == null) p = "200"; STARS = Integer.valueOf(p).intValue(); if (STARS == 0 || STARS > 200 || STARS < 1) STARS=200; x = new int[STARS]; y = new int[STARS]; xv = new int[STARS]; xi = new int[STARS]; c = new Color[STARS]; DB_Dimension = size(); if (DB_Graphics == null) { DB_Image = createImage(DB_Dimension.width, DB_Dimension.height); DB_Graphics = DB_Image.getGraphics(); } setBackground(Color.black); for(i=0;i2){} switch(t){ case 0:{ xv[i] = 2; c[i] = Color.gray; } break; case 1:{ xv[i] = 4; c[i] = Color.lightGray; } break; case 2:{ xv[i] = 6; c[i] = Color.white; } break; } // END SWITCH } // END FOR INIT LOOP } public void paint(Graphics g){ update(g); } public void update(Graphics g){ DB_Graphics.setColor(Color.black); DB_Graphics.fillRect(0, 0, DB_Dimension.width, DB_Dimension.height); // Unrolled loop to speed up pixel plotting for(int i=0;i=DB_Dimension.width) { x[i] = 0; t=4; while((t = (int)(Math.random()*30)) <0 || t>2){} switch(t){ case 0:{ xv[i] = 2; c[i] = Color.gray; } break; case 1:{ xv[i] = 4; c[i] = Color.lightGray; } break; case 2:{ xv[i] = 6; c[i] = Color.white; } break; } // END SWITCH } // END IF } // END FOR INDEX LOOP try{ Thread.sleep(50); } catch(InterruptedException e) {stop();} repaint(); } } /* * This is optional thread suspention code, it suspends the applet when the mouse enters * the applet region */ /* public boolean mouseEnter(Event evt, int x, int y) { try { runner.suspend(); } catch (InterruptedException e){stop();} return true; } public boolean mouseExit(Event evt, int x, int y) { try { runner.resume(); } catch (InterruptedException e){runner.start();} return true; } /* */ public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } }