Java Reference
In-Depth Information
11.6 Adding the Menus
Next, we will add menus for our program. We will add menus for starting/stopping the
game and for changing the color and speed of the ball. We will create the following two
enum types: for the possible value of the ball speed and ball color.
public enum BallColor
{
Red( Color .RED) ,
Blue(Color .BLUE) ,
Green(Color .GREEN) ;
private Color color ;
BallColor(Color color)
{
this . color = color ;
}
Color color() {
return color ;
}
}
public enum Ba l lSpeed
{
FAST( 1 ) ,
NORMAL ( 1 0 ) ,
SLOW(20) ;
private int speed ;
BallSpeed( int speed)
{
this . speed = speed ;
int speed() {
return speed ;
}
}
We set the ball color to red, blue, and green and the speed to fast, normal, or slow. Let
us first examine the new version of the BreakoutFrame class that shows the menus.
import javax . swing . ;
import java .awt. event . ;
public class BreakoutFrame extends JFrame
{
private static final int HEIGHT = 600;
private static final int WIDTH = 488;
private BreakoutPanel panel = new BreakoutPanel () ;
public BreakoutFrame () {
setDefaultCloseOperation(JFrame.DISPOSEON CLOSE) ;
displayMenu() ;
setLocation(100 , 100) ;
setSize(WIDTH, HEIGHT);
add(panel) ;
setResizable( false );
}
 
Search WWH ::




Custom Search