Docsity

Java Programming: Advanced Graphics, GUI, and Applets - Prof. Donald Adjeroh, Study notes of Computer Science

Toggle sidebar
bg1
1
Java Programming: From Problem Analysis to Program Design, Second Edition 1
Chapter 13: Advanced Graphics & GUI
Applets.
class Graphics.
class Font, and class Color.
Additional layout managers.
More GUI components.
Menu-based programs.
Handle key and mouse events.
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Inheritance Hierarchy of GUI Classes
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Java Programming: Advanced Graphics, GUI, and Applets - Prof. Donald Adjeroh and more Study notes Computer Science in PDF only on Docsity!

Java Programming: From Problem Analysis to Program Design, Second Edition 1

Chapter 13: Advanced Graphics & GUI

Œ Applets.

Œ class Graphics.

Œ class Font, and class Color.

Œ Additional layout managers.

Œ More GUI components.

Œ Menu-based programs.

Œ Handle key and mouse events.

Java Programming: From Problem Analysis to Program Design, Second Edition 2

Inheritance Hierarchy of GUI Classes

Java Programming: From Problem Analysis to Program Design, Second Edition 3

Constructors and Methods of the class Component

Java Programming: From Problem Analysis to Program Design, Second Edition 4

Constructors and Methods of the class Component

Java Programming: From Problem Analysis to Program Design, Second Edition 7

Applets

Œ A Java program that is embedded within a Web page and executed by a Web browser.

Œ Create an applet by extending the class JApplet.

Œ class JApplet is contained in package javax.swing.

Java Programming: From Problem Analysis to Program Design, Second Edition 8

Applets

Java Programming: From Problem Analysis to Program Design, Second Edition 9

Applets

Java Programming: From Problem Analysis to Program Design, Second Edition 10

Applets

Œ No main method.

Œ Methods init, start, and paint guaranteed to be invoked in sequence.

Œ To develop an applet:

Œ Override any/all of the methods above.

Java Programming: From Problem Analysis to Program Design, Second Edition 13

Applet Displaying Welcome Message

//Welcome Applet

import java.awt.Graphics; import javax.swing.JApplet;

public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super.paint(g); //Line 1 g.drawString("Welcome to Java Programming" , 30, 30); //Line 2 } }

Java Programming: From Problem Analysis to Program Design, Second Edition 14

HTML to Run Applet

Java Programming: From Problem Analysis to Program Design, Second Edition 15

class Font

Œ Shows text in different fonts.

Œ Contained in package java.awt. Œ Available fonts: Œ Serif/Sans Serif Œ Monospaced Œ Dialog/DialogInput

Œ Arguments for constructor: Œ String specifying the font face name. Œ int value specifying font style. Œ int value specifying font size. Œ Expressed in points (72 points = 1 inch).

Java Programming: From Problem Analysis to Program Design, Second Edition 16

class Font

Java Programming: From Problem Analysis to Program Design, Second Edition 19

class Color

Java Programming: From Problem Analysis to Program Design, Second Edition 20

class Color

Java Programming: From Problem Analysis to Program Design, Second Edition 21

class Graphics

Œ Provides methods for drawing items such as lines, ovals, and rectangles on the screen.

Œ Contains methods to set the properties of graphic elements including clipping areas, fonts, and colors.

Œ Contained in the package java.awt.

Java Programming: From Problem Analysis to Program Design, Second Edition 22

class Graphics

Java Programming: From Problem Analysis to Program Design, Second Edition 25

Constructors and Methods for the

class Graphics

Java Programming: From Problem Analysis to Program Design, Second Edition 26

Constructors and Methods for the

class Graphics

Java Programming: From Problem Analysis to Program Design, Second Edition 27

Differences Between Applets and GUI Applications

Œ Applets Œ Derived from JApplet. Œ No main method. Œ Uses init method. Œ Displayed by HTML. Œ Sets title in HTML. Œ Size set in HTML. Œ Applet closes when HTML doc closes.

Œ GUI applications Œ Class extends JFrame. Œ Invokes main method. Œ Uses constructors. Œ Uses method setVisible. Œ Uses setTitle method. Œ Uses method setSize. Œ Closes with Exit button.

Java Programming: From Problem Analysis to Program Design, Second Edition 28

Converting a GUI Application to an Applet

Œ Change JFrame to JApplet.

Œ Change constructor to method init.

Œ Remove method calls such as setVisible, setTitle, setSize.

Œ Remove the method main.

Œ If applicable, remove Exit button and all code associated with it (for example, action listener).

Java Programming: From Problem Analysis to Program Design, Second Edition 31

JTextArea

Java Programming: From Problem Analysis to Program Design, Second Edition 32

JTextArea

Java Programming: From Problem Analysis to Program Design, Second Edition 33

JTextArea Example

Java Programming: From Problem Analysis to Program Design, Second Edition 34

JCheckBox

Œ User selects from predefined values.

Œ Example of a toggle button.

Œ Clicking JCheckBox generates item event.

Œ Use interface ItemListener and its abstract method itemStateChanged to handle event.

Java Programming: From Problem Analysis to Program Design, Second Edition 37

Constructors and Methods of class JCheckBox

Java Programming: From Problem Analysis to Program Design, Second Edition 38

JRadioButton

Œ Created same way as check boxes.

Œ Placed in content pane of applet.

Œ Forces user to select only one radion button at a time.

Œ You create a button group to group radio buttons.

Œ Generates an ItemEvent.

Œ interface ItemListener and method itemStateChanged used to handle events.

Java Programming: From Problem Analysis to Program Design, Second Edition 39

JRadioButton

Java Programming: From Problem Analysis to Program Design, Second Edition 40

JRadioButton