Programming via Java User interaction

Similar documents
CS 106A, Lecture 14 Events and Instance Variables

CS 106A, Lecture 14 Events and Instance Variables

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

SUBCLASSES IN JAVA - II

Programming via Java Subclasses

Programming via Java Defining classes

MORE ON LOOPS IN JAVA

Assignment #3 Breakout!

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Chapter 6 Game Programming I

Solutions for Section #4

File: GFace.java /* * File: GFace.java * This class implements a face as a GCompound. */

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Data Structure Design II Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Assignment A7 BREAKOUT CS1110 Spring 2011 Due Fri 6 May 1

Object-Oriented Language Development Company

Chapter 8 Methods II

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

Solution to Section #7

Computer Graphics. Interaction

CS106A Review Session

Interactive Graphics

CISC 1600, Lab 2.2: Interactivity in Processing

Solutions for Section #2

CISC 1600, Lab 3.2: Interactivity in Processing

Interactive Graphics. Eric Roberts Handout #23 CS 106A January 25, 2016 Interactive Graphics. Computer Graphics and the Utah Teapot.

Solutions for Section #2

AP CS Unit 12: Drawing and Mouse Events

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

AP Computer Science Unit 13. Still More Graphics and Animation.

Visual C# Program: Simple Game 3

CS106A Review Session. Monday Oct. 31, 2016 Nick Troccoli

Animation. Piech, CS106A, Stanford University

Chapter 2 ACM Java Graphics

The AWT Event Model 9

Basic Computer Programming (Processing)

CS Programming Exercise:

Programmierpraktikum

CS 170 Java Programming 1. Week 12: Creating Your Own Types

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

CS/120 Final Exam. Name

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Event Driven Programming

1 Getting started with Processing

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Lesson 18: Animation. Computer Programming is Fun!

Graphical User Interfaces 2

Exploring Processing

PESIT Bangalore South Campus

Graphical User Interfaces 2

Graphical User Interfaces 2

5.1. Examples: Going beyond Sequence

CS 170 Java Programming 1. Week 9: Learning about Loops

SSEA Newbies Handout 09 Summer 2012 August 17 th, 2012 Assignment 4: Bouncing Ball and Bizz Bazz Buzz

CSE 143 Au04 Midterm 1 Page 1 of 9

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Chapter 9 Arrays & ArrayLists

Revisiting acm.graphics

Express Yourself. Writing Your Own Classes

Slide 1 CS 170 Java Programming 1

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Chapter 4 Defining Classes I

Variables, Types, and Expressions

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

CSE 331 Software Design & Implementation

Programming Exercise

CMSC 113: Computer Science I Review Questions for Exam #1

CS 106A, Lecture 11 Graphics

[ the academy_of_code] Senior Beginners

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

G51PRG: Introduction to Programming Second semester Applets and graphics

GRAPHICS PROGRAMMING. LAB #3 Starting a Simple Vector Animation

Expressions, Statements, and Control Structures

CSE 413 Winter 2001 Midterm Exam

GUI Event Handlers (Part I)

Solutions to Section #7

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Lecture 19 GUI Events

Getting Familiar with ACM_JTF

Dragging. Dragging Rectangles. Tuesday, October 21, October 21, User interface effect that we want:

Slide 1 CS 170 Java Programming 1 Object-Oriented Graphics Duration: 00:00:18 Advance mode: Auto

Objects and Graphics

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

Assignment 2: Welcome to Java!

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Chapter 6 Introduction to Defining Classes

CS 106A, Lecture 27 Final Exam Review 1

Lecture 10 Declarations and Scope

Computer Science II - Test 2

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS 134 Programming Exercise 9:

What is Java? professional software engineering.

Twin A Design Pattern for Modeling Multiple Inheritance

ISY00245 Principles of Programming. Module 7

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Lab 8. An Introduction to Objects

Transcription:

Programming via Java User interaction We'll now consider programs to get information from the user via the mouse, as supported by the acm.graphics package. Along the way, we'll learn about two more general Java concepts that will be useful in the future: null references and instance variables. 13.1. Accepting mouse events To permit a program to allow a user interacts with the program via a mouse, the GraphicsProgram class defines several methods. void addmouselisteners() Tells this program to receive information about the mouse's behavior and by invoking the mouse methods as appropriate. void mousepressed(mouseevent event) Invoked when the user presses the mouse button while the cursor is within this window. The parameter event contains information about the mouse click. void mousedragged(mouseevent event) Invoked when the user drags the mouse that is, the user moves the mouse while pressing the mouse button down. The method will be invoked even if the cursor has exited this window, as long as the initial mouse button press occurred within this window. The parameter event contains information about the mouse's state. There are also mousereleased, mouseclicked, and mousemoved methods. These mouse methods have a parameter, which is an object of the MouseEvent class, defined in the java.awt.event package. This MouseEvent object contains information about the mouse; among the MouseEvent methods, the most important by far provide information about the mouse's position at the time of the event. int getx() Returns the x-coordinate where this mouse event occurred. int gety()

Returns the y-coordinate where this mouse event occurred. Figure 13.1 contains a simple program that allows some mouse interaction. The window initially appears blank; but when the user clicks the mouse within the window, a new circles appears at the click's location. Figure 13.1: The DropCircles program. 1 import java.awt.event.*; // for MouseEvent 2 import java.awt.*; // for Color 3 import acm.graphics.*; // for GOval 4 import acm.program.*; // for GraphicsProgram 5 6 public class DropCircles extends GraphicsProgram { 7 public void run() { 8 addmouselisteners(); 9 } 10 11 public void mousepressed(mouseevent event) { 12 double mousex = event.getx(); 13 double mousey = event.gety(); 14 GOval circle = new GOval(mouseX - 10, mousey - 10, 20, 20); 15 circle.setfillcolor(new Color(255, 0, 0)); 16 circle.setfilled(true); 17 add(circle); 18 } 19 } As you can see, the run method leaves the window empty, but it invokes addmouselisteners so that mousepressed will be invoked whenever the user clicks the mouse. When the user does click the mouse, the mousepressed method determines the mouse's x- and y-coordinates, and it places a solid red circle centered at that spot. 13.2. The null reference Now we will try to change DropCircles so that no new circle is added when the user clicks within an existing circle. In order to do this, though, we need to need to study a new concept that we haven't seen before: the null reference.

In any place within a program where a reference to an object can appear, the null value can be used instead to indicate instead the absence of any object. For example, if we have a GOval variable named ball, and for the moment we don't want it to refer to any GOval object at all, we can assign it to be null. ball = null; Later in the program, we may ask the GOval named by ball to perform a method, such as move. ball.move(3, 4); The compiler will merrily accept this, and the computer will start to execute the program. If ball is null when the computer reaches this statement, though, the computer will complain that it is supposed to tell the GOval object to move, but in fact ball doesn't refer to a GOval object at all. It calls this a NullPointerException. Exception in thread "main" java.lang.nullpointerexception A program should never request that null perform any methods, since null is the nonobject. You might wonder why it's useful to have a null reference at all. The getelementat method in GraphicsProgram illustrates such a situation. GObject getelementat(double x, double y) Returns the topmost graphical object containing the point (x, y) in this window. If no objects contain that point, the method return null. The designers of the GraphicsProgram class decided that the getelementat method would be a useful method to have, but they had to confront this question: How should the method work in the situation where no objects contain the query point? They needed some way of returning nothing and Java's null reference is perfect for this. As you might guess, the getelementat method is quite relevant to our goal of modifying the DropCircles program so that it adds a new circle only when the user clicks outside the existing circles. Figure 13.2 contains such a program, with only a few lines changed from before. Figure 13.2: The DropDisjoint program.

1 import java.awt.event.*; 2 import java.awt.*; 3 import acm.graphics.*; 4 import acm.program.*; 5 6 public class DropDisjoint extends GraphicsProgram { 7 public void run() { 8 addmouselisteners(); 9 } 10 11 public void mousepressed(mouseevent event) { 12 double mousex = event.getx(); 13 double mousey = event.gety(); 14 GObject cur = getelementat(mousex, mousey); 15 if(cur == null) { 16 GOval circle = new GOval(mouseX - 10, mousey - 10, 20, 20); 17 circle.setfillcolor(new Color(255, 0, 0)); 18 circle.setfilled(true); 19 add(circle); 20 } 21 } 22 } As you can see, its mousepressed method now invokes getelementat (line 14) to see what object, if any, is at the point clicked by the user. If the method returns null, then the program knows there is no object at that point, and so within the if statement's body the program proceeds to add a circle into the window as before. 13.3. Instance variables Now let us examine Figure 13.3, which allows the user to both to create new circles by clicking where no circles exist already and also to drag existing circles around the window. Figure 13.3: The DragCircles program. 1 import java.awt.event.*; 2 import java.awt.*; 3 import acm.graphics.*; 4 import acm.program.*; 5

6 public class DragCircles extends GraphicsProgram { 7 private GObject cur; 8 9 public void run() { 10 addmouselisteners(); 11 } 12 13 public void mousepressed(mouseevent event) { 14 int mousex = event.getx(); 15 int mousey = event.gety(); 16 cur = getelementat(mousex, mousey); 17 if(cur == null) { 18 GOval circle = new GOval(mouseX - 10, mousey - 10, 20, 20); 19 circle.setfillcolor(new Color(255, 0, 0)); 20 circle.setfilled(true); 21 add(circle); 22 cur = circle; 23 } 24 } 25 26 public void mousedragged(mouseevent event) { 27 double mousex = event.getx(); 28 double mousey = event.gety(); 29 double curx = cur.getx() + cur.getwidth() / 2; 30 double cury = cur.gety() + cur.getheight() / 2; 31 cur.move(mousex - curx, mousey - cury); 32 } 33 } This program uses an important new concept called an instance variable, declared in line 7. So far all of our variables have been declared inside methods, like mousexon line 14. These variables, called local variables, exist only for the duration of the method; after the method completes, any value associated with the local variable is lost. By contrast, when a variable is declared outside all methods, like cur on line 7, that variable is an instance variable, and the value for an instance variable's value persists long-term, between method invocations. You may be wondering: Why the word private? This word does not make cur an instance variable: It is an instance variable because it is defined outside all methods. In fact, we could omit the word private, and cur would still be an instance variable, and the program would work just as before. Nonetheless, for stylistic reasons, instance variables should always be

declared private. The compiler will prevent local variables from being declared private. We'll study the notion of privatenessmore in Section 14.3. When the user presses the mouse button, the computer will execute mousepressed, and in line 16, the method will assign cur to reference the circle the user has selected. If the user clicks outside any existing circles, getelementat will return null, and mousepressed's if statement will execute. The if statement will create and add a new circle and assign cur to that. Because cur is an instance variable, its value will persist even after mousepressed completes. Thus, when the user drags the mouse with the button still pressed, and the computer invokes the mousedragged method, cur will still have the value it was assigned in mousepressed. The only job of mousedragged is to reposition the circle referenced by cur at the mouse's current location. When the user releases the mouse button, the computer will stop invoking mousedragged as the mouse moves, since mousedragged applies only while the mouse button is down. Later, when the user presses the mouse button again, the mousepressed method will assign cur to refer to the circle then selected (or the new circle thus created), so that the later mousedragged invocations will affect that circle instead. By now, you may be in the habit of always declaring all variables in each method the first time that they appear, so that you may be inclined to change line 16 to declare cur as a GOval. That is, you may be tempted to write GOval cur = getelementat(mousex, mousey);. Resist that temptation. If we write line 16 this way, then we would be defining a separate local variable named cur which hides the instance variable of the same name. The program would still compile, but mousepressed would work with the local variable cur rather than the instance variable cur. As a result, the program would never alter the instance variable cur from its default value, which is null. Consequently, mousedragged would attempt to tell null to perform thegetx method, which would lead to a program failure via a NullPointerException. You may be tempted to declare all variables as instance variables, so that you don't have to declare any variable names multiple times. This is dangerous, because methods can end up interacting in peculiar ways. For instance, one method may invoke another, and they both happen to use the same variable name, but there is no intention of the methods interacting in this way. The program will fail.

Not only is declaring instance variables needlessly dangerous, but just as importantly it is very poor programming style. Only use instance variables where the program genuinely needs to remember their value long-term. 13.4. Another example: Pong Let's look at a completely different example: Solitaire Pong. This is a simple game where a ball bounces around the window, except it will exit the window when it reaches the window's left edge. The user's mouse controls the vertical position of a paddle near the window's left edge. The goal of the game is for the user to keep the ball within the window as long as possible. Figure 13.4: Running Solitaire Pong. To accomplish this, we'll need a run method and a mousemoved method. The run method will add the paddle and ball to the window and manage the ball's movement similar to the BouncingBall program (Figure 7.2). The mousemoved method will read the mouse's y- coordinate and move the paddle to that location. Because both runand mousemoved will need work with the same paddle, we will need an instance variable to refer to that paddle. Figure 13.5 contains an implementation of these ideas. Figure 13.5: The Pong program. 1 import acm.program.*; 2 import acm.graphics.*; 3 import java.awt.*; 4 import java.awt.event.*; 5

6 public class Pong extends GraphicsProgram { 7 private GRect paddle; 8 9 public void run() { 10 paddle = new GRect(10, 10, 10, 100); 11 paddle.setfilled(true); 12 add(paddle); 13 addmouselisteners(); 14 15 GOval ball = new GOval(25, 25, 20, 20); 16 ball.setfilled(true); 17 ball.setfillcolor(new Color(255, 0, 0)); 18 add(ball); 19 20 double dx = 3; 21 double dy = -4; 22 while(true) { 23 pause(25); 24 double ballleft = ball.getx(); 25 double ballymid = ball.gety() + ball.getheight() / 2; 26 double paddleright = paddle.getx() + paddle.getwidth(); 27 double paddletop = paddle.gety(); 28 double paddlebot = paddle.gety() + paddle.getheight(); 29 if(ballleft + ball.getwidth() >= getwidth() 30 (ballleft <= paddleright && ballleft > paddleright + dx 31 && ballymid >= paddletop && ballymid < paddlebot)) { 32 dx = -dx; 33 } 34 if(ball.gety() + ball.getheight() >= getheight() 35 ball.gety() <= 0.0) { 36 dy = -dy; 37 } 38 ball.move(dx, dy); 39 } 40 } 41 42 public void mousemoved(mouseevent event) { 43 double paddley = event.gety() - paddle.getheight() / 2;

44 double paddlemax = getheight() - paddle.getheight(); 45 if(paddley < 0) paddley = 0; 46 if(paddley > paddlemax) paddley = paddlemax; 47 paddle.setlocation(10, paddley); 48 } 49 } One of the most complicated portions of the program is the first if condition in run, for determining whether the ball should bounce the other way horizontally. The first portion of this is easy: If the ball has passed the window's right edge, then it should bounce. But it should also bounce if the ball has struck the paddle's right edge, and that is much more complex. As illustrated in Figure 13.6(a), the ball has struck the paddle's right edge if the ball meets all of the following criteria: the ball's left edge has crossed (is to the left of) the paddle's right edge, the ball's left edge has not gone too far past the paddle's right edge, the ball is below the paddle's top, and the ball is above the paddle's bottom. These four criteria appear in the program on lines 30 31. Figure 13.6: Testing whether a ball has struck a paddle. (a) Hit (b) Miss Our program simplifies the paddle-ball logic by ignoring an important boundary case, illustrated in Figure 13.6(b): If the ball strikes a corner of the paddle, it will simply continue through the paddle, since the y-coordinate of ball's center is outside the range of y- coordinates that the paddle occupies. A more industrial-strength Pong program would have more sophisticated logic for bouncing off the paddle's corner. Of course, an industrialstrength Pong would include many more features, too, like keeping score somehow,

replacing the ball after it has gone off the window, and restricting the paddle's velocity. Such features are left for you to enjoy adding to the simple Pong program. Source: http://www.toves.org/books/java/ch13-interact/index.html