Java Reference
In-Depth Information
You should also display the current chosen color as a text somewhere inside the panel,
where the initial background color is blue. The getHeight and getWidth methods
can be called on a panel object to determine its height and width in pixels. The user
should also be able to change the background color to red, green, or blue by pressing
the r, g, and b keys on the keyboard (the color should be changed to red, green, or
blue, respectively). Remember to call the setFocusable method on the panel.
8. Write an application that draws ellipses. The ellipse should start drawing when the
left mouse button is pressed. The ellipse is completed when the left mouse button
is released. Create an ArrayList to store the ellipses. The paintComponent method
should display the ArrayList of ellipses and the current ellipse (i.e., the ellipse that
is currently being drawn). The content on the window should not change when the
window is resized. Pressing the right mouse button should clear the window.
10.9 Lab
Find a pretty image from the Internet and use an image editor to break it down into 9
pieces (for example, you can use the application Paint in Windows). Display the 9 images
in a 3x3 grid in a random order. Add a mouse listener. Allow the user to swap two images
by clicking on them. The goal of the game is to re-create the original image. Display an
appropriate message when the user wins.
10.10 Project
For this project, you will need to implement the Reversi game. The game is played on an
8 by 8 board. The starting position of the game is shown in Figure 10.4. The game is played
by two players. At every turn, a player can place at most 1 disk. When a disk is placed, all
disks of the opponent's color that are in a straight line and bounded by the disk just placed
and another disk of the current player's color are turned over to the current player's color.
A move is valid only if it turns over some disks. If a player does not have a valid move, then
they pass and the other player plays again. The objective of the game is to have the most
disks at the end of the game. One of the players is the computer and the other player is a
human. Choose randomly who starts first. During a human's move, if a cell on the grid is
valid, then turn it to green when the user hovers over it with the mouse. This will signal to
the human player that this is a valid square to play. Alternate moves between the computer
and the human, unless one needs to pass. Display an appropriate message when the game
ends. The game ends when the board is filled. The player with the most disks at the end
wins. Note that it is possible that the game is a draw.
The Artificial Intelligence (AI) of this game is tricky. To simplify it, follow these basic
rules in this order.
1. If the computer can go in one of the four corners, it should always do so.
2. The computer should avoid going in the squares that are adjacent to a corner when
the corner is free.
Search WWH ::

Custom Search