CSCI A201/A597

Lab Notes 8

Spring 2000


Getting started with assignment 7.

A few pointers:

Develop your program in stages:

  1. first make it draw the grid
  2. then add the two rectangles for erase and exit
  3. then get your window to report mouse clicks (to do that properly you need to accept any number of clicks so an infinite loop would be the most appropriate setting for that)
  4. if you report the clicks start paying attention to the x and y of the points where the mouse is clicked
  5. then make sure that your program exits when the right square is clicked
  6. then redraw the original grid when the other square is clicked
  7. then make the program place red filled squares at the point where the mouse was clicked, regardless of the location
  8. then restrict this to happen only inside the grid
  9. then find the cell in which the mouse was clicked and draw the filled red square on top of that one, precisely

I will update this set of notes later but hopefully this should give you a starting point. Here are some stages in the program development:

1. Creating a drawing window.

import element.*; // page 37

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44

  } 
}
2. Getting mouse input once.

import element.*; 

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44

    d.awaitMousePress(); // pages 47-49 
    System.out.println("The mouse has been pressed."); 

  } 
} 
3. Getting mouse input continuously (I)

import element.*; 

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44
    int i = 0; 
    while (true) 
    { d.awaitMousePress(); // pages 47-49 
      i = i + 1; 
      System.out.println("The mouse has been pressed " + i + " times."); 
      // this prints the message continuously while the 
      // mouse button is being kept pressed down 
    }

  } 
} 
4. Getting mouse input continuously (II)

import element.*; 

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44
    int i = 0; 
    while (true) 
    { d.awaitMousePress(); // pages 47-49 
      i = i + 1; 
      System.out.println("The mouse has been pressed " + i + " times."); 
      d.awaitMouseRelease(); // page 51
      // makes sure one message is printed per interval
      // of time that the mouse is being kepT pressed
    }

  } 
} 
This would be the right way to do mouse input here.

5. Drawing a rectangle.

import element.*; 

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44

    // Drawing a rectangle: pages 50 and 316 

    Rect r = new Rect(40, // x coord of top left corner 
                      10, // y coord of top left corner 
                      10, // width in pixels (horizontal) 
                      40); // height in pixels 

    // this creates the rectangle but doesn't draw it 
    // to draw it you need to let the windown know that
    // the rectangle should be drawn

    d.draw(r); // page 50 -- ask d to draw r 

    while (true) { 
      d.awaitMousePress();   // pages 47-49 
      d.awaitMouseRelease(); // page 51
    }
  } 
} 
6. Getting coordinates of mouse input.

import element.*;

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44

    // Drawing a rectangle: pages 50 and 316 

    Rect r = new Rect(40, // x coord of top left corner 
                      10, // y coord of top left corner 
                      10, // width in pixels (horizontal) 
                      40); // height in pixels 

    // this creates the rectangle but doesn't draw it 
    // to draw it you need to let the windown know that
    // the rectangle should be drawn

    d.draw(r); // page 50 -- ask d to draw r 

    Pt press; // define this like in the example on pages 52-53 
    // Points are described on page 45-47 and appendix E p. 312

    while (true) { 
      press = d.awaitMousePress();   // pages 47-49, 52-53 

      System.out.println("Mouse pressed at (" +  // text 
                          press.x() // invoke x() on press to get x coordinate
                          + ", " +  // text 
                          press.y() // obtain the y coordinate of the point  
                          + ")"     // text 
                        );  
      d.awaitMouseRelease(); // page 51
    }
  } 
} 
7. Closing

The only addition to the program from step 6 is marked in blue

import element.*; 

public class dgerman7 {
  public static void main(String[] args) {
    DrawingWindow d = new DrawingWindow(); // page 44

    // Drawing a rectangle: pages 50 and 316 

    Rect r = new Rect(40, // x coord of top left corner 
                      10, // y coord of top left corner 
                      10, // width in pixels (horizontal) 
                      40); // height in pixels 

    // this creates the rectangle but doesn't draw it 
    // to draw it you need to let the windown know that
    // the rectangle should be drawn

    d.draw(r); // page 50 -- ask d to draw r 

    Pt press; // define this like in the example on pages 52-53 
    // Points are described on page 45-47 and appendix E p. 312

    while (true) { 
      press = d.awaitMousePress();   // pages 47-49, 52-53 

      System.out.println("Mouse pressed at (" +  // text 
                          press.x() // invoke x() on press to get x coordinate
                          + ", " +  // text 
                          press.y() // obtain the y coordinate of the point  
                          + ")"     // text 
                        );  


      if (press.x() > 40 && press.x() < 40 + 10 && 
          press.y() > 10 && press.y() < 10 + 40 ) {
        System.out.println("Mouse clicked inside rectangle, program closing.");
        System.exit(0); 
      }  

      d.awaitMouseRelease(); // page 51
    }
  } 
} 
So now you have the basic framework of the assignment.