Java 1.4 Game Programming (Wordware Game and Graphics Library)

An application is a program that can either be a simple console window, which merely outputs text, or it can be a visual windowed program. We have so far been glued to the console window for our applications. In this section, we look at how we can create a windowed application in Java.

A Simple Java Application

To create a windowed application in Swing, you can use an object of type JFrame. We can do this by making our own class extend the JFrame class or contain a JFrame object. The JFrame class is defined in the Swing package, which is imported as javax.swing.*. Note that the equivalent component in the AWT package java.awt.* is simply called Frame. This is consistent with most of the GUI objects (i.e., all the Swing GUI objects are named the same as the AWT versions, except they are prefixed by the letter "J"). Let's now look at two very simple examples of how we can create a 400x300 pixel window with "My Application" in the title bar. They both have the same outcome, but the first extends a JFrame class, whereas the second just has a JFrame as a member of the class. Here are the two code listings.

Code Listing 8-1: Simple application (extending a JFrame)

import javax.swing.*; public class MyApplication extends JFrame { public MyApplication() { super("My Application"); setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { MyApplication theApp = new MyApplication(); } }

Code Listing 8-2: Simple application (JFrame as a member)

import javax.swing.*; public class MyApplication { public static void main(String args[]) { JFrame appFrame = new JFrame("My Application"); appFrame.setSize(400, 300); appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); appFrame.setVisible(true); } }

When we execute either of these two example applications, we will see that they create a window that looks like this:

Figure 8-1: Our basic application window

Let's now look at the first example, which extends the JFrame class in order to create the window. First, we need to include the Swing package, which is accomplished using the following import statement:

import javax.swing.*;

Now we declare our MyApplication class to extend the JFrame class, which will make it inherit all the members and methods of the JFrame class (if you are unsure about inheritance, refer back to Chapter 4). The class declaration can be seen in the following line of code:

public class MyApplication extends JFrame

Next, we need to declare a main method that is the entry point to the application code. All we need to do in this method is instantiate our MyApplication class so that the constructor will be called. The complete main method can be seen here:

public static void main(String args[]) { MyApplication theApp = new MyApplication(); }

In the constructor of MyApplication, we first make a call to the constructor of the super class (i.e., the JFrame class that we are extending) and pass a single argument that represents the title of our application. This can be seen here:

super("My Application");

Note you can also use the method setTitle of the JFrame to later change the title.

Next, we need to set the size of our application window, which we do by making a call to the setSize method, passing the width and the height as parameters, respectively. Note that the setSize method is a member of the JFrame class. This can be seen in the following line of code:

setSize(400, 300);

Then we set the default close operation for the application (e.g., what happens when the user clicks the "x" in the top-right corner of the window or presses Alt+F4 in Windows). For this, we specify that we wish the application to EXIT_ON_CLOSE. This can be seen in the following line of code:

setDefaultCloseOperation(EXIT_ON_CLOSE);

We will look at closing windowed applications properly in the next chapter, so do not worry about this section of code in this chapter.

Finally, we make a call to the setVisible inherited method. Passing true as a parameter informs the JFrame heavyweight object to make itself visible. This can be seen in this final line of code:

setVisible(true);

For the other example code, we do not extend the JFrame. Instead, we create a new JFrame object in the main method and assign it to our member variable appFrame. Note again how we pass the title of our application as the constructor. This can be seen in the following line of code:

JFrame appFrame = new JFrame("My Application");

Once we have our object instantiated, we can then use the object to call the setSize, setVisible, and setDefaultCloseOperation methods in a similar way to the first example code. This can be seen in the final three lines of code, as well as the default close operation line.

appFrame.setSize(400, 300); appFrame.setVisible(true); appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Note here that the EXIT_ON_CLOSE definition is a public constant member of the JFrame class, and hence we have to specify it as such (i.e., JFrame.EXIT_ON_CLOSE). In the previous code listing, we extended the JFrame, and hence the EXIT_ON_CLOSE variable was therefore a member of our own class as it extended the JFrame, inheriting this member.

Категории