/*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Assignment # 7 - Java How To Program 2nd edition, Deitel & Deitel * Problem 14.20 on page 764 - CLASS Sign: applet created to * practice loading and displaying multiple image files using the * Media Tracker. * * This excercise contains extra work above and beyond * the problem statement. It also demonstrates the image going off one * side as it appears at the other. * * Michael Kusmierz * Xerox Corporation * Rev 1.0 June 3, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; /** * This type was created in VisualAge. */ public class Sign extends Applet implements ActionListener, Runnable { private Button ivjBtnStop = null; private Label ivjLabel1 = null; private Label ivjLabel2 = null; private TextField ivjTxtFldSleepTime = null; private TextField ivjTxtFldStepSize = null; private Button ivjButImages = null; // Sign is derived from the example Fig. 14.5: DeitelLoop4.java // Many changes have been made. // Load one or more into the array of images, loop through the array, // and display each image. It has been modified to load one image and // paint them floating across the screen from right to left. private Image myImage[]; private int totalImages = 2; // total number of images private int currentImage = 0; // current image subscript private int sleepTime = 50; // milliseconds to sleep //--- The next two objects are for double-buffering private Graphics gContext; // off-screen graphics context private Image buffer; // buffer in which to draw image private MediaTracker imageTracker; // used to track images private Thread animate; // animation thread private boolean stopSign; //--- added these to enable the maquee to be sized and // move across the screen. private int intPos; // display position changes in paint private int intSecPos; // display following first for wrap around effect private int intWidth = 1050; // sign letter width private int intHeight = 120; private int intStep; /** * This method performs actions based on the button clicked. * @param e java.awt.event.ActionEvent */ public void actionPerformed(ActionEvent e) { if (e.getSource() == ivjBtnStop) { stop(); } else if (e.getSource() == ivjButImages) { if (totalImages == 2) { totalImages = 1; showStatus("One image file selected: Oops.jpg"); } else { totalImages = 2; showStatus("Two images files selected gives the effect of blinking lights: Oops1.jpg, Oops2.jpg"); } } else { try { if (e.getSource() == ivjTxtFldSleepTime) { sleepTime = Integer.parseInt(ivjTxtFldSleepTime.getText()); } else if (e.getSource() == ivjTxtFldStepSize) { intStep = Integer.parseInt(ivjTxtFldStepSize.getText()); } } catch (Throwable exception) { showStatus("INVALID ENTRY NUMBER!!! "); } } } /** * Gets the applet information. * @return java.lang.String */ public String getAppletInfo() { return "Sign created using VisualAge for Java."; } /** * Return the BtnStop property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getBtnStop() { if (ivjBtnStop == null) { try { ivjBtnStop = new java.awt.Button(); ivjBtnStop.setName("BtnStop"); ivjBtnStop.setBounds(14, 122, 288, 23); ivjBtnStop.setLabel("Test of stop( ) to simulate viewer leaving the page."); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjBtnStop; } /** * Return the ButImages property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getButImages() { if (ivjButImages == null) { try { ivjButImages = new java.awt.Button(); ivjButImages.setName("ButImages"); ivjButImages.setBounds(540, 122, 216, 23); ivjButImages.setLabel("Two Image files (default) or One"); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjButImages; } /** * Return the Label1 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel1() { if (ivjLabel1 == null) { try { ivjLabel1 = new java.awt.Label(); ivjLabel1.setName("Label1"); ivjLabel1.setText("Sleep Adjustment (ms)"); ivjLabel1.setBounds(313, 122, 125, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel1; } /** * Return the Label2 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel2() { if (ivjLabel2 == null) { try { ivjLabel2 = new java.awt.Label(); ivjLabel2.setName("Label2"); ivjLabel2.setText("Step Size"); ivjLabel2.setBounds(488, 122, 52, 23); ivjLabel2.setVisible(false); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel2; } /** * Return the TextField1 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTxtFldSleepTime() { if (ivjTxtFldSleepTime == null) { try { ivjTxtFldSleepTime = new java.awt.TextField(); ivjTxtFldSleepTime.setName("TxtFldSleepTime"); ivjTxtFldSleepTime.setText("50"); ivjTxtFldSleepTime.setBounds(441, 122, 39, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTxtFldSleepTime; } /** * Return the TextField2 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTxtFldStepSize() { if (ivjTxtFldStepSize == null) { try { ivjTxtFldStepSize = new java.awt.TextField(); ivjTxtFldStepSize.setName("TxtFldStepSize"); ivjTxtFldStepSize.setText("10"); ivjTxtFldStepSize.setBounds(541, 122, 43, 23); ivjTxtFldStepSize.setEditable(false); ivjTxtFldStepSize.setVisible(false); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTxtFldStepSize; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(Throwable exception) { /* Uncomment the following lines to print uncaught exceptions to stdout */ // System.out.println("--------- UNCAUGHT EXCEPTION ---------"); // exception.printStackTrace(System.out); } /** * Handle the Applet init method. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ public void init() { super.init(); try { setName("BtnStart"); setLayout(null); setSize(928, 156); add(getBtnStop(), getBtnStop().getName()); add(getTxtFldSleepTime(), getTxtFldSleepTime().getName()); add(getTxtFldStepSize(), getTxtFldStepSize().getName()); add(getLabel1(), getLabel1().getName()); add(getLabel2(), getLabel2().getName()); add(getButImages(), getButImages().getName()); // user code begin {1} ivjBtnStop.addActionListener(this); //stop button stopSign = false; ivjTxtFldSleepTime.addActionListener(this); ivjButImages.addActionListener(this); //single image option button //--- viewable but not available to the user at this release ivjTxtFldStepSize.addActionListener(this); //--- load the image ((s) if there are more then one) //-- when the applet begins executing. totalImages = 2; intPos = intWidth; intSecPos = intPos + intWidth; myImage = new Image[totalImages]; buffer = createImage(intWidth, intHeight); // create image buffer gContext = buffer.getGraphics(); // get graphics context sleepTime = 50; // sleep time in the run loop intStep = 10; // increment buffer display //--- set background of buffer to white gContext.setColor(Color.white); gContext.fillRect(0, 0, intWidth, intHeight); imageTracker = new MediaTracker(this); for (int i = 0; i < myImage.length; i++) { myImage[i] = getImage(getDocumentBase(), "images/OOPS" + i + ".JPG"); showStatus (" File Load: " +getDocumentBase()+ "images/OOPS" + i + ".JPG"); //--- track loading image imageTracker.addImage(myImage[i], i); } try { imageTracker.waitForID(0); } catch (InterruptedException e) { showStatus (" InterruptedException Getting File: " +getDocumentBase()+ "images/OOPS x " + ".JPG"); } // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } /** * main entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { Frame frame; try { Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); frame = (Frame)aFrameClass.newInstance(); } catch (java.lang.Throwable ivjExc) { frame = new Frame(); } Sign aSign; Class iiCls = Class.forName("Sign1"); ClassLoader iiClsLoader = iiCls.getClassLoader(); aSign = (Sign)java.beans.Beans.instantiate(iiClsLoader,"Sign"); frame.add("Center", aSign); frame.setSize(aSign.getSize()); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of java.applet.Applet"); exception.printStackTrace(System.out); } } // display the image in the Applet's Graphics context public void paint( Graphics g ) { g.drawImage( buffer, intPos, 0, this ); intPos = intPos - intStep; intSecPos = intSecPos - intStep; if (intPos == 0 ) { intSecPos = intWidth; } else if (intSecPos == 0 ) { intPos = intWidth; } g.drawImage( buffer, intSecPos , 0, this ); if (intPos < 0 - intWidth ) intPos = intWidth; else if (intSecPos < 0 - intWidth) intSecPos = intWidth; } public void run() { while (! stopSign ) { //--- mjk For the special case of only one image to display // prevent array out of bounds if ( currentImage >= myImage.length ) currentImage = 0; if ( imageTracker.checkID( currentImage, true ) ) { //--- clear previous image from buffer gContext.fillRect( 0, 0, intWidth, intHeight ); //--- draw new image in buffer gContext.drawImage( myImage[ currentImage ], 0, 0, this ); currentImage = ( currentImage + 1 ) % totalImages; } try { Thread.sleep( sleepTime ); } catch ( InterruptedException e ) { showStatus( e.toString() ); } repaint(); // display buffered image } } // start the applet public void start() { //--- always start with 1st image gContext.drawImage( myImage[ 0 ], 0, 0, this ); currentImage = 1; //--- create a new animation thread when user visits page if ( animate == null ) { animate = new Thread( this ); animate.start(); } } // terminate animation thread when user leaves page public void stop() { if ( animate != null ) { //animate.stop(); IBM Visual age uses JDK1.1.7, JDK 1.2 deprecates //animate = null; stopSign = true; } } // override update to eliminate flicker public void update( Graphics g ) { paint( g ); } }