Outline Creating Objects The String Class Packages Formatting

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images 1

Graphical Applications • Except for the applets seen in Chapter 2, the example programs we've explored thus far have been text-based • They are called command-line applications, which interact with the user using simple text prompts • Let's examine some Java applications that have graphical components • These components will serve as a foundation to programs that have true graphical user interfaces (GUIs) 2

GUI Components • A GUI component is an object that represents a screen element such as a button or a text field • GUI-related classes are defined primarily in the java. awt and the javax. swing packages • The Abstract Windowing Toolkit (AWT) was the original Java GUI package • The Swing package provides additional and more versatile components • Both packages are needed to create a Java GUI-based program 3

GUI Containers • A GUI container is a component that is used to hold and organize other components • A frame is a container that is used to display a GUI-based Java application • A frame is displayed as a separate window with a title bar – it can be repositioned and resized on the screen as needed • A panel is a container that cannot be displayed on its own but is used to organize other components • A panel must be added to another container to be displayed 4

GUI Containers • A GUI container can be classified as either heavyweight or lightweight • A heavyweight container is one that is managed by the underlying operating system • A lightweight container is managed by the Java program itself • Occasionally this distinction is important • A frame is a heavyweight container and a panel is a lightweight container 5

Labels • A label is a GUI component that displays a line of text • Labels are usually used to display information or identify other components in the interface • Let's look at a program that organizes two labels in a panel and displays that panel in a frame • See Authority. java (page 143) • This program is not interactive, but the frame can be repositioned and resized 6

Nested Panels • Containers that contain other components make up the containment hierarchy of an interface • This hierarchy can be as intricate as needed to create the visual effect desired • The following example nests two panels inside a third panel – note the effect this has as the frame is resized • See Nested. Panels. java (page 145) import java. awt. *; import javax. swing. *; 7
![Nested. Panels. java (page 145) public static void main (String[] args) { JFrame frame Nested. Panels. java (page 145) public static void main (String[] args) { JFrame frame](http://slidetodoc.com/presentation_image_h2/77f35e20af65ffda639256827cff013e/image-8.jpg)
Nested. Panels. java (page 145) public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels"); frame. set. Default. Close. Operation (JFrame. EXIT_ON_CLOSE); // Set up first subpanel JPanel sub. Panel 1 = new JPanel(); sub. Panel 1. set. Preferred. Size (new Dimension(150, 100)); sub. Panel 1. set. Background (Color. green); JLabel label 1 = new JLabel ("One"); sub. Panel 1. add (label 1); // Set up second subpanel JPanel sub. Panel 2 = new JPanel(); sub. Panel 2. set. Preferred. Size (new Dimension(150, 100)); sub. Panel 2. set. Background (Color. red); JLabel label 2 = new JLabel ("Two"); sub. Panel 2. add (label 2); // Set up primary panel JPanel primary = new JPanel(); primary. set. Background (Color. blue); primary. add (sub. Panel 1); primary. add (sub. Panel 2); } frame. get. Content. Pane(). add(primary); frame. pack(); frame. set. Visible(true); 8

Nested. Panels. java (page 145) output frame sub. Panel 1 sub. Panel 2 primary 9

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images 10

Images • Images are often used in a programs with a graphical interface • Java can manage images in both JPEG and GIF formats • As we've seen, a JLabel object can be used to display a line of text • It can also be used to display an image • That is, a label can be composed of text, and image, or both at the same time 11

Images • The Image. Icon class is used to represent an image that is stored in a label • The position of the text relative to the image can be set explicitly • The alignment of the text and image within the label can be set as well • See Label. Demo. java (page 147) 12
![• Label. Demo. java (page 147) public static void main (String[] args) { • Label. Demo. java (page 147) public static void main (String[] args) {](http://slidetodoc.com/presentation_image_h2/77f35e20af65ffda639256827cff013e/image-13.jpg)
• Label. Demo. java (page 147) public static void main (String[] args) { JFrame frame = new JFrame ("Label Demo"); frame. set. Default. Close. Operation (JFrame. EXIT_ON_CLOSE); Image. Icon icon = new Image. Icon ("devil. gif"); JLabel label 1, label 2; label 1 = new JLabel ("Devil Right", icon, Swing. Constants. CENTER); label 1. set. Horizontal. Text. Position (Swing. Constants. LEFT); label 1. set. Vertical. Text. Position (Swing. Constants. BOTTOM); label 2 = new JLabel ("Devil Above", icon, Swing. Constants. CENTER); label 2. set. Horizontal. Text. Position (Swing. Constants. CENTER); label 2. set. Vertical. Text. Position (Swing. Constants. BOTTOM); JPanel panel = new JPanel(); panel. set. Background (Color. cyan); panel. set. Preferred. Size (new Dimension (200, 250)); panel. add (label 1); panel. add (label 2); frame. get. Content. Pane(). add(panel); frame. pack(); frame. set. Visible(true); } 13

Label. Demo. java (page 147) output 14

Drawing an image (not in text) • We can draw an image without putting it into a label. • This will be illustrated with a program following the XXXXX. java template given to the class. You can find this program with the Power. Point files at http: //www. cs. usm. maine. edu/~welty/cos 1 60/160 fall 2006/powerpoint/image 15

Image. java (not in text) - JFrame /* Set up the frame and panel for drawing an image*/ import javax. swing. JFrame; public class Image extends JFrame{ public static void main (String[] args) { JFrame frame = new JFrame ("Image"); frame. set. Default. Close. Operation (JFrame. EXIT_ON_CLOSE); Image. Panel panel = new Image. Panel(); } } frame. get. Content. Pane(). add(panel); frame. pack(); frame. set. Visible(true); 16

Image. java (not in text) - JPanel /* Creates the panel of the program and draws an image on it. import javax. swing. JPanel; import java. awt. *; import javax. swing. *; */ /* Display an icon on the panel */ public class Image. Panel extends JPanel { private Image. Icon icon; // Constructor: Get an image public Image. Panel () { icon = new Image. Icon ("devil. gif"); set. Preferred. Size (new Dimension(100, 100)); } /* Draw the image. */ public void paint. Component(Graphics page) { super. paint. Component (page); page. draw. Image(icon. get. Image(), 15, this); }} 17

Image. java (not in text) - output You can find this program with the Power. Point files at http: //www. cs. usm. maine. edu/~welty/cos 160/ 160 fall 2006/powerpoint/image 18

Summary • Chapter 3 focused on: – object creation and object references – the String class and its methods – the Java standard class library – the Random and Math classes – formatting output – enumerated types – wrapper classes – graphical components and containers – labels and images 19
- Slides: 19