| CSCI A201/A597Lab Notes 8 Spring 2000 |
A few pointers:
element package. Make sure you
have it on your floppy.
DrawingWindow. Make sure you read
about it in your book page 43, chapter 2.
Develop your program in stages:
erase and exit
x and y of the points where the mouse
is clicked
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.