11
Java Applets

Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Embed Size (px)

Citation preview

Page 1: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Java Applets

Page 2: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Topics

What is an applet? To convert GUI applications to AppletsHello World applet example!!Applets life cycle examples Invoking applets from web

Page 3: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Developing Applets

Java applications and Applets share some common programming features although they differ in some aspectsEx: Every Java application must have a ‘main’ method invoked

by the Java interpreterJava applets on the other hand, do not need a ‘main’ method;

they can run in a web browser environmentThe Applet class provides the essential framework that

enables the applets to be run from a web browserEvery applet is a subclass of java.applet.AppletThe Applet class is in AWTTo use wing components, use javax.swing.JappletEvery GUI program can be converted into an applet by just

replacing the ‘main’ method!

Page 4: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

GUI & Applet

import javax.swing.JFrame;

import javax.swing.*;

public class CT1513Frame extends JFrame{

JButton jbtOK;

public static void main (String [ ] args ) {

CT1513Frame frame = new CT1513Frame ();

frame.setVisible(true);}

public CT1513Frame{

Container contentPane = getContentPane( );

contentPane.setLayout(new FlowLayout());

setSize (500, 200);setTitle("CT1513 FrameWithComponents ");//adding a Button onto the frame

jbtOK = new JButton ("OK");

contentPane.add(jbtOK);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

}

import javax.swing.JFrame;

import javax.swing.*;

public class CS1083 extends JApplet{

public static void main (String [ ] args ) {

JFrame frame = new JFrame ("CS1083FrameWithComponents");

//adding a Button onto the frame

JButton jbtOK = new JButton ("OK");

frame.add(jbtOK);

setSize (500, 200);

setLocationRelativeTo(null);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

Page 5: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Getting started…

Hello world..import javax.swing.JApplet;

import javax.swing.SwingUtilities;

import javax.swing.JLabel;

public class HelloWorld extends JApplet {

//Called when this applet is loaded into the browser.

public void init() {

//Execute a job on the event-dispatching thread; creating this applet's GUI.

try {

SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

JLabel lbl = new JLabel("Hello World");

add(lbl);

} });

}

catch (Exception e) { System.err.println("createGUI didn't complete successfully");

} } }

Page 6: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Applet life-cycle methods

Page 7: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Applet life-cycle

Applets are run from the applet container, which is a plug-in of a web browser.

The init Method: invoked after the applet is created; if a subclass f Applet has an initialization to perform, it should override this method

The start Method: invoked after the init method; a subclass overrides this method if there is an action to perform

The stop method: is the opposite of start method. The destroy method: invoked when the browser exits normally to

inform the applet that it is no longer needed and should release any resources it is holding.

Page 8: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

import java.applet.Applet; import java.awt.Graphics; //No need to extend JApplet, since we don't add any components; we just paint.

public class Simple extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); } private void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); } public void paint(Graphics g) { //Draw a Rectangle around the applet's display area. g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); //Draw the current string inside the rectangle. g.drawString(buffer.toString(), 5, 15); } }

Page 9: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

import java.applet.Applet; import java.awt.Graphics; //No need to extend JApplet, since

we //don't add any components; //we just paint. public class Simple extends Applet

{ StringBuffer buffer; public void init() { buffer = new StringBuffer();

addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); }

private void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord);

repaint(); } public void paint(Graphics g) { //Draw a Rectangle around //the applet's display area. g.drawRect(0, 0, getWidth() - 1,

getHeight() - 1); //Draw the current string //inside

the rectangle. g.drawString(buffer.toString(), 5, 15);

} }

Page 10: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Applets execution environment

Page 11: Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets

Invoking applet methods from web using javascripts

JavaScript code on a web page can interact with Java applets embedded on the page.

JavaScript code can perform operations such as the following: Invoke methods on Java objects Get and set fields in Java objects Get and set Java array elements Create new instances of Java objects

This is beyond our course description…so we stop here with an interesting applet example using sounds!