/** MyApplet13.java: Moving Text and Images Animation ** Assignment: Rewrite this program so that the scrolling text that ** is displayed is phrased correctly, e.g. so that "0th" is not ** displayed and "1st", "2nd", "3rd" is displayed instead of "1th", ** "2th", "3th", respectively. Make sure to fix all such incorrect ** occurrences. In addition, change scrollDistance and scrollPosition ** so that the text scrolls onto the page incrementally from the left, ** starting with the last character of the scrolling text, instead of ** the complete text jumping onto the page all at once. ** Also, take a look at the MyApplet13.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 MyApplet13 extends Applet implements Runnable {int frameNumber = -1; // Define some animator variables. int delay; Thread animatorThread; boolean frozen = true; // These two boolean variables are set up to prompt the user boolean clickPrompt = false; // to start the animation by clicking. Switching them to the // opposite truth values will start the animation automatically. // boolean frozen = false; // This second way is a good way to start when you have a lot // boolean clickPrompt = true; // of images to load, then a screen will appear asking the user // to wait for the images to load. Try it! First change both // 2's in the init method to 30, to get more images to load. 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,24); 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 = "Duke says, 'WAZZZZZZZZZUUUUUUUUUP'"; // Specify text for titles. String Title2 = ""; int rval, gval, bval; // Define random color variables. Image gifImage[]; // Define an array of gif images and MediaTracker tracker; // a tracker to preload them. int HiNumber; // A variable to keep track of // number of times "Hi" is said. 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 = 20; // 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. gifImage = new Image[10]; tracker = new MediaTracker(this); // Spawn the tracker background thread(s) for (int i = 1; i <= 10; i++) // in order to preload the images. {gifImage[i-1] = getImage(getCodeBase(), // Note that Java arrays "images/T"+i+".gif"); // start numbering from 0. tracker.addImage(gifImage[i-1],0); } } /** 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() {try // Start loading the images and {tracker.waitForAll();} // wait until done before starting catch (InterruptedException e) {} // the animation. 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(); if (clickPrompt == false) // Begin code to prompt user. {String clickPromptStr ="Click to Start/Stop Animation"; int xclickPromptStr = (d.width - font3m.stringWidth(clickPromptStr))/2; int font3ht = font3m.getHeight(); int yclickPromptStr = (d.height - font3ht)/3; g.clearRect(0, 0, d.width, d.height); // Before starting the animation frames, g.setFont(font3); // paint screen to prompt user to start g.setColor(neonblue); // animation by clicking. g.drawString(clickPromptStr,xclickPromptStr,yclickPromptStr); clickPrompt = true; } else {if (!frozen) update(g);} // This technique of painting by calling } // 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(); if (!tracker.checkAll()) // If the images aren't all {String waitStr ="Please wait while images are loading..."; // loaded, then clear the int xwaitStr = (d.width - font3m.stringWidth(waitStr))/2; // background and notify int font3ht = font3m.getHeight(); // the user to wait. int ywaitStr = (d.height - font3ht)/3; g.clearRect(0, 0, d.width, d.height); g.setFont(font3); g.setColor(neonblue); g.drawString(waitStr,xwaitStr,ywaitStr); } else {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). int xTitle1 = (d.width - font2m.stringWidth(Title1))/2; // Center text horizontally. int font2ht = font2m.getHeight(); // Get font height. int yTitle1 = font2ht; // Align text vertically. preImageGraphics.setFont(font2); preImageGraphics.setColor(brightgold); preImageGraphics.drawString(Title1,xTitle1,yTitle1); if ((frameNumber + 5) % 10 == 0) HiNumber++; // Say "Hi" to user, framestr = "Hi StLCOP for the " + HiNumber + "th time !"; // and count times. int scrollDistance = d.width; // Compute scroll position int scrollPosition = frameNumber % scrollDistance; // of leftmost character preImageGraphics.drawString( // of framestr, and draw framestr,scrollPosition,d.height/2); // framestr above current preImageGraphics.drawImage( // image. gifImage[frameNumber % 10], // Compute current image, scrollPosition + (font2m.stringWidth(framestr))/3, // and draw below framestr. d.height/2,this); } // 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; } }