Java Reference
In-Depth Information
resize windows, and so on. There are several GUI building tools that allow us to do so (e.g.,
the ones available with the NetBeans and Eclipse integrated development environments).
However, these tools are not covered in this textbook. Rather, the topic shows us how to
create robust GUI components from scratch by writing our own Java code. Many of the
components that we will create will have the added advantage that their size and placement
will not be fixed and will be determined automatically.
9.2 Creating Windows
The basic component of any GUI program is the window. In Java, there is a class called
Frame that creates a window. The Frame class is part of the AWT library, while the JFrame
class is part of the Swing library. In fact, all classes in the Swing library start with the letter
J .
As usual, we will introduce the different ways to create JAVA GUI using a game. Breakout
is a game that was developed by Atari in 1976. A layer of bricks covers the top third of the
screen. A ball travels across the screen, bouncing off the top and the side walls of the screen.
When a brick is hit, the ball bounces away and the brick is destroyed. The player loses a
turn when the ball touches the bottom of the screen. To prevent this from happening, the
player has a movable paddle to bounce the ball upward, keeping it in play.
This chapter describes how to display the different components of the Breakout game
(Figure 9.1). The actual Breakout game is finished in Chapter 11, where the ball movement
and mouse and keyboard interactions are addressed.
The first order of business is to create the window where all the action happens. A
typical design is to create a class that extends the JFrame class (i.e., a Swing window). Here
is a possible implementation of the main method of the Breakout game.
public class Breakout {
public static void main(String [] args) {
BreakoutFrame frame = new BreakoutFrame () ;
frame. setVisible( true );
}
}
Thefirstlineinthe main method creates a BreakoutFrame object (i.e., the window for
the Breakout game). The constructor of the BreakoutFrame class will create the window
and place the paddle, the ball, and the bricks in the window. By default, every Java window
is created to be initially invisible. This is done in order to allow the developer to populate
the window before it is displayed. The second line of the main method displays the window.
Note that unlike console programs, GUI programs do not terminate immediately after
all the code is executed. If a window is open, then the program will wait for the window to
be closed before it finishes execution, unless a command, such as System.exit(0) , forces
the program to terminate. The last command simply quits the program with exit code 0,
which means normal program termination. Note that even putting the command return
inside the main method will not terminate the program if a window is already created.
Usually, using the exit method to terminate a program is considered bad programming
practice. It is much better to wait for the user to close all the windows before the program
terminates. An exception to this rule is when something bad happens and the program does
not know how to proceed and the only alternative is to terminate the program.
 
Search WWH ::




Custom Search