Modify the TwoButtons program so that it has four buttons: one for red, one for green, one for blue, and one for gray. Clicking on a button changes the frame to the corresponding color. The GUI will look like this after the green button has been clicked:
A sensible way to do this is to copy the chapter's example program and modify it. For a real challenge, try to do it from scratch.Click here to go back to the main menu.
Write a program that has only one button in the frame.
Clicking on the button cycles through the colors: red --> green --> blue
--> gray --> red --> and so on, one color change per click.
In addition to the
setBackground( Color.color ) method we have been
using, you will need the
Color getBackground() method to get
the current color.
Another way to do this (a somewhat cruder way) is to use an instance variable (of type Color) in the frame to hold the current color.Click here to go back to the main menu.
Write a program implements a game:
There are three buttons in the frame. Two of the buttons will cause the program to quit using
System.exit(0); the remaining button will change the frame to green (a win!) The winning button is different each time the game is played.
The easy way to do this (although it seems like cheating)
is to treat each button the same way.
ActionPerformed() method will not even check which
button was clicked.
It will pick a random integer from 0 to 2 and if the integer
happens to be 0 perform the "winning" action.
Otherwise, it will perform the "loosing" action.
This is similar to some electronic gambling devices in casinos,
where it appears to the user that there are "winning moves" but
in fact the machine actually ignores what the user has done and just
declares a "win" every now and then,
according to pre-assigned odds.
You will need to use the
You may recall seeing Random in a previous chapter. Click here to go back to the main menu.Random randNum = new Random(); // create a Random number object . . . int someInt = Math.abs( randNum.nextInt() )%3 // someInt gets a number from 0 to 2
Modify the program of Exercise 3 so that only one button exits the program. If the user clicks on any of the other two buttons, the frame just changes color. The user keeps clicking until the "loosing button" is clicked. The user's goal is to click as many times as possible.Click here to go back to the main menu.
Create a frame with ten buttons, labeled 0 through 9. To exit the program, the user must click on the correct three buttons in order, something like 7-3-5. If the wrong combination is used, the frame turns red.Click here to go back to the main menu.
Create a frame with two buttons, called Expand
When the Expand button is clicked, the frame expands by 10 percent.
When the Shrink button is clicked, the frame shrinks by 10 percent.
Do this in the
actionPerformed() method by using
Keep track of the current size of the frame with two instance variables
of type int.
When you increase them or decrease them by 10 percent,
you will have to use integer arithmetic or use a type cast.