20
CSTP WS00 CS423 (cotter) 1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

Embed Size (px)

Citation preview

Page 1: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 1

Java Applets

Objective:Learn how to develop Java programs that interact

with users through a Web browser

Page 2: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 2

Java Applet Lifecycle Methods

• init ( )– Called when applet is loaded onto user’s machine. Prep

work or one-time-only work done at this time.

• start ( )– Called when applet becomes visible (page called up).

Called every time applet becomes visible.

• stop ( )– Called when applet becomes hidden (page loses focus).

• destroy ( )– Guaranteed to be called when browser shuts down.

Page 3: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 3

Applets vs. Applications

• Extend Applet instead of Frame• Use of init( ) vs. constructor• No use of main( )• Default layout managers

– application: BorderLayout

– applet: FlowLayout

• Security

Page 4: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 4

Applet Presentation

• Uses applet Tag in HTML

<HTML>

<TITLE> MyNewApplet </TITLE>

<BODY>

Here’s My first Applet

<APPLET CODE = “MyNewApplet” WIDTH = 300 HEIGHT = 200>

</APPLET>

</BODY>

</HTML>

Page 5: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 5

Applet Inheritance (extension)

• Object - “cosmic base class”• Component - button, listBox• Container - dialog box• Panel - “sub-window”, group of buttons, etc.• Applet - small application, web enabled• MyApplet - specific instance of an applet

Page 6: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 6

MyTextApplet

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class MyTextApplet extends Applet {

public void init() {

setBackground(Color.gray);

Panel p = new Panel();

ButtonAction aButtonAction = new ButtonAction();

p.setLayout(new FlowLayout());

tickButton = new Button("Tick");

tickButton.addActionListener(aButtonAction);

p.add(tickButton);

Page 7: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 7

MyTextApplet

setButton = new Button("Set time"); setButton.addActionListener(aButtonAction); p.add(setButton); hourField = new TextField("12", 3); p.add(hourField); minuteField = new TextField("00", 3); p.add(minuteField); timeField = new TextField("", 12); p.add(timeField); add(p); }

Page 8: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 8

MyTextApplet

Class ButtonAction implements ActionListener

{

public void actionPerformed(ActionEvent evt)

{

String buttoncommand = evt.getActionCommand();

if (buttoncommand.equals("Tick"))

{

int minutes = Integer.parseInt(minuteField.getText());

minutes += 1;

String min = String.valueOf(minutes);

minuteField.setText(min);

}

Page 9: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 9

MyTextApplet

else if (buttoncommand.equals("Set time")){

int hours = Integer.parseInt(hourField.getText()); int minutes =

Integer.parseInt(minuteField.getText()); String tim = hourField.getText() + ":" +

minuteField.getText(); timeField.setText(tim);

} }} //end of ButtonAction

private TextField hourField;private TextField minuteField;private TextField timeField;

private Button tickButton;private Button setButton;

}

Page 10: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 10

HTML file for MyTextApplet

<HTML><HEAD><TITLE> My Text Applet Demo </TITLE></HEAD><BODY background="Image1.jpg">

<H1> Here's my Demo Applet </H1><HR SIZE = 3><APPLET CODE = MyTextApplet WIDTH = 300 HEIGHT = 100></APPLET></BODY></HTML>

Page 11: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 11

MyTextApplet Output

Page 12: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 12

Parameter Passing to Applets

import java.applet.*;import java.awt.*;

public class FontApplet extends Applet{

public void paint (Graphics g){

String fontName = getParameter ("font");int fontSize = Integer.parseInt (getParameter("size"));Font f = new Font(fontName, Font.BOLD, fontSize);g.setFont (f);g.drawString("Welcome to CS423", 25, 50);

}}

Page 13: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 13

HTML file for FontApplet

<HTML><TITLE> FontApplet </TITLE><BODY background=“Image1.jpg”>Here’s a demo of parameter passing to an Applet<hr><APPLET CODE = "FontApplet" WIDTH = 300 HEIGHT = 200><PARAM NAME = font VALUE = "Helvetica"><PARAM NAME = size VALUE = "24"></APPLET></BODY></HTML>

Page 14: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 14

FontApplet.htm

Page 15: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter)

Using Applet Parameters in VJ++

Page 16: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 16

Java Applet Lifecycle Methods(again...)

• init ( )– Called when applet is loaded onto user’s machine. Prep

work or one-time-only work done at this time.

• start ( )– Called when applet becomes visible (page called up).

Called every time applet becomes visible.

• stop ( )– Called when applet becomes hidden (page loses focus).

• destroy ( )– Guaranteed to be called when browser shuts down.

Page 17: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 17

LifecycleApplet.javaimport java.applet.Applet;import java.awt.Graphics; public class LifecycleApplet extends Applet { public LifecycleApplet() { System.out.println("Constructor running..."); } public void init() { System.out.println("This is init."); } public void start() { System.out.println("Applet started."); } public void paint(Graphics theGraphics) { theGraphics.drawString("Hello, World!", 0, 50); System.out.println("Applet just painted."); } public void stop() { System.out.println("Applet stopped."); } public void destroy() { System.out.println("Applet destroyed."); } }

Page 18: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 18

LifecycleApplet.java

Page 19: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 19

LifecycleApplet.java

Page 20: CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser

CSTP WS00 CS423 (cotter) 20

Summary

• Java Applet managed through applet lifecycle

• Applet can interact with browser through HTML code

• Uses Event programming model to interact with user (using buttons, textfields, etc.)