/* ** TextBurst sample applet ** ** by (c) Copyright 2001 Simon Huggins ** ** For demonstration purposes only. ** ** You may use this applet on your web site, but it cannot be ** sold in either its original or altered state. ** This original notice must be included in all versions of ** the applet distributed, even if altered. ** ** The author accepts no liability for any difficulties or loss ** arising from use of this applet. It is primarily intended for ** demonstration purposes. ** ** Change History ** ~~~~~~~~~~~~~~ ** 17-Sep-2001 v1.0 Release version ** 24-Sep-2001 v1.1 Alter speed */ /* ** Import section - tells the compiler where to look for different classes ** used within this file */ import java.awt.event.*; import javax.swing.*; import java.awt.*; /* ** Defines a class called TextBurst which takes the basic JApplet ** class as a template, and adds to it to create our Applet. ** It also implements something called ActionListener which is an ** Interface for the purposes of checking when an event has occurred. */ public class TextBurst extends JApplet implements ActionListener { static int maxChangeAmount = 20; // Constant value to define the // max. speed of colour change Timer timer; // Used to update the display ever 50ms JLabel theLabel; // The label that shows the text int fontSize = 2; // Initial font size (in points) int fontDirection = -1; //How much the font size will shrink (minus) or grow by int maxFontSize; // Maximum font size int redAmount = maxChangeAmount; int redDirection = - Math.round((float)(Math.random() * maxChangeAmount + 1)); int greenAmount = maxChangeAmount; int greenDirection = - Math.round((float)(Math.random() * maxChangeAmount + 1)); int blueAmount = maxChangeAmount; int blueDirection = - Math.round((float)(Math.random() * maxChangeAmount + 1)); Color appletBgColor; //background color for the applet int timerSpeed = 80; // Timer will start at 80ms /* ** Initialize the applet - i.e. set up any values before processing begins */ public void init() { String burstText = getParameter("burstText"); // get the text parameter from the HTML page String timerSpeedString = getParameter("timerSpeed"); if (timerSpeedString != null) // check if the parameter was used timerSpeed = Integer.parseInt(timerSpeedString); // if so, convert it to an integer else timerSpeed = 80; // if not, use 80 as a default. timer = new Timer(timerSpeed, this); // Define a timer so actionPerformed gets called String fontSizeString = getParameter("maxfont"); // get font size as a string from HTML if (fontSizeString != null) // check if the parameter was used maxFontSize = Integer.parseInt(fontSizeString); // if so, convert it to an integer else maxFontSize = 20; // if not, use 20 as a default. String appletColorStr = getParameter("bgcolor"); // get color as a string from HTML if (appletColorStr != null) { // check if parameter used appletBgColor = new Color(Integer.decode(appletColorStr).intValue()); //decode hex } else appletBgColor = Color.lightGray; // if not, use light grey. theLabel = new JLabel(burstText,SwingConstants.CENTER); // Define a centred label theLabel.setForeground(new Color(redAmount,greenAmount,blueAmount)); // Set its colour theLabel.setFont(new Font("Helvetica",Font.BOLD,fontSize)); // and its font type getContentPane().setBackground(appletBgColor); // Set the Applet's background color getContentPane().add(theLabel,BorderLayout.CENTER); // and add it to the applet } /* ** Defines what should happen when the applet starts - e.g. after return to page */ public void start() { if (!timer.isRunning()) timer.start(); // start the timer running } /* **Defines what should happen when the applet stops - e.g. when leave the page */ public void stop() { if (timer.isRunning()) timer.stop(); // and stop it again } /* ** ActionListener Interface method called by the Timer every 50ms */ public void actionPerformed(ActionEvent e) { /* If the font size reaches 20 or 2, reverse the direction of shrinking / growing */ if ((fontSize==maxFontSize) || (fontSize==2)) fontDirection = -fontDirection; fontSize += fontDirection; // shrink or grow the fontSize by this amount theLabel.setFont(new Font("Helvetica",Font.BOLD,fontSize)); // change the font size /* These sections change the three colours in a similar way to the font size */ redAmount += redDirection; if ((redAmount<=10) || (redAmount>=250)) { redDirection = -redDirection; redAmount += redDirection; } greenAmount += greenDirection; if ((greenAmount<=10) || (greenAmount>=250)) { greenDirection = -greenDirection; greenAmount += greenDirection; } blueAmount += blueDirection; if ((blueAmount<=10) || (blueAmount>=250)) { blueDirection = -blueDirection; blueAmount += blueDirection; } theLabel.setForeground(new Color(redAmount,greenAmount,blueAmount)); // set the colour } /* ** Define information about the applet - in Appletviewer, see Applet | Info menu */ public String getAppletInfo() { return "Title: TextBurst\nAuthor: Simon Huggins\nDisplays a text burst effect"; } }