2. Write the Thermometer applet, which reads a temperature in degrees Fahrenheit and draws a thermometer with the correct number of degrees filled in. You should draw the thermometer's shape as a tall thin rectangle with a small circle at the bottom, with the circle and the correct proportion of the rectangle in red. Show the temperature to the right of the top of the red portion of the thermometer. You may assume the temperature will be in the range 0--100.
3. Draw the mouse's maze in a size proportional to the size of the applet's window. You need not change the size of the mouse, but if you are ambitious, you can change its size by calling the Image instance method getScaledInstance:
4. Draw the International Olympic Committee logo .. The colors of the circles, left to right, are blue, yellow, black, green, and red.
5. In Section 2.8, you wrote a program to calculate the price of a coffee order (see page 47 for the complete application). Turn this application into an applet.
7. You can get a better idea of how colors work in Java by writing an applet to display various red-green-blue (RGB) combinations. Recall that a new color can be created by giving the Color constructor three values, each between 0 and 255, representing the red, green, and blue components. Write an applet with three text fields, labeled Red, Green, and Blue, and a (filled) rectangle. When the user enters a number between 0 and 255 in any of the three fields, a new color is created using the values in those three fields and is displayed as the fill color of the rectangle (rather, a new filled rectangle is drawn over the existing one). In this applet, all three text fields are reactive components, and all should be registered accordingly. However, the actionPerformed method does not need to know which field had a new value entered in it, since it just reads the value in all three fields; thus, the complexities arising from multiple reactive components do not occur.
14. A (very) simple picture-drawing applet can be written in which the user indicates, with text fields, what type of shape is to be drawn and where it is to appear. Write such an applet. Include a text field for the type of shape---0 for a circle, 1 for a square---and text fields for the center of the shape and its diameter (or the length of a side of the square). (All fields should be filled in by tabbing from one field to another; actionPerformed is called only when the Enter key is pressed.) Don't forget to redefine update, as described in Chapter 3, so that all the shapes show up in the applet's window. If one of the values entered in a text field is invalid, place a label in the applet indicating the error; that label's text should be reset back to the empty string (using errorMessage.setText("")) when actionPerformed is invoked.
2. In Exercise 3 on page 105 you wrote a switch statement to compute the postage rate for an item, given the weight of the item and the kind of mail class. Write an applet to compute and display the postage rate. Use a TextField to input the weight, and use four grouped radio buttons to select the class of mail.
3. In Exercise 7 on page 77, you wrote an applet in which users entered RGB values (three numbers) and saw the corresponding color. Add to that applet six buttons, labeled Less red, More red, Less green, and so on. When one of these buttons is pushed, the value in the corresponding TextField is incremented or decremented by ten, and the new color displayed.
6. Write an applet that shows a clock face, initially set to 8:20, and three buttons, labeled One hour, Ten minutes, and One minute. Each time a button is pressed, the clock advances by that much time. (Note. Microsoft Internet Explorer does not work properly with this applet.)
7. Exercise 8 on page 106 described the children's game of Cootie. Turn that game into an applet. You will have one text field to enter the last die throw. You will have instance variables to remember what parts of the Cootie have been drawn, so that you can tell what new part to draw, if any. Since this is a two-player game, you will also need an instance variable to indicate whose turn it is, so that you can announce the correct winner.
5. Write a tic-tac-toe-playing applet. ... You should start with the tic-tac-toe board drawn in the applet's window with an X in the upper-left corner. There should be three buttons: ``down'' should move the X down (or back up to the top row if it is on the bottom row); ``right'' should move the X to the right (or back to the left column if it it is in the right column); and ``okay'' should leave the X where it is and go on to the next move. The next move should begin by placing O in a free space and letting the ``down'' and ``right'' keys move it to the next free space in the indicated direction. Again, pressing the ``okay'' button ends O's move. The game should continue like this until X or O wins, or all spaces are filled, at which time an appropriate label should be displayed.
2. Write a tic-tac-toe-playing applet similar to the one in Exercise 5 on page 160, but with this difference: the user indicates the next move by clicking the mouse in the desired empty square instead of pushing buttons.
3. Write an applet similar to the one in Exercise 4 on page 159, but using the mouse to indicate how to move or reshape the rectangle. This applet should have just one button, the rotate button, and it should start, as before, with a square drawn in the middle of the window. The square is rotated by pressing the rotate button, but it is moved or resized by giving two mouse clicks, as follows: Assume the mouse clicks are at points p and q, respectively. If p is ``near'' one of the rectangle's corners, then this is a reshape operation, and q is the new location of that corner. Otherwise, if p is ``near'' one of the rectangle's sides, then this is also a reshape operation, but one that moves that entire line, either vertically or horizontally; if the line is a horizontal line, then q's y-coordinate is the y-coordinate to which that line should be moved; if it is a vertical line, q's x-coordinate gives the x-coordinate. If p is not near a corner or a line, but is inside the rectangle, then this is a move operation; q gives the location to which the rectangle should be moved; specifically, the rectangle should be moved so that q is in the same relative location with respect to the rectangle as p was before the move. Finally, if p is outside the rectangle, the mouse clicks should be ignored.
4. A somewhat more difficult version of Exercise 3 is to use ``clicking and dragging'' to effect the moves and reshapes. That is, to move a corner of the rectangle, you should click and hold the mouse key, move the mouse to the desired new location of the corner, and release it. This should work similarly for reshaping on a side and for moving. Furthermore, you should show the rectangle changing shape and moving as the mouse is dragged.
11. Write an applet to draw regular polygons. The applet should have a single text field. When the user enters a positive integer n in the text field, the applet draws an n-gon in its window.
12. Write a method
void drawDottedLine(Graphics g, int x0, int y0, int x1, int y1)that draws a dotted line from point (x0, y0) to point (x1, y1). Using this method, modify the code from Section 3.12 (page 70) to draw the mouse's maze with dotted lines spanning the openings between cells (as shown in the pictures in Chapter 1).
8. Write an applet in which a ball bounces around within the applet's window. When the circle hits a side of the applet's window, change the direction.
2. Animate the clock applet you wrote for Exercise 6 on page 132, as follows: When the time is incremented, instead of just showing the new position of the minute and hour hands, show the hand moving to that new position. (Note. Microsoft Internet Explorer does not work properly with this applet.)
10. Create a freehand-drawing applet. Start with an applet window that is empty except for three buttons, Clear, Undo, and Undo last line. When the user clicks-and-drags the mouse, the line it traces out should be drawn (and stored in an arrray), ending when the mouse button is released. The user should be able to draw a many lines as he or she wishes, subject to a limit on the number of points the applet can store (say, 10,000). The Clear button should clear the screen and empty the array; the Undo button should erase the last point that was added; the Undo last line should erase the entire last line drawn. The Undo buttons can be pressed multiple times, erasing more and more each time. Of course, at any time the user can begin drawing a new line by clicking and dragging.
11. Modify the RandomCircles applet from Section 3.7 (page 73) to draw circles filled with a specified color. Use the idea of the ColorTest applet (Exercise 4 on page 138) to let the user specify a color. Specifically, the applet should have six buttons: Circle, More (which toggles to Less and back), Red, Green, Blue, and Bright. The Circle button places a random circle in the applet, filled with the current color. Whenever any of the other buttons is pressed, this indicates a change in the color, and all the circles should be redrawn in the new color.
2. Reprogram the Body Mass applet above so that the message ``This is not considered high.'' is displayed in blue, 16 point Helvetica type, while the message ``This is considered high.'' is displayed in red, 40 point bold Helvetica type. Be sure to resize the applet when increasing this message size. (Note. You can see some of the effects using a browser, but you can resize and see other effects if you run this using appletviewer.)
3. Modify our CalendarApplet so that today's date is enclosed in a rectangle. (See Exercise 4 below.)
4. Modify our CalendarApplet so that the first row of the calendar body ("Sun Mon ... Sat") is displayed in magenta, bold, 16 point Helvetica font.
void drawDottedPolygon(Graphics g, Point[] points)
10. Write applets to draw ``bar charts.'' Given an array of integers (entered by using a TextField, after which the Draw Bar Chart button must be pressed to create the bar chart), the values should be shown by vertical bars placed side-by-side. The height of the bars should be scaled so that the tallest one fits just inside the applet's window without hitting the buttons; their width should be calculated so that all the bars fill the window with a small gap on each side. Write three variants.
1. Sometimes a superclass acts as a ``conduit,'' in which a client invokes
a method in the superclass; that method not only invokes a method that
is defined in a subclass, but it also takes some additional action or records
some information.
Add such an instance method to the Mouse
class--- public void makeRecordMove()---which
simply invokes the method makeMove(),
and then records the new location and direction for the mouse. You'll want
to define a list class to record the configurations for the mouse. The
client of Mouse (namely MouseMaze)
should be changed to invoke the instance method makeRecordMove()
instead of makeMove(). When the
mouse has completed its navigation, the client should print a summary of
the mouse's moves. (See Exercise 2 below.)
2. Now that the base Mouse class can monitor its subclasses (acting like a good ``parent'' to its ``children''), we can allow the superclass to intervene when something bad happens. Namely, you should modify makeRecordMove so that it can prevent the mouse from running in circles. First, define a new protected method void backup(). Now when makeMove() returns to makeRecordMove() with a new configuration (location and direction) that the mouse has already experienced, makeRecordMove() should refuse that suggestion, and should invoke another method---makeAlternativeMove(int attempt)---which will choose a different move. The makeRecordMove() method may similarly reject that suggestion, and ask makeAlternativeMove(int attempt) for another. If after three tries makeAlternativeMove(int attempt) is unable to find a suitable move, then makeRecordMove() should cause the mouse to back up.
5. Again, the Smarter Player isn't so clever! A much stronger playing strategy involves looking ahead more than one move. Each move that a player looks ahead is called a ply. For example, an Expert Player might utilize two ply look-ahead. The evaluation of a move is determined by the situation that will exist after that move, and after the opponent makes a move in response. Three ply and four ply look-ahead carry this idea even further!
In general, n-ply look-ahead involves building a game tree, with the present Board leading to several successor Boards (one for each of the possible moves), and each of those Boards leading to several successors, and so on.
At the bottom of such a game tree, each Board is evaluated using a static stone-counting scheme such as that used by the Smart Player in Exercise 4. At the level above those terminal Boards, the opponent will presumably select his best move by choosing the minimum value of the Boards below. At the level above that, the Expert Player selects the maximum, etc. This is the strategy if the look-ahead is an even number. If it is an odd number, then the Expert selects from bottom most Boards, and the role of minimizing and maximizing is reversed.
Thus, the Expert Player implements a pair of mutually recursive evaluation methods, one to maximize results, and one to minimize results: ... For obvious reasons, this technique is called a min-max game-playing strategy. Program such an Expert Player.