import java.awt.*; import java.applet.*; import java.lang.*; import java.util.*; // for Vector import java.awt.event.*; // for the ActionListener stuff // public class MovingStrings extends BasicAnimationApplet { // tf: area for entering new strings to animate TextField tf = new TextField("Type in here and press return"); // moving_strings: a list of AnimatedStrings Vector moving_strings = new Vector(); public void init() { // Add the TextField to the applet add(tf); // Register an ActionListener for the TextField tf.addActionListener(new TfReturnListener()); // Create a first string to have something to do until the user // gives me another string to animate. newString("hi"); } // newString: Add a single AnimatedString to our list of them public void newString(String str) { int width = getSize().width; // width of applet window int height = getSize().height; // height of applet window AnimatedString as = new AnimatedString(str, width, height); moving_strings.addElement(as); } // Update the states of all of the the AnimatedString public void step() { int width = getSize().width; // width of applet window int height = getSize().height; // height of applet window for(int si=0; si < moving_strings.size(); si++) { AnimatedString as = (AnimatedString) moving_strings.elementAt(si); as.step(width, height); } } // Draw all of the the AnimatedString public void paint(Graphics g) { for(int si=0; si < moving_strings.size(); si++) { AnimatedString as = (AnimatedString) moving_strings.elementAt(si); as.paint(g, this); } } // TfReturnListener: ActionListener for TextField // This is called when "return" is pressed in the TextField class TfReturnListener implements java.awt.event.ActionListener { public void actionPerformed(ActionEvent ev) { // Get the text from the field and add it to the list of AnimatedStrings. newString(tf.getText()); // Erase the TextField to make this easier to play with. tf.setText(""); } } } // AnimatedString: a String with a position and velocity class AnimatedString extends Object { int x; // horizontal position of string int y; // vertical position of string int vx; // horizontal velocity of string int vy; // vertical position of string String str; // string to draw float [] color_hsb = new float[3]; // color to draw text in public AnimatedString(String s, int width, int height) { str = s; x = rand(1, width-1); y = rand(1, height-1); vx = rand(-5, 5); vy = rand(-5, 5); color_hsb[0] = 1.0f; // hue color_hsb[1] = 0.8f; // saturation color_hsb[2] = 1.0f; // value } public void step(int width, int height) { x += vx; y += vy; // To make the string not draw as much off the right side, // subtract an approximation of the width of the string from // the applet width. int check_width = width - str.length() * 6; if (x > check_width) { // hit right wall vx = -vx; // bounce x = check_width; } if (x < 0) { // hit left wall vx = -vx; // bounce x = 0; } if (y > height) { // hit bottom wall vy = -vy; // bounce y = height; } if (y < 0) { // hit top wall vy = -vy; // bounce y = 0; } color_hsb[0] += 0.05f; // rotate the hue color_hsb[1] = 0.80f; // high saturation color_hsb[2] = 1.00f; // high brightness } public void paint(Graphics g, Component c) { // c.setForeground( // Color.getHSBColor(color_hsb[0], color_hsb[1], color_hsb[2])); g.drawString(str, x, y); } // NAME // rand: random number generator // // ARGUMENTS // low (in): lower limit of random value // high (in): upper limit of random value public static int rand(int low, int high) { return low + (int) (Math.random( ) * (high - low + 1)); } }