415.111 Monday 26th May Java Lecture 10 Overview Interactive Graphics Examples Demos Explorer’s Guide handout: Chapter 8 to end.
Bentley’s Programming Pearls If code and comments disagree, then both are probably wrong.
Bentley’s Programming Pearls Testing can show the presence of bugs, but cannot prove their absence.
Making graphics programs more interactive We can now extend our graphics programs by making them more interactive. We can get the x and y coordinates of the point in the Applet Window where the mouse was clicked by using: Mouse.getX() Mouse.getY().
Loops and Mouse Techniques p 177 import java.awt.*; import java.applet.*; import java.awt.event.*; public class DisplayClickPoints extends Applet { public void init() { new Mouse(this); } public void paint(Graphics g) { // * * * * * * * * * * * * * * * * * * * * * * * * * *
// Draw the coordinates of the mouse click at the click point int h, v; h = Mouse.getX(); v = Mouse.getY(); g.drawString("(" + h + ", " + v + ")", h, v); // * * * * * * * * * * * * * * * * * * * * * * * * * *
// Draw smiley faces at up to 10 click points g.drawString( "Smiley", 30, 30 ); int x,y; int counter; counter = 0; do { x = Mouse.getX(); y = Mouse.getY(); // Draw a Smiley centred at the point (x, y) g.drawOval( x-20, y-20, 40, 40 ); g.drawOval( x - 10, y, 2, 2 ); g.drawOval( x+ 10, y, 2, 2 ); g.drawOval( x, y + 10, 4, 3 ); counter = counter + 1; } while (counter != 10);
Introducing Buttons p Introducing Buttons p 178 int top, left, width, height; int x, y; // initialize left = 20; top = 20; width = 40; height = 20; // Draw a Rectangle and put HI in it g.drawRect(left, top, width, height); g.drawString( "HI", left + 5, top + height - 5 );
Introducing Buttons p Buttons contd // Get the mouse coordinates x = Mouse.getX(); y = Mouse.getY(); // if the mouse is inside the rectangle display BYE if (x > left && x < (left + width) && y > top && y < (top + height)) { g.clearRect(left, top, width, height); g.drawRect(left, top, width, height); g.drawString( "BYE", left + 5, top + height - 5 ); }
Exercise 1 p179 Write a program that displays 3 buttons as shown in the Applet Window below: When the user clicks in one of the buttons a price is displayed. (It is your job to think of appropriate prices in this age of inflation!)
Introducing Buttons p GuessIt3 p180 // program to play a simple guessing game int x, y, randomNbr, userNbr; g.drawString( "Guess it", 30, 30 ); int left1,width, height, top, counter, left; left1 = 30; width = 40; height = 20; top = 40;
GuessIt3 contd Introducing Buttons p counter = 1; left = left1; // Display the buttons (1, 2, and 3) do { g.setColor(Color.blue); g.fillRect(left, top, width, height); g.setColor(Color.black); g.drawString("" + counter, left + width / 2, top + height / 2); left = left + 2 * width; counter = counter + 1; } while (counter <= 3);
Introducing Buttons p GuessIt3 contd // Get a random number between 1 and 3 inclusive randomNbr = (int) (3 * Math.random() + 1); // Get the mouse coordinates x = Mouse.getX(); y = Mouse.getY();
Introducing Buttons p GuessIt3 contd userNbr = 0; if (x > left1 && x < left1 + width && y > top && y < top + height) userNbr = 1; else if (x > left1 + 2* width && x < left1 + 3 * width && y > top && y < top + height) userNbr = 2; else if (x > left1 + 4 * width && x < left1 + 5 * width && y > top && y < top + height)` userNbr = 3;
GuessIt3 contd if (userNbr == randomNbr) { g.setColor(Color.red); g.fillRect(100, 80, 100, 40); g.setColor(Color.black); g.drawString("You got it!", 120, 100); }
Exercise 2 p 181 Write a program segment to simulate the game Rock, Scissors, and Paper. In this game for two players each makes a choice of Rock / Scissors / Paper (and indicate their choice by simultaneously displaying appropriate hand signs). Rock wins over Scissors Scissors wins over Paper Paper wins over Rock
Write a program segment to simulate throwing a coin. Exercise 3 p 183 Write a program segment to simulate throwing a coin.
Modify the program so that the computer generates the tosses Exercise 3 Extension Modify the program so that the computer generates the tosses and the user indicates his "guess" by pressing one of the buttons. Indicate incorrect guesses by colouring the circles grey. Note: The Ex3Template file for you to place your program segment in is located in the Chapter 8-progs folder.
Homework Explorer’s guide: WHOLE BOOK (in prep for exam)