| CSCI A201/A597Lecture Notes 8 Spring 2000 |
We will first discuss the following program:
import element.*;
public class Feb03Echo {
public static void main(String[] args) {
ConsoleWindow c = new ConsoleWindow();
c.out.println("Welcome to the Echo program!\n");
c.out.println("At the prompt please type and then press Enter.");
c.out.println("To stop the program please type: quit.\n");
c.out.print("Echo> "); c.out.flush();
while (true) {
String user = c.input.readLine();
c.out.println("You typed: (" + user + ")");
c.out.print("Echo> "); c.out.flush();
if (user.equals("quit")) {
System.exit(0);
}
}
}
}
Classes in Java are used as:
To understand the Feb03Echo program we need to realize that
it relies on the material covered in the book on pages 37-43 as follows:
output channel of the console window. In most cases when a user starts a program the first one to say "Hello" is the program. It is supposed to tell something to the user, to offer a few choices, perhaps a menu, a selection of actions. It should at the very least tell the user what the program is doing and what the user needs to do to obtain results out of the program. This, of course, if the program is user-friendly. Also, the only time when the user is the first to say something is whe the user starts the program with command line arguments (which is not the case here)
input streams. If you want to build an interactive program you need to talk and the user needs to talk too. To hear what the user's saying you need an input channel, from which you can read what the user is typing.
The key then to writing interactive programs is to anticipate the user's actions. If we can anticipate the user we can serve the user better. We just have to plan our actions around the possible things that the user could be asking for.
Next we move to draing windows and interactive graphics.
There's a part of the lecture notes from last time that we didn't cover, regarding the coordinate system in the drawing window that we need to look at. Here's that diagram again:
This looks really really close to the overall framework of the
Crossed program, except we now have additional ways
of moving around the two-dimensional organization of dots (or
pixels) on the screen.
Here are four of them:
moveTo(x, y)
move(dX, dY)
lineTo(x, y)
line(dX, dY)
We'll go over these in class.
We will use the Pt kind of objects when we will look at how
the mouse works. Some of the functions that get input from the mouse are
going to return a Pt object that represents the dot (or
pixel) where the mouse was pressed or released.
We will look at mouse input later.
In class we will look at this program:
import element.*;
public class Feb03Commands {
public static void main(String[] args) {
ConsoleWindow c = new ConsoleWindow();
DrawingWindow d = new DrawingWindow();
c.out.println("Welcome to the Line Drawing program!\n");
c.out.println("At the prompt type 'line', then an x and a y and\n" +
"then press Enter. The initial point is (100,100)");
d.moveTo(100, 100);
c.out.println("To stop the program please type: quit.\n");
c.out.print("Line> "); c.out.flush();
while (true) {
String user = c.input.readString();
if (user.equals("quit")) {
System.exit(0);
}
int dX = c.input.readInt();
int dY = c.input.readInt();
d.line(dX, dY);
c.out.print("Line> "); c.out.flush();
}
}
}