// mas964 ps5 // draws phrases along the mouse path, swapping from one to the next. import java.applet.*; import java.awt.*; import java.lang.Math; import java.lang.String; import StrokeAnimator; public class PhraseStroke extends StrokeAnimator { Font drawFont; FontMetrics dfm; String texts[] = {"Iamtheseaofpermutation", "Iamthecrowofdesperation", "Iamtheturmiteoftemptation", "Isenserelentlessvariation"}; int numColors = 7; char textArrays[][]; int textLengths[]; int letterYOffset, letterXOffsets[][]; int numTexts = texts.length; Color textColors[]; boolean swapText; int frameNumber; // calculate whether to swap letters left/right, reset frame counter public boolean mouseUp(Event e, int mouseX, int mouseY) { swapText = x[numPoints-1] < x[0]; frameNumber = 0; return super.mouseUp(e, mouseX, mouseY); } // Draw strings. Note that we ignore the millisecond value entirely, // preferring to just use a static frame number. public void drawFrame(int ms) { if (numPoints <= 0) return; Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setFont(drawFont); // simple method - just fade in one string, then disappear it. if (false) { g.setColor(textColors[frameNumber % numColors]); drawChars(g, (frameNumber / numColors) % numTexts); } // fancy method - crossfade between words if (true) { int color1, color2, text1, text2; color1 = frameNumber % numColors; color2 = numColors - color1 - 1; text1 = (frameNumber / numColors + 1) % numTexts; text2 = text1 - 1; if (text2 < 0) text2 = numTexts - 1; // draw the darker string last. Would be nice to have alpha... if (color1 > numColors / 2) { g.setColor(textColors[color2]); drawChars(g, text2); g.setColor(textColors[color1]); drawChars(g, text1); } else { g.setColor(textColors[color1]); drawChars(g, text1); g.setColor(textColors[color2]); drawChars(g, text2); } } frameNumber++; // ignore the time entirely repaint(); } // this method actually draws the string with the set colour. Each // letter is centered on one mouse-event point public void drawChars(Graphics g, int textNum) { int i, charIndex; for (i = 0; i < numPoints; i++) { charIndex = i % textLengths[textNum]; if (swapText) // go right to left? charIndex = textLengths[textNum] - charIndex - 1; g.drawChars(textArrays[textNum], charIndex, 1, x[i] + letterXOffsets[textNum][charIndex], y[i] + letterYOffset); } } public void init() { int i, j; // turn strings into char arrays with lengths textArrays = new char[numTexts][]; textLengths = new int[numTexts]; for (i = 0; i < numTexts; i++) { textArrays[i] = texts[i].toCharArray(); textLengths[i] = texts[i].length(); } // get the font drawFont = new Font("Helvetica", Font.BOLD, 24); dfm = getFontMetrics(drawFont); // now figure out all the centers for the characters. letterYOffset = dfm.getHeight() / 2; letterXOffsets = new int[numTexts][]; for (i = 0; i < numTexts; i++) { letterXOffsets[i] = new int[textLengths[i]]; for (j = 0; j < textLengths[i]; j++) { letterXOffsets[i][j] = - dfm.charWidth(textArrays[i][j]) / 2; } } // construct a colourmap - grey sinusoid textColors = new Color[numColors]; textColors[0] = Color.white; textColors[1] = new Color(224, 224, 224); textColors[2] = new Color(192, 192, 192); textColors[3] = new Color(128, 128, 128); textColors[4] = new Color(64, 64, 64); textColors[5] = new Color(32, 32, 32); textColors[6] = Color.black; this.setBackground(Color.white); this.setForeground(Color.black); super.init(); if (frameDelay == 0) { frameDelay = 250; } } }