/** MyApplet11.java: Random Color Wheel Animation ** This applet has all the code necessary to create ** a nice animation using only drawn images and text. ** In addition, it illustrates how to use random ** colors and simulate a circular rotation effect. ** Assignment: Experiment with changing the text ** colors, and shapes. Further, see if you can ** change the wheel to appear to rotate clockwise. ** Also, take a look at the MyApplet11.html webpage ** file for displaying this applet. **/ /** USE EXISTING JAVA CLASSES **/ import java.awt.*; import java.applet.Applet; /** EXTEND THE JAVA APPLET CLASS AND IMPLEMENT THE RUNNABLE INTERFACE **/ public class MyApplet11 extends Applet implements Runnable {int frameNumber = -1; // Define some animator variables. int delay; Thread animatorThread; boolean frozen = true; String framestr = ""; Dimension preImageDim; // Define preImage variables needed to Image preImage; // create the offscreen copy of the image Graphics preImageGraphics; // we want to paint on the screen. Font font1 = new Font("TimesRoman",Font.PLAIN,30); // Define some fonts. Font font2 = new Font("TimesRoman",Font.BOLD,30); Font font3 = new Font("TimesRoman",Font.ITALIC,30); Font font4 = new Font("TimesRoman",Font.BOLD+Font.ITALIC,30); FontMetrics font1m = getFontMetrics(font1); // Define font metrics for centering text. FontMetrics font2m = getFontMetrics(font2); FontMetrics font3m = getFontMetrics(font3); FontMetrics font4m = getFontMetrics(font4); String Title1 = "Random Color Wheel"; // Specify text for titles. String Title2 = ""; int rval, gval, bval; // Define random color variables. Color brown = new Color(128,128,0); // Define a bunch of colors. Color purple = new Color(135,31,120); Color plum = new Color(127,0,127); Color skyblue = new Color(112,147,219); Color aqua = new Color(56,176,222); Color richblue = new Color(63,0,255); Color newmidnightblue = new Color(0,0,156); Color starblue = new Color(0,100,255); Color darkblue = new Color(0,0,80); Color hotpink = new Color(255,28,174); Color neonpink = new Color(255,110,199); Color turquoise = new Color(56,147,219); Color aquamarine = new Color(112,219,147); Color chocolate = new Color(92,51,23); Color blueviolet = new Color(159,95,159); Color darkgreen = new Color(74,118,110); Color darkorchid = new Color(153,50,205); Color firebrick = new Color(142,35,35); Color forestgreen = new Color(35,142,35); Color huntergreen = new Color(33,94,33); Color limegreen = new Color(50,205,50); Color mandarinorange = new Color(228,120,51); Color maroon = new Color(142,35,107); Color mediumblue = new Color(50,50,205); Color mediumorchid = new Color(147,112,219); Color mediumslateblue = new Color(127,0,255); Color mediumturquoise = new Color(112,219,219); Color mediumvioletred = new Color(219,112,147); Color midnightblue = new Color(47,47,79); Color navyblue = new Color(35,35,142); Color neonblue = new Color(77,77,255); Color brightgold = new Color(217,217,25); Color orchid = new Color(219,112,219); Color seagreen = new Color(35,142,104); Color slateblue = new Color(0,127,255); Color springgreen = new Color(0,255,127); Color lightturquoise = new Color(173,234,234); Color violetred = new Color(204,50,153); Color verydarkbrown = new Color(92,64,51); /** INITIALIZE THE APPLET **/ public void init() {setBackground(purple); String str; int fps = 10; // Define the default fps (frames per second). str = getParameter("fps"); // Possibly, get HTML string specifying the try // fps. {if (str != null) {fps = Integer.parseInt(str);} // If possible, convert HTML fps to an } // integer. catch (Exception e) {} // In case the try caused something strange. delay = (fps > 0) ? (1000/fps): 100; // Convert fps to the milisecond delay } // time between frames. /** CLICK TO START OR STOP THE APPLET **/ public boolean mouseDown(Event e, int x, int y) {if (frozen) {frozen = false; start();} else {frozen = true; stop();} return true; } /** GENERATE AND/OR START THE ANIMATION THREAD **/ public void start() {if (frozen) { } // The animation is supposed to stay frozen. else // Otherwise, generate and/or start the {if (animatorThread == null) // animation thread. Apparently, if run {animatorThread = new Thread(this);} // breaks after catching an exception, animatorThread.start(); // the animatorThread though not null } // will still need to be restarted. } /** CREATE AND DISPLAY FRAMES OF THE ANIMATION THREAD **/ public void run() {Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // Set the priority low. long startTime = System.currentTimeMillis(); // Remember start time. while (Thread.currentThread() == animatorThread) // The animation loop. {frameNumber++; // Advance the frame number. framestr = "Frame " + frameNumber; // Create the frame string. repaint(); // Display the frame. try // Sleep for the delay period. {startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) {break;} // In case the try caused } // something strange, exit } // the while loop. /** CREATE THE CURRENT FRAME OF THE ANIMATION THREAD **/ public void paint(Graphics g) {Dimension d = size(); String ClickPrompt ="Click to Start/Stop Animation"; // Begin code to prompt user to start. int xClickPrompt = (d.width - font2m.stringWidth(ClickPrompt))/2; int font3ht = font3m.getHeight(); int yClickPrompt = (d.height - font3ht)/3; if (frameNumber == -1) // Before starting the animation frames, {g.setFont(font3); // paint screen to prompt user to start g.setColor(neonblue); // animation by clicking. g.drawString(ClickPrompt,xClickPrompt,yClickPrompt); } else if (frameNumber >= 0) // This technique of painting by calling {if (!frozen) update(g);} // update, prevents the default (and hidden) } // behavior of update, and instead allows // the creation of an offscreen preImage. // The combined effect eliminates flicker. public void update(Graphics g) {Dimension d = size(); int WheelWidth = 175; // Define dimensions for wheels. int WheelHeight = 75; int WheelSpacing = 50; int xTitle1 = (d.width - font2m.stringWidth(Title1))/2; // Center text horizontally. int font2ht = font2m.getHeight(); // Get font height. int yTitle1 = font2ht; // Align text vertically. int xWheel = (d.width - WheelWidth)/2; // Center wheel horizontally. int yWheel = yTitle1 + WheelSpacing; // Align wheel vertically. String Title2 = framestr; int xTitle2 = (d.width - font2m.stringWidth(Title2))/2; int yTitle2 = yWheel + WheelHeight + WheelSpacing + font2ht; if ((preImageGraphics == null) || // When all images are loaded, (d.width != preImageDim.width) || // compare the current applet (d.height != preImageDim.height)) // viewing size to our preImage, {preImageDim = d; // and if different, resize our preImage = createImage(d.width,d.height); // next preImage. preImageGraphics = preImage.getGraphics(); } preImageGraphics.setColor(getBackground()); // Clear the previous preImage preImageGraphics.fillRect(0,0,d.width,d.height); // by filling with the current // background color (purple). // and then create the next // offscreen preImage. preImageGraphics.setFont(font2); preImageGraphics.setColor(turquoise); preImageGraphics.drawString(Title1,xTitle1,yTitle1); // Paint Title1. preImageGraphics.drawString(Title2,xTitle2,yTitle2); // Paint Title2. for (int i = 0; i < 360; i += 20) // Loop which generates a random red, {rval =(int) Math.floor(Math.random()*256); // green, blue mix for every 20 degree gval =(int) Math.floor(Math.random()*256); // piece of pie in the circle. bval =(int) Math.floor(Math.random()*256); preImageGraphics.setColor(new Color(rval,gval,bval)); // Set the current random color. preImageGraphics.fillArc(xWheel,yWheel, WheelWidth,WheelHeight,i + frameNumber + 1,20); // Paint the current piece of pie. } // Offscreen preImage completed. g.drawImage(preImage,0,0,this); // Paint preImage on the screen. } /** STOP THE ANIMATION THREAD **/ public void stop() {animatorThread = null; // The user either clicked to stop or left the page, preImageGraphics = null; // so wipe out the current thread and preImage context. preImage = null; } }