0% found this document useful (0 votes)
30 views17 pages

U05-01-JApplets & AWT Introduction

JApplet is a subclass of Applet that belongs to the javax.swing package. It inherits all variables and methods from the Applet class. Some key differences between AWT and Swing are that AWT components are platform-dependent while Swing components are platform-independent, and Swing follows the MVC architecture and provides more powerful components. The AWT classes are arranged hierarchically with Container and Component as the top-level classes. Common AWT classes include Panel, Frame, and Window.

Uploaded by

dhyan114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

U05-01-JApplets & AWT Introduction

JApplet is a subclass of Applet that belongs to the javax.swing package. It inherits all variables and methods from the Applet class. Some key differences between AWT and Swing are that AWT components are platform-dependent while Swing components are platform-independent, and Swing follows the MVC architecture and provides more powerful components. The AWT classes are arranged hierarchically with Container and Component as the top-level classes. Common AWT classes include Panel, Frame, and Window.

Uploaded by

dhyan114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

JAVA APPLETS

Dhyaneswaran J
AP - IT
Applet Introduction
• Applets are small applications accessed on an Internet server
• run as part of a Web document
• Applets have a user interface and different program structure
Example

import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
Apple Creation
• It needs two packages java.awt.*,java.applet.* imported
• awt provides Window or Graphical based UI’s
• applet package consists of Applet class (inheritance)
• Applet classes are declared as public
• paint( ) method invoked to create or redraw applet on screen
• Provided by AWT and is overridden in applets
• Graphics context is used as a parameter in paint method

• void drawString(String message, int x, int y)


• Draw string at specified location x,y
• Method is defined in Graphics class
• Execution of Applet differs from normal JAVA program
Apple - Execution
• Applet program compilation is similar to JAVA program
• Uses javac to compile code to create class file

• Running an Applet can be of two ways


• applet execution through Web Browser
• Applet execution through applet viewer provided in JAVA SDK

• Key Points
• Applets do not need a main( ) method
• Applets use interface provided by AWT classes
Applet – HTML
• Creation of Applet Tag in a HTML file
<applet code="SimpleApplet" width=200 height=60>
</applet>
• Run HTML file created using Web browser
• Use appletviewer command along with HTML file to run applet
C:\>appletviewer RunApp.html
Faster way
• Applet creation by adding Applet tag in comment line of our JAVA
program
• Run using appletviewer
Applet - Architecture
• Applet Architecture is event driven
• AWT notifies Applet about an event via event handlers
• Event Handlers are provided by Applets
• Applet makes quick response and returns control to AWT
• Applet Life Cycle contains four methods
• init( )
• start( )
• stop( )
• destroy()

Methods are defined by Applet class

• paint( )

Method defined by AWT Component class


Applet Display methods
• Applet uses AWT to perform Input and Output operations
• Few methods provided by Component class are
setBackground(Color.green);
setForeground(Color.red);

• Obtain current setting of color by using


Color getBackground( )
Color getForeground( )

to displaying information in its window


showStatus("This is shown in the status window.");
Applet repaint( )
• Applet updates window when paint( ) method is invoked
• paint( ) method is provided by AWT
• In order to repaint already created window following forms of
repaint( ) method can be used
• void repaint( )
• void repaint(int left, int top, int width, int height)
• void repaint(long maxDelay)
• void repaint(long maxDelay, int x, int y, int width, int height)
Japplet - Introduction
• JApplet belongs to the package javax.swing.*

• JApplet class extends the Applet class

• All the variables & member methods of Applet class are inherited
into JApplet class
import javax.swing.*;
import java.awt.event.*;
public class AppletEx extends JApplet implements ActionListener {
JButton b;
JTextField tf;
JLabel jl, jl1;
public void init() {
jl = new JLabel("Enter Text");
jl.setBounds(30, 10, 100, 100);
tf = new JTextField();
tf.setBounds(30, 100, 150, 20);
b = new JButton("Click");
b.setBounds(80, 150, 70, 40);
jl1 = new JLabel("");
jl1.setBounds(100, 180, 100, 100);
add(b);add(tf);
add(jl);add(jl1);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
}
public void actionPerformed(ActionEvent e) {
jl1.setText(tf.getText());
}
}
AWT Classes
• AWT classes are available in java.awt.* package
• Classes are arranged in Hierarchical order and easy to understand its
purpose
• AWT classes defines windows functionality at each level
• Two most common classes are
• Panel – used by applets
• Frame – Standard window
• Component is abstract class and top level class in AWT hierarchy
• All attributes are visual components displayed on screen
• Component class maintains methods for managing events of mouse
and keyboard
• Component class remembers foreground, background and current
text font
SWING Vs. AWT

Java AWT Java Swing


• AWT components are platform- • Java swing components are platform-
dependent. independent.

• AWT components are heavyweight. • Swing components are lightweight.

• AWT doesn't support pluggable look and • Swing supports pluggable look and feel.
feel.

• AWT provides less components than • Swing provides more powerful


Swing. components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.

• AWT doesn't follows MVC(Model View • Swing follows MVC.


Controller) where model represents data,
view represents presentation and
controller acts as an interface between
model and view.
AWT Classes Hierarchy
Container Class
• Container is a sub class of Component class
• Container class are responsible for positioning or laying out of any
components
Panel Class
• Panel subclass of Container class and superclass of Applet
• Panel class is a window without titlebar, menubar and border
• appletviewer provides titlebar and border
• add() method used to add components (inherited from Container)
• setLocation(),setSize(), setBounds() methods defined by Component
are inherited into Panel class
Window
• Window class creates a top level of window
• Its objects are not created directly
• Instead subclass of Window called Frame creates objects
Frame Class
• subclass of Window class
• Has title bar, menu bar, borders and resizing corners
• Frame window is created as normal program window
• Frame class contains two constructors
Frame( ) – window without title
Frame(String title)
• Methods of Frame class are
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
Dimension getSize( )
void setVisible(boolean visibleFlag)
void setTitle(String newTitle)

You might also like