CSCI A201/A597

Lecture Notes 8

Spring 2000


Console windows, interactive programs. Drawing windows, graphical primitives, mouse input.

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: With time we'll keep pounding on this until one day you will realise how clear this is.

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:

In addition to that we will be also talking about it in class. I promise I will be bringing a console window with me, one that I have in my office in Lindley, I use it all the time. We will look at it and discuss it, then I will be taking it back to the office.

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:

These things are all functions that you could invoke on all drawing window objects. These are essentially buttons that you could push on the window inside it, as if the window is a TV screen where you can create your pictures.

We'll go over these in class. Pt (from "point," on the screen).

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(); 


	} 
    } 
} 


Last updated on February 3, 2000 by Adrian German