Review or while loops, using the static method showInputDialog()
from the JOptionPane class, and String processing basics.
Click RELOAD or REFRESH to try it a 2nd time. Its much faster the 2nd
time.
10/08/2002
/* Tuesday, October 8th, 2002 - good review of while loops
Background material for this example is from Big Java textbook
Pages 107-109 Section 3.7 Strings
Pages 110-112 Section 3.8 Reading Input */
import javax.swing.JOptionPane;
import objectdraw.*;
import java.awt.*;
public class ReadInputTest extends WindowController
{
private final static String prompt = "Enter the name of your favorite movie:";
private int yPosition;
public void begin()
{
new FilledRect(0, 0, 400, 550, canvas).setColor( Color.yellow );
yPosition = 10;
String str;
String theMovieName = JOptionPane.showInputDialog(prompt);
int n = theMovieName.length();
int i = 1; for (int i = 0; i < n; i++)
while (i <= n) {
{ str = "";
str = theMovieName.substring(0, i); for (int j = 0; j <= i; j++)
printString(str); {
i++; str = str + " ";
} }
str = str + theMovieName.substring(i, i + 1);
}
i = n;
while (i > 0)
{
printString( theMovieName.substring(0, i) );
i--;
}
}
private void printString( String str )
{
Text t = new Text(str, 40, yPosition, canvas);
t.setFont("Courier New");
t.setColor( Color.blue );
t.setBold();
yPosition += 14;
}
}