


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















Java Programming: From Problem Analysis to Program Design, Second Edition 1
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
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
Java Programming: From Problem Analysis to Program Design, Second Edition 10
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
//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
Java Programming: From Problem Analysis to Program Design, Second Edition 15
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
Java Programming: From Problem Analysis to Program Design, Second Edition 19
Java Programming: From Problem Analysis to Program Design, Second Edition 20
Java Programming: From Problem Analysis to Program Design, Second Edition 21
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
Java Programming: From Problem Analysis to Program Design, Second Edition 25
Java Programming: From Problem Analysis to Program Design, Second Edition 26
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
Java Programming: From Problem Analysis to Program Design, Second Edition 32
Java Programming: From Problem Analysis to Program Design, Second Edition 33
Java Programming: From Problem Analysis to Program Design, Second Edition 34
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
Java Programming: From Problem Analysis to Program Design, Second Edition 38
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
Java Programming: From Problem Analysis to Program Design, Second Edition 40