next up previous
Next: Lecture 8
Up: Home

Readings



Insertion Sort Program

,----------.   ,---------.   ,--------.
| unsorted |-->| program |-->| sorted |
`----------'   `---------'   `--------'

  class InsertSort {
    int[] sortedList;
  InsertSort (int[] list) {
  // create a new list.
  // could have use the same list
    sortedList = new int[list.length];
    for (int i=0;i < list.length; i++)
      sortedList[i] = list[i];
  }
  public void sort (String[] item) {
// Given on Previous Slide
  }
  public void print () {
    for (int i=0;i < sortedList.length; i++) 
      System.out.print (sortedList[i] + " ");
    System.out.println();
  }

  public void main (String[] a) {
    String[] list =
       {"5", "2", "6", "1", "3", "4", "7"};
    InsertSort newList = new InsertSort(list);
    newList.print(); // before sorting
    newList.sort();  // sorting
    newList.print(); // after sorting
  }  }


Understanding Programs



Summary
Views of the Object Oriented Paradigm



Graphics

Applications programs: input and output files can be arbitrarily large.

Graphics programs: input is usually small and may be interactive and output changes the state of a small finite section of the screen.


These classes are derived from Object

See text Appendix ``O''


Graphics class
import java.awt.*;

A Graphics object encapsulates state
information for the basic rendering operations

This state information includes the following properties:



Graphics class
public abstract class derived from Object

provides useful drawing methods and tools for manipulating graphics

defines a context in which the user draws


Applet

Inheritance Hierarchy



Coordinate System

coord.gif

setSize(width, height);
page.drawRect(x, y, width, height);
page.drawLine(x, y, x_end, y_end);


One Raindrop only

import java.applet.Applet;
import java.awt.Graphics;
import java.util.Random;

public class Storm extends Applet { // EXTENDS
   private final int MAX_COUNT   = 500;
   private final int BUSY_WAIT   = 50000;
   private final int APPLET_SIZE = 200;
   private Random position = new Random();
   private Raindrop drop1;
   private Graphics page;

   public void init() {
      drop1 = new Raindrop();
      setSize (APPLET_SIZE, APPLET_SIZE);
      setVisible (true);
   } // method init


    public void start() {
      int count = 1;
      int wait;

      while (count < MAX_COUNT) {
         check_drop (drop1);
         count = count + 1;
         draw (page);
         wait = 0;
         while (wait < BUSY_WAIT) {
            wait = wait + 1;
         }      }   } // method start
   public void check_drop (Raindrop drop) {
     if (drop.visible())
        drop.ripple();
     else {
      int x = Math.abs(position.nextInt() % APPLET_SIZE)+1;
      int y = Math.abs(position.nextInt() % APPLET_SIZE)+1;
      drop.set_position (x, y);
      }
   } // method check_drop

   public void draw (Graphics page) {
      page.setColor(getBackground());
      page.fillRect (0, 0, APPLET_SIZE, APPLET_SIZE);
      page.setColor(getForeground());
      drop1.draw (page);
   } // method draw
} // class Storm
}


class Raindrop {
   private final int MAX_RIPPLE = 30;
   private final int RIPPLE_STEP = 2;
   private static Random new_size = new Random();
   private int current_size = 0;
   private int visible_size = 0;
   private int x = 1, y = 1;
   public boolean visible() {
      return current_size < visible_size;
   } // method visible
   public void set_position(int x_position,int y_position){
      x = x_position;
      y = y_position;
      visible_size =
         Math.abs(new_size.nextInt()
                  % (int)((y+10)/500.0*MAX_RIPPLE))+1;
      current_size = 1;
   } // method set_position
   public void ripple() {
      x = x - RIPPLE_STEP/2;
      y = y - RIPPLE_STEP/2;
      current_size = current_size + RIPPLE_STEP;
   } // method ripple
   public void draw (Graphics page) {
      page.drawOval (x, y, current_size, current_size/2);
   } // method draw
} // class Raindrop


Classes and Objects

See Storm.java

RainDrop            Storm
                    ,--------.
                    |        |
current_size 18  <---- drop1 |
                    |        |
current_size 4   <---- drop2 |
                    |        |
current_size 12  <---- drop3 |
                    |        |
current_size 7   <---- drop4 |
                    |        |
current_size 24  <---- drop5 |
                    |        |
                    `--------'



Storm and Raindrop

The state of a Raindrop is determined by

The Storm contains

Storm applet from Chapter 4

Follow the progress of one drop through the Storm applet

  1. init(): creates a new drop
  2. start(): displays 500 frames of the storm
    1. check_drop(drop1): If drop1 is visible then increase diameter otherwise find a new random position for drop1 and call set_position which finds a new random maximum diameter: visible_size.
    2. draw(page): set background and foreground colors and ask the drop to draw itself
      1. drop1.draw(page) draw current state of drop1 oval at x,y and with diameter current_size.
    3. waste some time to slow down the rate of display.



Storm could use an array for the drops

Tying the size of the array to the number of drops needed.

private static final Factor double = (APPLET_SIZE/200.0);
private final int num_drops = (int)5.0*Factor*Factor;
private Raindrop[] drop = new Raindrop[num_drops];
...
public void init () {
  for (int i;i< num_drops;i++)
    drop[i] = new Raindrop();
...}
public void start() {
...
while (count ...
  for (int i;i< num_drops;i++)
    check_drop(drop[i]);
...
public void draw (Graphics page) {
...
  for (int i;i< num_drops;i++)
    drop[i].draw(page);


More Graphics methods

Color class

Used by a Graphics object

there are 256 x 256 x 256 possible colors
16 million

constructors to define new colors

Predefined Colors



XOR mode

setXORMode(Color alt) method in the
Graphics class

shapes are drawn with color alt

painting a pixel twice cancels the alternate colour erasing the shape


Drawing Shapes

closed shapes can be opaque or unfilled

even arcs can be made opaque

public void paint (Graphics g) {
  ...
  g.drawOval(x, y, width, height);
  ...
}


Arcs

All parameters are type int

drawArc ( x, y, width, height, start_angle, end_angle)

Polygons closed shape

drawPolygon (int[] x, int[] y, int Npoints)
drawPolygon (Polygon p)
fillPolygon (int[] x, int[] y, int Npoints)
fillPolygon (Polygon p)
Polylines open shape

drawPolyline (int[] x, int[] y, int Npoints)



Fonts

italics

Bold face

page.setFont ( new Font (Name, Style, size))
page.drawString(quote, x, y);

Name is a string: "TimesRoman"

Style is a static Font field: PLAIN, BOLD

size is an integer: 12 point


Bouncing Ball working applet




next up previous
Next: Lecture 8 Up: Home

Craig MacDonald
Wed Jun 24 16:06:29 EDT 1998