// // Snowfall.java - graphic animation // // Dec-11-1996 0.01 North Create // Dec-12-1996 0.02 North Message Scroll // // // usage: // // // // // import java.awt.*; import java.applet.Applet; // // main class // public class Snowfall extends Applet implements Runnable { // animation properties Thread thAnimator; boolean bFrozen = false; Image iOffscreen = null; Graphics gOffscreen; int nDelay = 100; int nScreenWidth = 600; int nScreenHeight = 400; // snow-pixels pixel snow[] = new pixel[300]; int nPixel; // message String sMessage = "Snowfall.java ver0.02"; Color cMesColor = new Color(255, 0, 0); int nMesSize = 36; int nMesWidth; int cxMesPos; int cyMesPos; public void init() { // get parameter getparam(); // initialize screen resize(nScreenWidth, nScreenHeight); setBackground(Color.black); // initialize offscreen iOffscreen = createImage(nScreenWidth, nScreenHeight); gOffscreen = iOffscreen.getGraphics(); // initialize pixels for (int i = 0; i < nPixel; i++) { snow[i] = new pixel(); snow[i].init(nScreenWidth, 1000); } // initialize message if (sMessage != null) { Font f = new Font("TimesRoman", Font.ITALIC, nMesSize); FontMetrics fm = getFontMetrics(f); nMesWidth = fm.stringWidth(sMessage); cxMesPos = nScreenWidth + 20; cyMesPos = 40; gOffscreen.setFont(f); } } public void start() { if (!bFrozen) { if (thAnimator == null) { thAnimator = new Thread(this); } thAnimator.start(); } } public void stop() { if (thAnimator != null) { thAnimator.stop(); thAnimator = null; } } public boolean mouseDown(Event e, int x, int y) { if (thAnimator == null) { bFrozen = false; start(); } else { bFrozen = true; } return true; } public void run() { // remember the starting time long nStartTime = System.currentTimeMillis(); // animation loop Graphics g = getGraphics(); while (!bFrozen) { // display paint(gOffscreen); g.drawImage(iOffscreen, 0, 0, this); // delay depending on how far we are behind try { nStartTime += nDelay; Thread.sleep(Math.max(0, nStartTime - System.currentTimeMillis())); } catch (InterruptedException e) { break; } } thAnimator = null; } public void paint(Graphics g) { // paint background g.setColor(Color.black); g.fillRect(0, 0, nScreenWidth, nScreenHeight); // paint message if (sMessage != null) { g.setColor(cMesColor); g.drawString(sMessage, cxMesPos, cyMesPos); if (--cxMesPos + nMesWidth < 0) { cxMesPos = nScreenWidth + 20; } } // paint snow-pixels for (int i = 0; i < nPixel; i++) { //snow[i].erase(g); snow[i].drop(nScreenWidth, nScreenHeight); snow[i].paint(g); } } private void getparam() { String sParam; // screen size sParam = getParameter("width"); if (sParam != null) { nScreenWidth = Integer.parseInt(sParam); } sParam = getParameter("height"); if (sParam != null) { nScreenHeight = Integer.parseInt(sParam); } // number of snow-pixels nPixel = 100; sParam = getParameter("number"); if (sParam != null) { nPixel = Integer.parseInt(sParam); } if (nPixel == 0) { nPixel = 50; } else if (nPixel > 300) { nPixel = 300; } // flame delay sParam = getParameter("delay"); if (sParam != null) { nDelay = Integer.parseInt(sParam); } // message string sParam = getParameter("message"); if (sParam != null) { sMessage = sParam; } // message size sParam = getParameter("point"); if (sParam != null) { nMesSize = Integer.parseInt(sParam); } } } // // snow-pixel class // class pixel { public double cx, cy; public double dx, dy; int nSize; public void init(int nWidth, int nHeight) { cx = Math.random() * nWidth - 10 + 5; cy = Math.random() * -nHeight; dx = Math.random() * 2 - 1; dy = Math.random() * 2 + 1; nSize = (dy > 2) ? 3 : 2; } public void drop(int nWidth, int nHeight) { cx += dx; cy += dy; if (cy > nHeight || cx < 0 || cx > nWidth) { init(nWidth, 0); } dx += Math.random() - 0.5; if (dx > 1) { dx = 1; } else if (dx < -1) { dx = -1; } } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect((int)cx, (int)cy, nSize, nSize); } public void erase(Graphics g) { g.setColor(Color.black); g.fillRect((int)cx, (int)cy, nSize, nSize); } }; // Snowfall.java