Computer Science 15-111 (Sections A & B), Spring 2007

Class Notes:  02-Feb-2007

Logistics

  1. Quiz 1 Retake:  today!
  2. Missed quiz policy == same as missed test policy.
  3. Reading:
    1. Mon:  Ch. 6.6 to 6.10  (and 6.0 - 6.5)
    2. Rest of Chapter 6 moved to next week!

 

Topic Outline:

1.      2d Arrays Redux:
   int[][] pixels = new int[5][7];
   System.out.println(pixels.length);    // prints out 5
   System.out.println(pixels[0].length); // prints out 7

So:
   pixelWidth  = pixels.length;
   pixelHeight = pixels[0].length;
But:
   int[][] board = new int[5][7];
   rows = boardHeight = board.length;
   cols = boardWidth  = board[0].length;
Non-rectangular arrays:
    Same, but each row can have a different number of cols.


2.      2d Arrrays Example:
     9    8    7
     6    5    4

int[][] board = { {9, 8, 7}, {6, 5, 4} };
rows = board.length = 2;
cols = board[0].length = 3;

int[][] pixels = { {9,6}, {8,5}, {7,4} };
width  = pixels.length = 3;
height = pixels[0].length = 2;

// Here is a ragged example:
int[][] raggedPixels = { {5,6}, {7}, {} }
raggedPixels.length = 3
raggedPixels [0].length = 2
raggedPixels [1].length = 1
raggedPixels [2].length = 0

 

3.      rotateImage() hints

a.      First write rotate90, then reuse this for other rotations
(Not the most efficient, but the easiest, and efficient enough for our purposes)

b.      Rotating by 90 changes dimensions from (width x height) to (height x width)
int[][] rotated = new int[pixelsHeight][pixelsWidth];

c.      Rotating by 90 maps large x to small y, but it maps large y to large x:
rotated[y][pixelsWidth - 1 - x] = pixels[x][y];

 

4.  SampleGUI.java

//////////////////////////////////////////////
// Simple Application
//////////////////////////////////////////////


public class SampleGUI extends IntroCsApplication {
  public static void main(String[] args) { new SampleGUI(); }
  public void init() {
      add(new CustomComponent());
  }
}

//////////////////////////////////////////////

// Application with buttons and component timer
//////////////////////////////////////////////


public class SampleGUI extends IntroCsApplication {
  public static void main(String[] args) { new SampleGUI(); }
  private CustomComponent circlePainter;
  public void init() {
      add(circlePainter = new CustomComponent());
      addButtons("change color", "beep");
      circlePainter.startTimer(100);
  }
 
  public void onButtonPressed(String label) {
      if (label.equals("change color")) circlePainter.changeColor();
      else if (label.equals("beep")) beep();
  }  
}


//////////////////////////////////////////////
// Application API
//////////////////////////////////////////////


public class SampleGUI extends IntroCsApplication {
  public void init() { … }
  public Component add(Component c);
  public void addButtons(String... buttonLabels) { … }
  public void onButtonPressed(String label) { … }
  public void startTimer(int timerDelay);
  public void onTimer() { … }
  public void beep();
}

//////////////////////////////////////////////
// Custom Component API

//////////////////////////////////////////////

class CustomComponent extends IntroCsComponent {
  public void paintComponent(Graphics page) { … }
  public void onMousePressed(int x, int y) { … }
  public void onMouseDragged(int x, int y) { … }
  public void onMouseReleased(int x, int y) { … }
  public void startTimer(int timerDelay);
  public void onTimer() { … }
  public void beep();
}