In this unit, we will be exploring the concept of drawing, or "painting", and the creation of graphical user interfaces (GUIs). Up to this point, we have been working with console applications.

Java GUI Toolkits

The Graphics toolbox which resides within the Abstract Windowing Toolkit (or AWT) makes it possible to draw simple geometric shapes, print text, and position images within the borders of a component, such as a frame (a window). The AWT is a Java interface to the native system GUI in the OS (operating system) being utilized. The AWT may not work the same way with all operating systems.

Swing, on the other hand, is a more "pure" Java GUI toolkit. It uses AWT to create an operating system window and then draws within that window, making it 100% portable across platforms. It also provides a more sophisticated set of GUI components than those found in its predecessor AWT. The classes found in Swing have similar names to those found in AWT, but they are prefixed with the letter J. It is most likely that the use of AWT will be phased out in favor of Swing. We will be working with Swing.

JavaFX is a toolkit that surfaced with Java 8. JavaFX utilizes many of Swing's core concepts including components, layouts and events, but works with a different structure. JavaFX is compatible with Swing, and Swing and AWT continue to be supported by Java. This introductory unit to Java GUI will concentrate on beginning concepts in Swing.

GUI Programming

A GUI program still contains a main() method, but it generally only creates one or more GUI components and displays them on the screen. The GUI components, once created, follow their own programming, which tells them how to draw themselves on the screen.
You will be importing one or all of the following:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


Working in a Window

In a Java GUI, you will be working with a window. Windows can be closed or opened and resized. A window can display a title bar at its top and contain other forms of components such as buttons and menus. The most common type of window is a JFrame, which is included in the package javax.swing.

A JFrame is an independent window which will act at the main window of an application. There will be nothing contained in your window (no content) until you place it there. The content that is displayed in a JFrame is referred to as a content pane.

A JPanel is an invisible container for laying out other drawing components. New components can then be added to a JPanel, or something can be drawn on the surface of a JPanel.

A JPanel appears to simply be a portion of the larger JFrame. In the diagram at the right, the entire box (window) is the JFrame, while the white drawing area is the JPanel. In this unit, we will be using JPanels as drawing surfaces.

display1
You may hear the JFrame referred to as a "heavyweight", while the JPanel is referred to as a "lightweight".


Working on a Drawing Surface

All graphical user interfaces require some kind of application frame (window) in which to present the display. Our drawing surface will appear within a JPanel of a JFrame.

To create a drawing surface, you need to provide a custom paintComponent() method, which the system will call when it is time to create the drawing. Note that the system, not the programmer, calls this method. All drawing operations should occur in this method.

You may find that Java graphics programs appear to be somewhat of a mystery. It may seem that you are losing the "line by line" control you experienced with your Java console applications. By presenting a graphical display frame, graphics programs must interact with other parts of the computer's operating system. This system control will appear to take away some of the control of the programmer.

For example, you will now be dealing with programming "methods" that are executed even though you will not be "calling" them. Such is the case with the paintComponent() method. The JRE (Java Runtime Environment) calls the paint() method for a JFrame to redraw, which then passes the command to the JPanel to redraw using the paintComponent() method.

If your drawings do not change (no animations, for example), you will see that you are in mostly in control of your displays. But when you attempt to create animations (which change as the program progresses), you will start to see how parts of your program and the system will be interacting with one another to produce your drawing. You may find when using repaint() that a time gap in execution may occur when these forces try to work together.

Drawing in Java is fun and colorful!!!
Let's get started!

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.