CSC 012

Introduction to Computer Science

Summary of 2/13 Lecture

 

The OutputBox Class

The mouse is fun, but suppose we just want to put some text into a window.   The authors' OutputBox class does the trick.  Besides the obvious setTitle() method, it has several print() and println() methods that allow displaying information in an OutputBox window.

//File:  Display.java
import
CSLib.*;
public class Display
{
    public static void main (String [] args)
    {
        OutputBox out = new OutputBox("Testing");
       
        out.println ("This is a test.");
        out.println ("");
        out.println ("The double space is generated by the extra println statement.");
    }
}

testing.jpg (17361 bytes)

The InputBox Class

To make things more interactive, we explored the InputBox class.  The following is a very simple example:

//File: Inbox.java
import CSLib.*;
public class Inbox
{
    public static void main (String [] args)
    {
        InputBox in = new InputBox ("Some Input");
       
        String whatever = in.readString();
    }
}   

in.jpg (4427 bytes)

Putting things together, we developed a truly interactive program:

//File: InOut.java
import CSLib.*;
public class InOut
{
    public static void main (String [] args)
    {
        OutputBox out = new OutputBox ("Great huh?");
       
        out.print ("What's your name? ");
        out.print ("");
       
        InputBox in = new InputBox();        
        String name = new String (in.readString());
       
        out.println (name);
        out.println ("Have a nice day " + name);
    }
}

prompt.jpg (15067 bytes)

input.jpg (4262 bytes)

result.jpg (16001 bytes)

 

The DrawingBox Class

Before calling it a day, we began the exploration of the authors' DrawingBox class and had some fun drawing and filling circles.  In this context, we used the Color class to add a little color to our creations.

//File:    Circles.java
import java.awt.*;    //This is where the Color class is stored.
import CSLib.*;        //This is where the DrawingBox class is stored.
public class Circles
{
    public static void main (String [] args)
    {
        DrawingBox box = new DrawingBox ("This is fun!");
       
        box.drawCircle (100, 100, 50);
        box.fillCircle (200, 100, 25);
       
        box.setColor (Color.RED);
        box.fillCircle (250, 200, 50);
    }
}

wpeB.jpg (10035 bytes)

Back to CSC 012 Home Page