Why we use System in Java

23.2 Basics of graphic output

23.2.1 Creating a window

In order to be able to use the graphics capabilities of Java, the package must be integrated. This is best done with the help of the following statement at the beginning of the class definition:

All classes from the package are then available.

In order to output graphic elements, the application requires a window to which the output operations can be applied. While a standard window is automatically made available when programming an applet, a application create their own windows. Since communication with a window is handled using a number of callback methods, a window class is usually not simply instantiated. Instead, it is usually necessary to derive your own class from one of the existing ones and to implement the required interfaces.

Typically, either the class or the class derived from is used to derive your own window class. Since it is primarily used to create dialog boxes that communicate with the user via the components they contain, we want to postpone their use until Chapter 31. The most important class for outputting graphics in Java applications is.

To create a simple window and display it on the screen, a new element of the class must be created, brought to the desired size and made visible by calling the method:

001 / * Listing2301.java * / 002 003 import java.awt. *; 004 005 class Listing2301 006 {007 publicstaticvoid main (String [] args) 008 {009 Frame wnd = new Frame ("simple window"); 010 011 wnd.setSize (400,300); 012 wnd.setVisible (true); 013} 014}Listing2301.java
Listing 23.1. Creating a Simple Window

Running this program will create a window with the title "Simple Window" and display it on the screen at a size of 400 * 300 pixels.

Since we have not yet implemented any code for handling GUI events, the window only offers the standard behavior known from Windows. It can be moved and changed in size and has a title and menu bar with a system menu. In contrast to other graphics-oriented systems, there is still no functionality for closing the window. The sample program can therefore only be terminated by a hard abort on the part of the user (e.g. by pressing + in the DOS box from which the window was started). In the next example, we'll add code to properly close the window.

23.2.2 The paint

The output in a window is done by overlaying the method, which is called whenever the window has to be redrawn in whole or in part. This is the case, for example, when the window is displayed for the first time or when a part of the window that was previously covered becomes visible due to user actions. receives an instance of the class when called:

is Java's implementation of a Device context (also Graphic context called) and thus represents the abstraction of a universal output device for graphics and writing. The class offers methods for generating line, fill and text elements. It also manages the character color, in which all output is made, and a font that is used for outputting fonts. A device context can therefore be viewed as a kind of universal output device that provides elementary functions for outputting colored graphics and characters.

23.2.3 The graphic coordinate system

The output of graphics is based on a two-dimensional coordinate system, the origin of which (0,0) is in the upper left corner (see Figure 23.1). Positive xValues ​​extend to the right, positive yValues ​​down. The unit of measurement corresponds to one screen pixel and is therefore device-dependent.

In most cases, the program does not have the entire window available for output, but rather there is Edge elements such as title lines, menus or frames that cannot be overwritten. The method can be used to determine the size of these edge elements. We'll come back to this in Chapter 24.

Figure 23.1: The Java coordinate system

23.2.4 Closing a window

A simple main window, as shown in the previous example, does not have any functionality to enable the user to close it in an orderly manner. All corresponding dialog elements in the system menu have no function. In fact, we had to cancel the program by pressing +. Without going too deep into details (these will be handed in later in Chapter 28 and Chapter 29), we would like to present an example of a suitable mechanism for closing the main window.

If the user is to be able to close a main window, one must be registered. This is an interface whose method is called when the user wants to close the window via the system menu or the close button. In this case, the program will call to close the window and, if necessary, append to also terminate the program.

Since such a class is required in practically every GUI program, we want to introduce a class that can be reused in all of the following examples:

001 / * WindowClosingAdapter.java * / 002 003 import java.awt. *; 004 import java.awt.event. *; 005 006 publicclass WindowClosingAdapter 007 extends WindowAdapter 008 {009 privateboolean exitSystem; 010 011 / ** 012 * Generates a WindowClosingAdapter for closing 013 * the window. If exitSystem is true, the complete 014 * program is terminated. 015 * / 016 public WindowClosingAdapter (boolean exitSystem) 017 {018 this.exitSystem = exitSystem; 019} 020 021 / ** 022 * Generates a WindowClosingAdapter to close 023 * the window. The program does not end. 024 * / 025 public WindowClosingAdapter () 026 {027 this (false); 028} 029 030 public void windowClosing (WindowEvent event) 031 {032 event.getWindow (). SetVisible (false); 033 event.getWindow (). Dispose (); 034 if (exitSystem) {035 System.exit (0); 036} 037} 038}WindowClosingAdapter.java
Listing 23.2. The WindowClosingAdapter Class

In order to achieve the desired effect, the listener must be registered by calling up the window to be closed. As a result, when you click the close button (or when you call up the corresponding system menu entry), the method is called and the window is closed. If passed to the constructor, the listener terminates the entire program.

This can be used to improve Listing 23.1 as follows:

001 / * Listing2303.java * / 002 003 import java.awt. *; 004 005 class Listing2303 006 {007 publicstaticvoid main (String [] args) 008 {009 Frame wnd = new Frame ("Close window"); 010 wnd.addWindowListener (new WindowClosingAdapter (true)); 011 wnd.setSize (400,300); 012 wnd.setVisible (true); 013} 014}Listing2303.java
Listing 23.3. Enable Closing the Window

The WindowClosingAdapter.java file must exist in the current directory so that the examples in which the is used can be compiled. It can be found on the CD-ROM for the book or can be taken directly from Listing 23.2.