Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAPPLET.

Similar presentations


Presentation on theme: "JAPPLET."— Presentation transcript:

1 http://www.hostitwise.com/java/japplet.html http://download.oracle.com/javase/tutorial/uiswing/components/applet.html JAPPLET

2 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java program that runs “within” another program (usually a browser) Applets can be run within any browser To run Java applets, browsers need an up-to-date Java plugin appletviewer is a program that can run applets When you download the Java SDK, appletviewer comes with it appletviewer is always up-to-date with your Java system Eclipse has an built-in applet viewer

3 Applets Applets are applications that cannot run by themselves. They run in the context of a browser, or software such as an appletviewer, that provides the interface in which the applet will run. From a programming point of view applet classes are extensions of the JApplet class, which is a panel that has four methods that can be overridden, namely init(), start(), stop(), and destroy().

4 4 Packages and classes Java supplies a huge library of pre-written “code,” ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class, or all the classes in a package For this applet, you will need to import two drawing packages, awt and swing

5 JApplet hierarchy JApplet hierarchy is as follows: java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet

6 init() method The init() method is called once by a browser when the web page first creates the applet. The method usually contains code to perform basic setup tasks. If you do not provide this method in your applet then the method in JApplet is run

7 start() method The start() method is always called whenever the applet becomes visible. It is called immediately after the execution of init() on the first occasion, and then subsequently when the applet reappears after scrolling or browsing, for example

8 stop() method The stop() method is always called by a browser whenever the applet becomes invisible. This allows any applet code producing effects such as animation to be stopped.

9 destroy() method The destroy() method is called by a browser at some convenient point when it decides to remove the resources of the applet. It thus allows the applet a last chance to clean up before it is removed.

10 Understanding the JApplet Life Cycle Init() start() stop() destroy()

11 paint() method This applet provides a paint() method that draws on its panel. This is called by the browser each time the panel's visible area is affected and is supplied with a Graphics object that facilitates drawing on its surface. Because paint() overrides the superclass method, a call of super.paint() is advisable since it ensures that any other components of the superclass are painted

12 Some more methods

13 Applet Methods init Method Initializes variables Gets data from user Places various GUI components paint Method Performs output

14 Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { }

15 Example of Basic Applet import javax.swing.JApplet; import java.awt.Graphics; public class AnyApplet extends JApplet { // declare variables here public void init( ) { // data initialization goes here } public void paint( Graphics g ) { super.paint( g ); // your code goes here } }

16 Running an applet in browser We need to use html code to run applets. The minimum html required to run applet with a browser (java host) is as follows: TitleName CODE = Classname.class CODEBASE =. directory of class file WIDTH = 50 width of window in pixels HEIGHT = 50 height of window in pixels Note that in the applet tag you include the. class bytecode file and not the.java.

17 Features provided by JApplet Because JApplet is a top-level Swing container, each Swing applet has a root pane. The most noticeable effects of the root pane's presence are support for adding a menu bar and the need to use a content pane. JApplet has a single content pane. The content pane makes Swing applets different from regular applets in the following ways: You add components to a Swing applet's content pane, not directly to the applet. You set the layout manager on a Swing applet's content pane, not directly on the applet. The default layout manager for a Swing applet's content pane is BorderLayout. This differs from the default layout manager for Applet, which is FlowLayout. You should not put painting code directly in a JApplet object.

18 18 A Simple Applet

19 19 Drawing rectangles There are two ways to draw rectangles: g.drawRect( left, top, width, height ); g.fillRect( left, top, width, height );

20 20 The complete applet import javax.swing.JApplet; import java.awt.*; public class Drawing extends JApplet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); g.setColor(Color.BLACK); g.drawString("Example JApplet", 20, 80); } }

21 21 The HTML page You can only run an applet from an HTML page The HTML looks something like this: Drawing Applet Eclipse (or BlueJ) will create this HTML for you You don’t even need to think about the HTML just yet

22 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

23 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/all code associated with it

24 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 area, fonts, and colors Contained in the package java.awt

25 Constructors and Methods of the class Graphics

26 Constructors and Methods for the class Graphics

27

28


Download ppt "JAPPLET."

Similar presentations


Ads by Google