Lecture 8 - Friday, September 13th, 2002 - 810:061 Mark Jacobson
 

Conditionals

We start out by looking at a program which allows the user to drag a box around the screen. T he idea is that if the user presses the mouse down on the box and then drags the mouse around then the box should move just as much as the mouse does.

There is a problem. We only want to move the box if the user actually pressed the mouse down on the box. We need to be able to detect that and we need to make sure that we only drag the box if the mouse was on the box.

Luckily, we have a method for graphic objects which allows us to detect whether a point is in the object.

    item.contains(point)

will have value true if point is in the bounding box of item, and has value false otherwise. In order to write this program, we want to write something like:

    if item.contains(point) then

        move the box the appropriate amount


Notice that if we use contains with an oval, it used to only 
tell us if the point is inside the bounding rectangle, not within the oval.
The objectdraw package latest version correctly detects whether the 
click was inside the oval or not!  :-)
Try out this Humpty Dumpty example applet.

Let's see how we can do this in Java.

The values true and false are the only values of data type boolean.

Java contains several relational operators which give values of type boolean. The operators include:

        < , > , ==, <=, >=, !=

Notice how we must use == in order to test for equality, because = has already been used for assignment. The symbol != stands for inequality. Thus we can write x > 4, y != z+17, x+2 <= y. Depending on the values of the variables, each of these expressions will evaluate to either true or false. That is, each of these expressions evaluates to a value of type boolean.

The message contains, introduced above and found on the graphics cheatsheet, also returns a value of type boolean.

As you might expect, Java's if statement determines whether or not to execute a block of code depending on the value of a boolean expression.

The syntax of Java's if statement is as follows:

    if (booleanExpression)

    {

       statements

    }

In the case of dragging the box around, we'll write a statement:

    if ( box.contains(lastPoint))

    {

       box.move( point.getX() - lastPoint.getX(),

                 point.getY() - lastPoint.getY() );

       lastPoint = point;

    }

where lastPoint is the position of the mouse before the drag.

Demo. MouseDrag

While this program works, it is not ideal, because every time we move the mouse, we must check to see whether lastPoint is inside box. However, if it is in there the first time, it will be there each successive time. As a result, it would be better to check this once, and then simply remember the answer.

We can do this if we remember the answer in a variable boxGrabbed, of type boolean. Each time we do a mouse drag, we will still need to check whether we originally pressed the mouse on the box, but it is much simpler to check what the answer was, rather than recalculate it each time. See the code below:

Demo. Better MouseDrag

The Pong game

The classic pong game is a good example of computer animation that is easily handled in Java using our graphics primitives. In the demo below, we only show you the code for handling the pong paddle. We will put off a description of the bouncing ball for several weeks until we have a bit more experience in programming.

Demo. Simple Pong

While the above program works pretty well, if the user moves the mouse too far to the right or left, the paddle goes out of the court. That isn't very realistic, so we'd like to determine when that happens and stop the paddle at the edge of the court.

In the onMouseMove method of the following example, we pick up when the mouse has moved off the left edge of the court and stop the paddle. More completely, if the mouse is not off the left edge, we move the paddle appropriately, otherwise we stop it at the left edge.

Java allows us to express this kind of conditional behavior using an if-else statement with syntax as follows:

    if (booleanExpression)

    {

       // stuff to do if booleanExpression is true

    } 

    else 

    {

       //stuff to do if booleanExpression is false

    }

In our particular case it will look like:

    if (point.getX() > COURT_LEFT)

    {

       // move paddle to mouse ..

    }

    else

    {

       // move paddle to left edge ..

    }

Demo. Half-fixed Pong

It is yet more complicated if we wish to ensure that the paddle doesn't go off either side of the court. Here is the logical structure:

    if (point.getX() < COURT_LEFT)

    {

       // move paddle to left edge ..

    }

    else if (point.getX() < COURT_LEFT + COURT_WIDTH - PADDLE_WIDTH)

    {

       // move paddle to right edge ..

    }

    else

    {

       // move paddle to mouse ..

    }

Notice how we can make the else clause contain another if-else clause. In this way, we can stack up as many alternatives as we need to solve our problem.

Demo. Fixed Pong

Be sure to read Chapter 3 of Williams College Manuscript in order to see many more examples of the use of if-else statements.