Download presentation
Presentation is loading. Please wait.
Published byLambert Shepherd Modified over 9 years ago
1
Java Applets
2
Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets from web
3
Developing Applets Java applications and Applets share some common programming features although they differ in some aspects Ex: Every Java application must have a ‘main’ method invoked by the Java interpreter Java applets on the other hand, do not need a ‘main’ method; they can run in a web browser environment The Applet class provides the essential framework that enables the applets to be run from a web browser Every applet is a subclass of java.applet.Applet The Applet class is in AWT To use wing components, use javax.swing.Japplet Every GUI program can be converted into an applet by just replacing the ‘main’ method!
4
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); }
5
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"); } } }
6
Applet life-cycle methods
7
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.
8
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); } }
9
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); }
10
Applets execution environment
11
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!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.