//// // Blinking Neon Light by Mattias Flodin // import java.awt.Graphics; import java.awt.Image; import java.lang.Math; public class BlinkItem extends java.applet.Applet implements Runnable { Image imPic[]; // An array that holds the two images int iPicIndex=0; // Keeps track of which image is displayed Thread kicker; public void init() { // *Always* resize, in case the HTML author forgot to // include WIDTH and HEIGHT tags resize(512,243); } public void Paint(Graphics g) { update(g); } // Using the update method will get rid of some flickering public void update(Graphics g) { // Display an error message if something // unexpected has happened to the images if(imPic[iPicIndex]==null) g.drawString("Error when loading picture", 0, 172); // Draw the current image g.drawImage(imPic[iPicIndex],0,0, this); } public void start() { if(kicker == null) { // If no thread is started yet kicker=new Thread(this); // then create one kicker.start(); // and start it } } public void stop() { kicker=null; } public void run() { imPic=new Image[2]; // Dimension the image array // Load the two images in our 'animation' imPic[0]=getImage(getCodeBase(), "images/Homepage1.gif"); imPic[1]=getImage(getCodeBase(), "images/Homepage2.gif"); for(;;) { // Loop forever repaint(); // Redraw the window iPicIndex=iPicIndex==0 ? 1 : 0; // Switch image // The sleep method below might be interrupted // and cause an InterruptedException, so we'll // have to catch it. try { // Wait for a random amount of time Thread.sleep( (int) (Math.random()*500)); } catch (InterruptedException e){} } } }