SCREAM MAGNET POETRY
// java poetry copyright (c) 1995 pdc
import java.applet.*;
import java.awt.*;
public class Poetry extends Applet {
// board width and height
final int boardWidth = 500;
final int boardHeight = 500;
// applet size
final int appletWidth = boardWidth;
final int appletHeight = boardHeight + 50;
// for double buffering to prevent flicker
Image offScreenImage;
Graphics offScreen;
// do things with the font
Font wordFont;
FontMetrics wordMetrics;
// is there an active tile?
boolean activeTile = false;
// number of words
int numberWords;
// define the array of tiles
Tile tiles[];
int messagePosition;
String message;
////////////////////////////////////////////////////////////////////////////
public void init() {
resize (appletWidth, appletHeight);
try {
offScreenImage = createImage (appletWidth, appletHeight);
offScreen = offScreenImage.getGraphics ();
} catch (Exception e) {
// double-buffering not available
offScreen = null;
}
// what font to use
wordFont = new Font("Helvetica", Font.BOLD, 12);
// in case the font is stupid
if (wordFont == null)
wordFont = getFont();
wordMetrics = getFontMetrics (wordFont);
// list of words
String wordList[] = {
"I", "burn", "a", "about", "above", "ache", "after", "all",
"always", "am", "an", "and", "are", "arm", "as", "ask", "at",
"away", "bare", "be", "beat", "beauty", "bed", "behind",
"beneath", "billy" "bitter", "black", "blood", "blow", "blue", "boil",
"boy", "breast", "but", "he", "she", "by", "can", "car", "casey", "chain",
"chant", "cici", "club", "cook", "cool", "cooper", "cotton", "could", "crush", "cry", "d",
"day", "death", "debbie", "delicate", "delicious", "derek", "dewey", "diamond", "did", "do",
"dream", "dress", "drive", "drool", "drunk", "easy", "eat",
"ed", "egg", "enormous", "er", "es", "essential", "est", "evens", "fall",
"fast", "feet", "felt", "fiddle", "finger", "flood", "fluff",
"for", "forest", "frantic", "friend", "from", "ful", "garden", "ghostface",
"gift", "girl", "go", "goddess", "gone", "gorgeous", "gown",
"hair", "hallie", "has", "have", "he", "head", "heave", "her", "here",
"him", "his", "hit", "honey", "hot", "how", "if", "in", "ing",
"iron", "is", "it", "juice", "knife", "lake", "language",
"languid", "lather", "lazy", "leave", "leg", "less", "let",
"lick", "lie", "life", "light", "like", "live", "love", "loomis"
"luscious", "lust", "ly", "mad", "makers", "man","maureen", "may", "me", "mean",
"meat", "meeks", "men", "mickey" "milk", "mist", "moan", "moon", "most", "mother",
"music", "must", "my", "near", "need", "ness", "never", "next",
"no", "none", "not", "of", "on", "one", "only", "or", "orb",
"our", "ous", "out", "over", "pant", "part", "peach", "petal",
"picture", "pink", "place", "play", "please", "pole", "pound",
"power", "prescott", "produce", "puppy", "purple", "put", "r", "rain", "randy",
"raw", "read", "recall", "red", "repulsive", "riley" "rip", "road",
"rock", "rose", "run", "rust", "s", "sad", "said", "salt", "sausage",
"say", "scream", "sea", "see", "shadow", "shake", "she",
"shine", "ship", "shot", "show", "sidney", "sing", "sit", "size", "skin",
"sky", "sleep", "smear", "smooth", "so", "soar", "some",
"sordid", "spray", "spring", "stare", "still", "stop", "suit",
"summer", "sun", "sweat", "sweet", "swim", "take", "tatum", "tell",
"the", "their", "them", "then", "there", "these", "they",
"thing", "think", "those", "though", "through", "time", "tiny",
"to", "top", "trip", "trudge", "true", "ugly", "under", "up",
"urge", "use", "vision", "void", "want", "was", "watch",
"water", "wax", "we", "weak", "weary" "weather,", "weathers", "were", "what", "when", "whisper",
"white", "who", "why", "will", "wind", "winter", "with",
"woman", "women", "woodsboro", "worship", "would", "y", "yet", "you" };
numberWords = wordList.length;
tiles = new Tile [numberWords];
// pick initial coordinates for the tiles and assign names
for (int i=0; i < numberWords; i++) {
tiles[i] = new Tile (wordList[i], wordMetrics);
int tileX = boardWidth/2;
int tileY = boardHeight/2;
while ((tileX + tiles[i].width > boardWidth / 4) &&
(tileX < boardWidth * 3 / 4) &&
(tileY + tiles[i].height > boardHeight / 3) &&
(tileY < boardHeight * 2 / 3)) {
tileX = (int)(Math.random () * (float) (boardWidth - tiles[i].width));
tileY = (int)(Math.random () * (float) (boardHeight - tiles[i].height));
}
tiles[i].X = tileX;
tiles[i].Y = tileY;
}
tiles[0].X = boardWidth / 2;
tiles[0].Y = boardHeight / 2 - 10;
tiles[1].X = boardWidth / 2 + 20;
tiles[1].Y = boardHeight / 2 + 20;
message = "copyright (c) october 1995 Prominence Dot Com, Inc.";
messagePosition = (boardWidth - wordMetrics.stringWidth(message))/2;
}
////////////////////////////////////////////////////////////////////////////
public void paintApplet (Graphics g, Rectangle clip) {
if (clip.x == Integer.MIN_VALUE)
clip = new Rectangle (0, 0, size ().width, size ().height);
// set up the playing rectangle
g.setColor (new Color (99,167,167));
g.fill3DRect (0, 0, boardWidth, boardHeight, true);
// set the font in the graphics context
g.setFont (wordFont);
// set the message on the bottom
g.setColor (new Color (175, 70, 70));
g.fill3DRect (0, boardHeight, boardWidth, 30, true);
// color for the text on the bottom
g.setColor (new Color (30, 30, 30));
g.drawString (message, messagePosition, boardHeight + 20);
// draw the tiles (from the back of the list to the front)
for (int i = numberWords - 1; i >= 0; i--) {
// only redraw the tile if it falls within the clipping region
if (tiles[i].within_clip(clip.x, clip.y, clip.width, clip.height))
tiles[i].drawTile(g);
}
} // end of paintApplet
/////////////////////////////////////////////////////////////////////////////
public void paint (Graphics g) {
if (offScreen != null) {
// double-buffering available
paintApplet (offScreen, g.getClipRect ());
g.drawImage (offScreenImage, 0, 0, this);
}
else {
// no double-buffering
paintApplet (g, g.getClipRect ());
}
} // end of paint
///////////////////////////////////////////////////////////////////////////////
public void destroy() {
if (offScreen != null) {
offScreen.dispose();
// im.dispose();
}
} // end of destroy
/////////////////////////////////////////////////////////////////////////////
public void update(Graphics g) {
// Paint the applet
paint(g);
}
////////////////////////////////////////////////////////////////////////////
public boolean mouseDown(java.awt.Event event, int x, int y) {
// current tile by index of the tiles array
int currentTile = 0;
// for swapping
Tile temp;
// check to see which one was clicked in
for (int i=numberWords - 1; i >= 0; i--) {
if (tiles[i].mouse_within(x, y)) {
currentTile = i;
getAppletContext().showStatus(tiles[currentTile].word);
activeTile = true;
}
} // end of for
// put in the front of the list to ensure that it is redrawn
// to be on top of the others
if (activeTile) {
temp = tiles[currentTile];
for (int j = currentTile; j > 0; j--) {
tiles[j] = tiles[j-1];
} // end for
tiles [0] = temp;
} // end of if there is a tile active
repaint (tiles[0].X, tiles[0].Y, tiles[0].width, tiles[0].height);
return true;
} // end of mouseDown
////////////////////////////////////////////////////////////////////////////
public boolean mouseDrag(java.awt.Event event, int x, int y) {
getAppletContext().showStatus(tiles[0].word);
if (activeTile) {
repaint (tiles[0].X, tiles[0].Y, tiles[0].width, tiles[0].height);
tiles[0].X = Math.max (1, Math.min (x - tiles[0].dx, boardWidth - 1 - tiles[0].width));
tiles[0].Y = Math.max (1, Math.min (y - tiles[0].dy, boardHeight - 1 - tiles[0].height));
repaint (tiles[0].X, tiles[0].Y, tiles[0].width, tiles[0].height);
} // end of if there is a tile active
return true;
}
////////////////////////////////////////////////////////////////////////////
public boolean mouseUp(java.awt.Event event, int x, int y) {
activeTile = false;
return true;
}
/////////////////////////////////////////////////////////////////////////////
} //end of Poetry
class Tile {
static final int padX = 6, padY = 4;
// the coordinates for the upper left corner of the tile
public int X = 0;
public int Y = 0;
// offset
public int dx;
public int dy;
// the width and height of the tile
int width;
int height;
int ascent;
// the word for this tile
String word;
/////////////////////////////////////////////////////////////////////////////////////////////
// Tile constructor
Tile (String s, FontMetrics fm) {
// the word for this tile
word = s;
// find the total height of the tile; i have added 3 pixels to the top and bottom,
// this may need adjustment
height = fm.getHeight () + padY;
ascent = fm.getAscent ();
// find out how long the tile needs to be, given the word; again, i have added padding
// of 3 pixels for each side
width = (fm.stringWidth (word) + padX);
} // end of Tile constructor
//////////////////////////////////////////////////////////////////////////////////////////////
// is the current mouse position within the tile's bounds?
public boolean mouse_within (int x_coord, int y_coord) {
boolean within = false;
if (x_coord >= X && (x_coord <= (X + width)) && y_coord >= Y && (y_coord <= (Y + height)))
within = true;
// set the offset
dx=(x_coord - X);
dy=(y_coord - Y);
// true if current x and y position is within the bounds of the tile
return (within);
} // end of mouse_within
//////////////////////////////////////////////////////////////////////////////////////////////
public boolean within_clip (int clipx, int clipy, int clipwidth, int clipheight) {
boolean within = false;
// if ((l+w > LHS) && (l < RHS) && (b+h > THS) && (b < BHS))
if (((X + width) > clipx) && (X < (clipx + clipwidth)) && ((Y + height) > clipy)
&& (Y < (clipy + clipheight)))
within = true;
return (within);
} // end of within_clip
//////////////////////////////////////////////////////////////////////////////////////////////
public void drawTile (Graphics g) {
// draw the background of the tile
g.setColor (new Color (140,140,140));
g.fill3DRect (X, Y, width, height, true);
// draw the word
g.setColor (new Color (40, 40, 40));
g.drawString (word, X + padX / 2, Y + ascent + padY / 2);
} // end of drawTile
/////////////////////////////////////////////////////////////////////////////////////////////
} // end of class Tile