This section presents an extended applet example I call Poetry In Browser. It allows you to arrange word tiles on a canvas to create short poems. The words that form the tiles are set via applet parameters. Examples 21.9 through 21.11 present the code for two classes named Node and Poetry and the HTML page that serves up the Poetry applet.
Example 21.9: Node.java
1 package com.pulpfreepress.poetry; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.lang.Math; 6 7 8 /***************************************************************** 9 Node represents an individual word to be drawn on a canvas. 10 ******************************************************************/ 11 public class Node { 12 13 private int _oldx; 14 private int _oldy; 15 private int _x; 16 private int _y; 17 private int _h; 18 private int _w; 19 private String _its_string = null; 20 private FontMetrics _its_fontmetrics = null; 21 private final int XMARGIN = 3; 22 private final int YMARGIN = 3; 23 24 25 public Node(String the_string, Component c){ 26 _its_fontmetrics = c.getFontMetrics(c.getFont()); 27 _its_string = the_string; 28 _w = _its_fontmetrics.stringWidth(the_string) + (XMARGIN * 2); 29 _h = _its_fontmetrics.getHeight() + (YMARGIN * 2); 30 31 Dimension size = c.getSize(); 32 _x = (int)(Math.random() * size.width); 33 _y = (int)(Math.random() * size.height); 34 } 35 36 37 public void setX(int x){ 38 _oldx = _x; 39 _x = x - 10; 40 } 41 42 public void setY(int y){ 43 _oldy = _y; 44 _y = y - 5; 45 } 46 47 public void paint(Graphics g){ 48 g.setColor(Color.black); 49 g.fillRect(_oldx, _oldy, _w, _h); 50 g.setColor(Color.yellow); 51 g.fillRect(_x, _y, _w, _h); 52 g.setColor(Color.blue); 53 g.drawString(_its_string, _x + XMARGIN, _y + (_h/2) + YMARGIN); 54 } 55 56 public boolean inNode(int x, int y){ 57 return((x > (_x)) && (x<(_x+_w + 10)) && (y > (_y)) && (y<=(_y+_h + 5))); 58 } 59 60 public String getString(){ return _its_string;} 61 62 public boolean wasTouched(int x, int y, int w, int h){ 63 return( (x >= (_x - _w - w*4)) && (x <= (_x+_w + w*4 )) && 64 (y >= (_y - _h - h*4)) && (y <= (_y+_h + h*4))); 65 } 66 67 public int getWidth(){return _w;} 68 69 public int getHeight(){return _h;} 70 71 }// end Node class
Example 21.10: Poetry.java
1 package com.pulpfreepress.poetry; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 import java.util.*; 7 import com.pulpfreepress.*; 8 9 public class Poetry extends JApplet implements MouseMotionListener, MouseListener { 10 11 private Node _nodes[]; 12 private Node _selected_node = null; 13 private int _oldX, _oldY; 14 private Dimension _size = null; 15 private String _words; 16 private StringTokenizer _st; 17 18 public void init() { 19 this.setBackground(Color.black); 20 _size = this.getSize(); 21 _words = new String(this.getParameter("lexicon")); 22 _st = new StringTokenizer(_words, ","); 23 int count = _st.countTokens(); 24 _nodes = new Node[count]; 25 for(int i=0; i<count; i++){ 26 _nodes[i] = new Node(_st.nextToken().trim(), this); 27 } 28 this.addMouseMotionListener(this); 29 this.addMouseListener(this); 30 repaint(); 31 } // end init() method 32 33 34 public void paint( Graphics g ) { 35 for(int i = 0; i < _nodes.length; i++) { 36 _nodes[i].paint(g); 37 } 38 } 39 40 /******************************************************** 41 MouseListener Interface Methods 42 *********************************************************/ 43 public void mouseClicked(MouseEvent e){} 44 public void mouseEntered(MouseEvent e){} 45 public void mouseExited(MouseEvent e){} 46 47 public void mousePressed(MouseEvent e){ 48 for(int i = 0; i < _nodes.length; i++){ 49 if(_nodes[i].inNode(e.getX(), e.getY())) { 50 _selected_node = _nodes[i]; 51 } // end if 52 } // end for 53 } // end mousePressed() method 54 55 public void mouseReleased(MouseEvent e){ 56 _selected_node = null; 57 } 58 59 /******************************************************** 60 MouseMotionListener Interface Methods 61 ********************************************************/ 62 public void mouseDragged(MouseEvent e){ 63 if(_selected_node != null){ 64 _selected_node.setX(e.getX()); 65 _selected_node.setY(e.getY()); 66 _selected_node.paint(this.getGraphics()); 67 for(int i = 0; i < _nodes.length; i++) { 68 if(_nodes[i].wasTouched(e.getX(), e.getY(), _selected_node.getWidth(), 69 _selected_node.getHeight())) { 70 _nodes[i].paint(this.getGraphics()); 71 } // end if 72 } // end for 73 } // end if 74 } // end mouseDragged() method 75 76 public void mouseMoved(MouseEvent e){} 77 78 } // end Poetry class definition
Example 21.11: PoetryApplet.html
79 <title>Poetry In Browser</title> 80 <hr> 81 <applet archive="PoetryClasses.jar" code="com.pulpfreepress.poetry.Poetry.class" width=600 height=600> 82 <PARAM name="lexicon" value="hate,love,love,passion,desire,sex,you, 83 us,we,we,I,I,need,you,to,love,me,forever,tonight,the,our, 84 passion,will,rage,s,'s,sheets,flannel,wrap,wrapped,fan,flames, 85 fire,heat,night,sun,moon,stars,shine,bright,and,cast,an,image, 86 upon,my,soul,touch,reach,beyond,and,but,or,class,back,seat,butt, 87 tight,wet,make,to,run,swim,ing,less,less,regret,shack,up,down,go, 88 round,round,tonight,make,love,fill,full,ing,"> 89 </applet> 90 <hr> 91 <a href="Poetry.java">The source</a>
Referring to example 21.9 — the Node class represents an individual word to be displayed in the Poetry applet. Example 21-10, the Poetry applet, reads the lexicon parameter from the HTML page and creates an array of Nodes, which are painted onto the applet. The words that make up the lexicon can be easily changed by changing the lexicon parameter’s value string on the HTML page.
Referring to example 21.11 — the Poetry and Node classes are packaged in a jar file named PoetryClasses.jar. The fully qualified name of the Poetry class is “com.pulpfreepress.poetry.Poetry.class”. Note that the <html> and </ html> tags are optional and are not part of the HTML code in this example.
Figure 21-11 shows the Poetry applet running in a web browser with a short verse arranged.
Figure 21-11: Poetry Applet In Action