/* WhiteNoise Applet * (C) 2000 by Mark Ganson * Freeware * http://www.bigfoot.com/~java4free * * You can modify this code for your own purposes, but please leave the above * copyright lines in place and provide your own "modified by:" lines below. * * */ import java.awt.*; import java.applet.*; import java.util.Random; import java.net.*; public class WhiteNoise extends Applet implements Runnable{ Image imagePrettyCool = null; Image imageVisibleLink = null; Image imageBackground = null; Image imageLink = null; Image imageLink2 = null; Thread threadAnimate = null; Image imageOffScreen = null; final int NUMIMAGES = 12; Image myImages[] = new Image[NUMIMAGES]; int nCurrentImage = 0; boolean bRunning = false; boolean bImagesReady = false; boolean bImagePreparationBegan = false; Graphics graphicsOffScreen = null; int nDelay = 50; //very short delay int nImageWidth = 100; int nImageHeight = 85; Random random; int nGranularity = 1; Button statusButton; Button nameButton; Button authorButton; Button siteButton; Image imageBuffer; Graphics graphicBuffer; AudioClip audioClip = null; public void init(){ setBackground(Color.white); imagePrettyCool = getImage(getCodeBase(), "prettycool.gif"); imageBackground = getImage(getCodeBase(), "tvimage.gif"); imageLink = getImage(getCodeBase(), "whitenoiselink.gif"); //blue hyperlink image imageLink2 = getImage(getCodeBase(), "whitenoiselink2.gif"); //red hyperlink image MediaTracker tracker = new MediaTracker(this); tracker.addImage(imageBackground, 0); tracker.addImage(imageLink, 0); tracker.addImage(imageLink2, 0); tracker.addImage(imagePrettyCool, 0); try { tracker.waitForAll(); } catch (InterruptedException ie){ } imageVisibleLink = imageLink; random = new Random(); nameButton = new Button ("WhiteNoise Applet, by Mark Ganson"); siteButton = new Button("http://www.bigfoot.com/~java4free"); setLayout(new BorderLayout()); add(nameButton, "North"); add(siteButton, "South"); statusButton = new Button("Scanning available channels: "); add(statusButton, "Center"); imageBuffer = createImage(100+nImageWidth * 2, 100+nImageHeight * 2); graphicBuffer = imageBuffer.getGraphics(); } public void start() { //Start animating! bRunning = true; threadAnimate = new Thread(this); threadAnimate.start(); } public void stop(){ bRunning = false; threadAnimate = null; graphicsOffScreen = null; imageOffScreen = null; graphicBuffer = null; audioClip.stop(); } public void run() { //Just to be nice, lower this thread's priority //so it can't interfere with other processing going on. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //Remember the starting time. long startTime = System.currentTimeMillis(); //This is the animation loop. while (Thread.currentThread() == threadAnimate) { //Display it. if (!bImagePreparationBegan){ initializeImages(); try{ audioClip = getAudioClip(new URL(getCodeBase(), "white.au")); } catch (MalformedURLException me){ System.out.println(me.toString()); } audioClip.loop(); remove(nameButton); remove(siteButton); remove(statusButton); } else { repaint(); } //Delay depending on how far we are behind. try { startTime += nDelay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } } } int getRandomPrimaryColor(){ int nRetVal = (int)(random.nextFloat() * 255); return nRetVal; } Color nextColor(){ int nPrimaryColor = getRandomPrimaryColor(); return new Color(nPrimaryColor, nPrimaryColor, nPrimaryColor); } public void initializeImages(){ bImagePreparationBegan = true; bImagesReady = false; for (int i=0; i 200){ try { getAppletContext().showDocument(new URL("http://www.bigfoot.com/~java4free"), "_blank"); } catch (MalformedURLException me){ } return true; } else { if (bRunning){ stop(); } else { start(); } return true; } } public boolean mouseMove(Event e, int x, int y){ if (y > 200){ Cursor myHandCursor = new Cursor(Cursor.HAND_CURSOR); setCursor(myHandCursor); imageVisibleLink = imageLink2; return true; } else if (y < 200 && x > 250) { imageVisibleLink = imagePrettyCool; return true; } else { setCursor(Cursor.getDefaultCursor()); imageVisibleLink = imageLink; return true; } } public boolean action(Event e, Object arg){ if (e.target==siteButton){ try { getAppletContext().showDocument(new URL("http://www.bigfoot.com/~java4free"), "_blank"); } catch (MalformedURLException me){ } return true; } else { return false; } } public void paint(Graphics g){ } public void update(Graphics g){ if (imageBuffer == null || graphicBuffer == null){ imageBuffer = createImage(100+ nImageWidth * 2, 100+nImageHeight * 2); graphicBuffer = imageBuffer.getGraphics(); } if (nCurrentImage < NUMIMAGES-4){ graphicBuffer.drawImage(myImages[nCurrentImage], 25, 25, null); graphicBuffer.drawImage(myImages[nCurrentImage+1], 25+nImageWidth,25, null); graphicBuffer.drawImage(myImages[nCurrentImage+2], 25+nImageWidth, 25+nImageHeight, null); graphicBuffer.drawImage(myImages[nCurrentImage+3], 25, 25+nImageHeight, null); graphicBuffer.drawImage(imageBackground, 0, 0, null); graphicBuffer.drawImage(imageVisibleLink, 0, 200, null); g.drawImage(imageBuffer, 0, 0, null); nCurrentImage++; } else { nCurrentImage = 0; } } }