Chapter 14: Painting


We now begin a series of three chapters about visual programming. Up until now, all your applications have produced text output. In Chapter 11, "Exceptions", you learned how to provide text input via command-line arguments. Text input and output are fine, up to a point, but the mouse and the GUI provide a much richer environment for communicating a user's ideas to a program, and for communicating a program's results to a user.

Graphical user interface is usually abbreviated GUI. (Yes, it's pronounced "gooey.") The java.awt package contains dozens of classes that support GUI concepts like windows, colors, lines, squares, fonts, buttons, and check boxes. This chapter will show you how to display a window on your screen and paint basic shapes in it. That isn't spectacular, but this chapter will also prepare you for Chapters 15, "Components," and 16, "Events," where you will learn how to populate your GUIs with buttons, scrollbars, labels, and other standard controls. This chapter will end with an extended example program whose GUI combines custom painting with standard components.

Frames

A frame is a window on a computer screen, plus the "decoration" that makes it look like an independent window, plus the underlying programmatic behavior that lets you move windows around on your screen, resize them, iconify them, and so on. Figure 14.1 shows a frame whose contents are gray.


Figure 14.1: A frame with boring contents

The figure shows a Windows frame that was created by a Java program running on a Windows platform. Windows users will recognize the Minimize, Maximize, and Close buttons in the upper-right corner, as well as the decorations that give the outline its 3-D beveled appearance. On a different platform, the same program would create a frame whose controls and decorations looked slightly different, appropriate to the platform's windowing software.

So on any platform, a frame created by a Java program looks exactly like any other frame. This happens because the classes of the java.awt package do not directly draw components onto the screen. Instead, they instruct the underlying system's windowing software to do the work.

Note

Java provides two alternative toolkits for creating GUIs. The simpler one is called AWT, which stands for Another Windowing Toolkit. The more complicated toolkit is called Swing, which doesn't stand for anything and is not discussed in this book. Swing does not use the underlying windowing software to draw components.

Here is the application that created the frame in Figure 14.1:

 1. import java.awt.*;  2.  3. public class EmptyFrame extends Frame  4. {  5.     EmptyFrame()  6.     {  7.         setTitle("A bleak empty gray frame");  8.         setBackground(new Color(128, 128, 128));  9.         setSize(300, 220); 10.     } 11. 12.     public static void main(String[] args) 13.     { 14.         EmptyFrame em = new EmptyFrame(); 15.         em.setVisible(true); 16.     } 17. }

The application class extends java.awt.Frame. The superclass provides all the generic functionality of a frame. When you create a subclass of java.awt.Frame, you only have to provide the non-generic, application-specific behavior. In this example, the subclass does four things:

  • Puts a message in the frame's title bar.

  • Sets the frame's background color.

  • Sets the frame's size.

  • Makes the frame visible.

Line 7 sets the message in the title bar. The setTitle() method is inherited from the superclass; it takes a string argument.

Line 8 sets the frame's background color. Whenever the frame needs to be drawn on the screen, all of its pixels are set to the background color (except for the decoration pixels, of course). Then any application-specific drawing is performed. In this example, there is no application-specific drawing, so all you see in the frame's interior is uniform gray. The setBackground() method takes an argument of type java.awt.Color. We will look more deeply at this class in the next section, "Colors."

Line 9 uses the setSize() method to set the frame's size. The method's arguments are the desired width and height, in pixels. (A pixel, or picture element, is a dot on a display screen.) It's important to set a frame's size, because the default size is zero pixels wide by zero pixels high. So if you neglect to call setSize(), your frame will be too small to see.

Line 14 makes the frame visible. Before a frame executes setVisible(true), it's just a lot of bytes somewhere in memory, like any other object. The first time setVisible() is executed, the frame establishes communication with the windowing software on the underlying system, and the windowing software draws the frame on the screen. The process is quite complicated, but it all happens automatically. You only need to remember to call both setSize() and setVisible(). Otherwise, your frame will not be seen.

Notice that the title bar message, foreground color, and size are set in the EmptyFrame constructor, while setVisible()is called in main(), after the frame has been constructed. The program would function the same if any of the calls on lines 7-9 were moved into main(). For example, the following code sets the background color and size in main():

 1. import java.awt.*;  2.  3. public class EmptyFrame extends Frame  4. {  5.     EmptyFrame()  6.     {  7.         setTitle("A bleak empty gray frame");  8.     }  9. 10.     public static void main(String[] args) 11.     { 12.         EmptyFrame em = new EmptyFrame(); 13.         em.setBackground(new Color(128, 128, 128)); 14.         em.setSize(300, 220); 15.         em.setVisible(true); 16.     } 17. }

This version produces an identical frame, but the previous version is considered better design. In the previous version, the constructor was responsible for setting the properties of the subclass instance, but it did not make the frame visible. This is clean design, because code that uses the class might want to create the object but not display it for a while. In general, constructors should set up the internal properties of an object without dictating when and how the object is to be used.

Note

A frame that you create in Java does not automatically disappear when you click the "Close" button in its upper-right corner. The frame only sends an event to its listeners, using the mechanism that will be explained in Chapter 16. To kill a frame, you can always type CONTROL-C in the console window where you started the program.

The code examples throughout the remainder of this book will feature frame subclasses whose constructors do everything except call setVisible(). Making the frame visible is the job of the code that uses the frame subclass. For us, this will always happen in main(), immediately after the subclass is created.

Now you know how to create a frame with boring uniform contents. Now it's time to learn how to put interesting things inside the frame. These things can be seen only if their colors are different from the frame's background color, so let's begin by looking at how Java handles colors.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net