home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 2.0 KB | 80 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- /*
- * application/applet class
- */
- public class CrazyText extends Applet {
-
- String text = "Java"; // string to be displayed
- int delta = 5; // "craziness" factor: max pixel offset
- String fontName = "TimesRoman";
- int fontSize = 36;
-
- char chars[]; // individual chars in 'text'
- int positions[]; // base horizontal position for each char
- FontMetrics fm;
-
- /*
- * called when the applet is loaded
- * creates a font and initializes positions of characters
- */
- public void init() {
-
- int fontStyle = Font.BOLD + Font.ITALIC;
- setFont(new Font(fontName, fontStyle, fontSize));
- fm = getFontMetrics(getFont());
-
- chars = new char[text.length()];
- text.getChars(0, text.length(), chars, 0);
-
- positions = new int[text.length()];
- for (int i = 0; i < text.length(); i++) {
- positions[i] = fm.charsWidth(chars, 0, i) + 20;
- }
- }
-
- /*
- * draws the characters and forces a repaint 100ms later
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int x, y;
- g.setColor (new Color((float) Math.random(),
- (float) Math.random(),
- (float) Math.random()));
- for (int i = 0; i < text.length(); i++) {
- x = (int)(Math.random() * delta * 2) + positions[i];
- y = (int)(Math.random() * delta * 2) + fm.getAscent() - 1;
- g.drawChars (chars, i, 1, x, y);
- }
- repaint (100);
- }
-
- /*
- * override default update() method to eliminate
- * erasing of the panel
- */
- public void update (Graphics g) {
- paint (g);
- }
-
- /*
- * application entry point
- * create a window frame and add the applet inside
- * @param args[] - command line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Crazy");
- CrazyText crazy = new CrazyText ();
-
- f.setSize (130, 80);
- f.add ("Center", crazy);
- f.show ();
- crazy.init ();
- }
- }
-
-