CSCI A201/A597

Lecture Notes 21

Spring 2000


Twelve little methods (part I).

This set of lecture notes is supposed to help you with the homework assignment. The same is true of the lab notes 10 (from last week) so make sure you check them. We will follow the steps outlines in those notes but won't repeat them here too much.

Let's start with problem 3: drawing a triangle in a window.

First we need to know if we can solve it.

So we write a test program:

import element.*;
import java.awt.Color;

public class TLP {
    public static void main(String[] args) {
	//---------------------- where is the drawing going to occur? 
	DrawingWindow myWindow = new DrawingWindow(); 
	//---------------------- what will determine the triangle? 
	Pt p, q, r;
	//---------------------- let's get them from the audience...
	p = new Pt(30, 30); 
	q = new Pt(100, 50); 
	r = new Pt(50, 120); 
	//---------------------- now we need to draw the triangle...
	Line a = new Line(p, q); 
	Line b = new Line(q, r); 
	Line c = new Line(r, p); // back to the starting point ...  
	//---------------------- we have first created the sides ...
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	//---------------------- and now we have drawn them... 
    } 
} 
Let's write this again with no comments. Let's also add color.

import element.*;
import java.awt.Color;

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
	Pt p, q, r;
	p = new Pt(30, 30); 
	q = new Pt(100, 50); 
	r = new Pt(50, 120); 
	Color color = Color.red;
	Line a = new Line(p, q); 
	Line b = new Line(q, r); 
	Line c = new Line(r, p); // back to the starting point ...  
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    } 
} 
What would it take to be able to draw several such triangles, say 6 triangles, of various colors?

We either write a program that is about 14 times 6 lines long, or define a procedure that draws a triangle and use that six times. We chose the second approach here.

Define a procedure (abstract away the details, identify the inputs and the outputs, and give it a name,) place it in a class and use it.

import element.*;
import java.awt.Color;

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
	Pt p, q, r;
	p = new Pt(30, 30); 
	q = new Pt(100, 50); 
	r = new Pt(50, 120); 
	Color color = Color.red;
        Library.drawTriangle(myWindow, p, q, r, color); 
    } 
} 

class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
} 
Now let's generate the color randomly:
import element.*;
import java.awt.Color;
import java.util.Random; 

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
	Pt p, q, r;
	Random gen = new Random();
	p = new Pt(30, 30); 
	q = new Pt(100, 50); 
	r = new Pt(50, 120); 
	Color color = new Color(gen.nextFloat(),
                                gen.nextFloat(), 
                                gen.nextFloat()); 
        Library.drawTriangle(myWindow, p, q, r, color); 
    } 
} 


class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
} 
Now let's generate the location of the points p, q, and r randomly, knowing that the default of a drawing window is 200 pixels by 200 pixels.

Now everytime we run the program we get a triangle whose color size and location is random.

So we should do this several times, using a for loop. Let's do this 100 times.

import element.*;
import java.awt.Color;
import java.util.Random; 

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
	Pt p, q, r;
	Random gen = new Random();
	for (int i = 0; i < 100; i++) { 
	    p = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    q = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    r = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    Color color = new Color(gen.nextFloat(),
				    gen.nextFloat(),
				    gen.nextFloat());
	    Library.drawTriangle(myWindow, p, q, r, color);
	}
    } 
} 


class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
} 
Run this and you will agree it looks great as a background. So let's create a procedure out of this and place it in our library. Then we can work against this background.

import element.*;
import java.awt.Color;
import java.util.Random; 

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
        Library.background(myWindow, 100); 
    } 
} 


class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
    public static void background(DrawingWindow d, int n) { 
	Pt p, q, r;
	Random gen = new Random();
	for (int i = 0; i < n; i++) { 
	    p = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    q = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    r = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    Color color = new Color(gen.nextFloat(),
				    gen.nextFloat(),
				    gen.nextFloat());
	    Library.drawTriangle(d, p, q, r, color);
	}
    }
} 
Now let's move a blue square against this background:
import element.*;
import java.awt.Color;
import java.util.Random; 

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
        Library.background(myWindow, 100); 
        Pt a = myWindow.awaitMousePress(); 
	int size = 20; 
	Rect rect = new Rect(a.x() - size/2, a.y() - size/2, size, size); 
	myWindow.setForeground(Color.blue); 
	myWindow.fill(rect); 
	while (myWindow.mousePressed()) {
	    Pt b = myWindow.getMouse(); 
	    rect.moveTo(b.x() - size/2, b.y() - size/2); 
	    myWindow.fill(rect); 
	} 
    } 
} 


class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
    public static void background(DrawingWindow d, int n) { 
	Pt p, q, r;
	Random gen = new Random();
	for (int i = 0; i < n; i++) { 
	    p = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    q = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    r = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    Color color = new Color(gen.nextFloat(),
				    gen.nextFloat(),
				    gen.nextFloat());
	    Library.drawTriangle(d, p, q, r, color);
	}
    }
} 
It will act as an eraser, a blue one.

We can use invertMode() to erase it first, then to draw it again.

import element.*;
import java.awt.Color;
import java.util.Random; 

public class TLP {
    public static void main(String[] args) {
	DrawingWindow myWindow = new DrawingWindow(); 
        Library.background(myWindow, 100); 
        Pt a = myWindow.awaitMousePress(); 
	int size = 20; 
	Rect rect = new Rect(a.x() - size/2, a.y() - size/2, size, size); 
	myWindow.setForeground(Color.blue); 
	myWindow.invertMode(); 
	myWindow.fill(rect); 
	while (myWindow.mousePressed()) {
	    Pt b = myWindow.getMouse(); 
	    myWindow.fill(rect); 
	    rect.moveTo(b.x() - size/2, b.y() - size/2); 
	    myWindow.fill(rect); 
	} 
    } 
} 


class Library {
    public static void drawTriangle(DrawingWindow myWindow, 
                                    Pt v1, Pt v2, Pt v3, 
                                    Color color) {
	Line a = new Line(v1, v2);
	Line b = new Line(v2, v3); 
	Line c = new Line(v3, v1); 
	myWindow.setForeground(color); 
	myWindow.draw(a); 
	myWindow.draw(b); 
	myWindow.draw(c);
	myWindow.setForeground(Color.black); 
    }
    public static void background(DrawingWindow d, int n) { 
	Pt p, q, r;
	Random gen = new Random();
	for (int i = 0; i < n; i++) { 
	    p = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    q = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    r = new Pt(Math.abs(gen.nextInt())%200, Math.abs(gen.nextInt())%200);
	    Color color = new Color(gen.nextFloat(),
				    gen.nextFloat(),
				    gen.nextFloat());
	    Library.drawTriangle(d, p, q, r, color);
	}
    }
} 
The only two changes are marked in blue.

Lab notes 9 (nine) also have a useful example.

We'll go over a similar set of examples in class today.

More precisely we'll go over these two examples:

import element.*; 

public class RB {
    public static void main(String[] args) {
	Pt p, q, r;
	p = new Pt(100, 10); 
	q = new Pt(10, 100);
	r = new Pt(99, 140); 
	Line a = new Line(p, r); 
	Line b = new Line(q, r); 
	DrawingWindow d = new DrawingWindow(); 
	d.invertMode(); 
	d.draw(a); d.draw(b); 
	while (true) { 
	    Pt m = d.awaitMousePress();
	    while (d.mousePressed()) {
		m = d.getMouse();
		d.draw(a); d.draw(b);
		a = new Line(p, m);
		b = new Line(q, m);
		d.draw(a); d.draw(b);
	    }
	}
    } 
} 
which implements the rubber band, and
import element.*;
import java.awt.Color; 

public class IM {
    public static void main(String[] args) {
	DrawingWindow d = new DrawingWindow();
	Rect r = new Rect(20, 20, 30, 30); 
	d.setForeground(Color.blue); 
	d.invertMode(); 
	d.fill(r); 
	while (true) {
  	  d.awaitMouseClick(); 
	  d.fill(r); 
	}
    } 
}
which turns a rectangle on and off.

The second one is meant to illustrate the invertMode() of the element package.

Question: what changes if we replace awaitMouseClick() with awaitMousePress() in the last example?


Last updated: Mar 28, 2000 by Adrian German