SlideShare a Scribd company logo
A Simple Applet
1 RAJESHREE KHANDE
Applets
2
 Applet are small program use in Internet Computing
 Applet are run using Applet Viewer or Web Browser that support Java.
 Applet can perform arithmetic operations, display graphics play sound,
accept user input, create animations and play interactive games.
 Applet do not use the main() method for initiating the execution of the
code.
 When loaded , automatically calls certain method of applet to start &
execute the applet code.
RAJESHREE KHANDE
Applets
3
 Applets are run from inside a Web Page using a
special features know as applet tag
 Applet can not read from or write to a file form local
computer
 Applet are restricted from using libraries. From other
language like C & C++ .
RAJESHREE KHANDE
Applet Class
4
 Applet code uses services of two classes namely Applet &
Graphics
 The Applet class provide life and behavior to applet
through it’s method such as
- init()
- start()
- paint()
- stop()
- destroy()
 Applet class extends the AWT class Panel.
RAJESHREE KHANDE
Applet Class
5
 When loaded , automatically calls series of Applet class
methods for starting , running & stopping the applet code.
 Therefore maintains a lifecycle of an applet.
 The paint() method of Applet class when called, actually
display the result of the applet code on the screen.
 The paint() method requires a graphics object as an
argument.
syntax : public void paint(Graphics g)
RAJESHREE KHANDE
Skeleton of Applet
6
import java.awt.*;
import java.applet.*;
……………
……………
public class appletclassName extends Applet
{
public void init()
{
…………….
…………….
}
public void paint(Graphics g)
{
…………….
…………….
}
} RAJESHREE KHANDE
Graphic class
7
 public void paint(Graphics g) { … }
 public says that anyone can use this method
 void says that it does not return a result
 A Graphics (short for “Graphics context”) is an object
that holds information about a painting
 It remembers what color you are using
 It remembers what font you are using
 You can “paint” on it (but it doesn’t remember what
you have painted)
RAJESHREE KHANDE
Graphic class
8
 The java.awt package defines a class named Color
 There are 13 predefined colors—here are their fully-
qualified names:
 For compatibility with older programs (before the naming
conventions were established), Java also allows color
names in lowercase: Color.black, Color.darkGray, etc.
Color.BLACK Color.PINK Color.GREEN
Color.DARK_GRAY Color.RED Color.CYAN
Color.GRAY Color.ORANGE Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE Color.MAGENTA
RAJESHREE KHANDE
New Color
9
 Every color is a mix of red, green, and blue
 You can make your own colors:
new Color( red , green , blue )
 Amounts range from 0 to 255
 Black is (0, 0, 0), white is (255, 255, 255)
 We are mixing lights, not pigments
 Yellow is red + green, or (255, 255, 0)
RAJESHREE KHANDE
Step to develop an Applet
10
1. Building an applet code ( .java file)
2. Creating an executable applet ( .class file)
3. Designing a Web page using HTML tag
4. Prepare an <APPLET> tag
5. Incorporating an <APPLET> tag into the Web
page
6. Creating HTML file.
7. Testing an applet code.
RAJESHREE KHANDE
Example
11
/* applet code i.e. HelloApplet.java
*/
import java.awt.*;
import java.applet.*;
public void HelloApplet extend applet
{
public void paint(Graphics g)
{
g.drawString(“Hello applet”,10,10);
}
}
/* HTML file */
<HTML>
<HEAD>
<TITLE>
Welcome to Java Applet
</TITLE>
</HEAD>
<BODY>
<CENETR>
<H1> Welcome to 1st applet </H1>
</CENTER>
<BR> <CENETR>
<APPLET CODE =HelloApplet.class
WIDTH =400 HEIGHT =200>
</APPLET>
</CENTER>
</BODY>
</HTML>
RAJESHREE KHANDE
Running the Applet
12
 We must have the following file in the current
directory
1. HelloApplet.java
2. HelloApplet.class
3. HelloApplet.html
 To run an applet we require the one of the
following
1 Java enable Web browser
If we use this we will be able to see the entire Web Page
containing the applet
2 Java appletviewer
if we use appletviewer we will only see the applet
output.
RAJESHREE KHANDE
Applet Life Cycle
13
 Every java applet inherits a set of default behaviors
from Applet Class.
 When an applet is loaded, it undergoes a series of
changes in it’s state.
 The applet state includes
1. Born or Initialization state
2. Running State
3. Idle State
4. Dead or Destroyed state
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
14
Begin
Born(Load Applet)
Initialization
Running Idle
DeadDestroy
Exit of Browser
End
destroy( )
start( )
paint( )
Display
stop( )
start( )
Stopped
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
15
1. Initialization state
- Enters in initialization state when it’s first loaded.
- Achieved by calling init( ) method of an Applet class.
- The applet is born (creation of object needed by applet,
setting initial values, set up colors, load images and fonts.
- Initialization occurs only once in the applet life cycle
- Therefore override the init() method
public void init()
{
--------
--------
--------
} Action
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
16
2. Running state
- Enter in the running state when system call start( ) method
of an Applet class.
- This occurs automatically when applet is initialized
- start() method may be called more than once.
- We may override the start( ) method to create a thread
to control the applet
public void start()
{
--------
--------
--------
}
Action
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
17
3. Idle or Stopped state
- When applet stopped running it becomes idle
- Stopping occurs when we leave the page containing the
currently
running applet.
- We can also do so by calling stop() method explicitly.
- If we use thread to run an applet then we must use stop()
method
to terminate the thread.
- We can achieve this by overriding the stop() method.
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
18
public void stop()
{
--------
--------
--------
}
Action
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
19
3. Dead state
- An applet is said to be dead when it is removed from memory.
- This occurs automatically by invoking destroy() method when we
quit the browser.
- It occurs only once in applet’s life cycle.
- If applet has created any resource like thread, we may override
destroy() method to clean up the resource.
public void destroy()
{
--------
--------
--------
}
Action
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
20
Display state
- Applet moves to display state whenever some output
operation has to perform on the screen.
- It occurs after the applet enters into running state.
- The paint( ) method is called to do this task.
- Every applet will have a paint( ) method.
- Override paint( ) method whenever we want to display
anything on the screen.
RAJESHREE KHANDE
Applet Life Cycle
( An Applet State transition Diagram )
21
public void paint(Graphics g)
{
--------
--------
--------
}
Display Statement
RAJESHREE KHANDE
Difference between Applet & Application
Applet Aplication Program
An applet can be embedded in HTML Web
page & easily downloaded over Internet
An application program can not be
embedded in HTML Web page.
An applet runs inside java compatible
container such as internet explorer or
netscape navigator.
Runs on command prompt using java
interpreter , java.exe
An applet executes under strict security
limitation that reject some opreation such as
accessing a file or downloading a file
Has no security restriction.
It is java program that is written for
distribution over a network. i.e. applet is not
standalone program
It is standalone program
It does not require main() method for it’s
execution.
Need main() method for it’s execution.
22
Designing a Web Page
23
<HTML>
</HTML>
<!
………
>
<HEAD>
</HEAD>
Title Tag
<BODY>
</BODY>
<APPLET> Applet Tag
</APPLET>
Comment Section
Head Section
Body Section
RAJESHREE KHANDE
Applet Tag
24
<APPLET
[ CODEBASE = codebase_URL]
CODE =AppletFileName.class
[ALT = alternate text]
[NAME = applet_instance_name]
WIDTH = pixel
HEIGHT =pixel
[ALIGNMENT = alignment]
[VSPACE = pixel ]
[HSPACE = pixel ]
>
[<PARAM NAME =name1 VALUE =value1 >]
[<PARAM NAME =name2 VALUE =value2 >]
………………………………………….
…………………………………………
</APPLET>
RAJESHREE KHANDE
Attribute of Applet Tag
25
1. CODE : =AppletFileName.class
Specifies a name of the applet class to be loaded
i.e the name of already compiled file
2. CODEBASE=codebase_URL(optional)
Specifies the URL of the directory in which the applet
resides. If applet resides in the same directory as HTML
file, then this attribute is ommitted.
3. WIDTH=pixel HEIGHT=pixel
This specifies width & height of the space on HTML
page.
RAJESHREE KHANDE
Attribute of Applet Tag
26
4. NAME =applet_instance_name (optional)
A name for the applet , so that other applet on the
page may refer to this applet. Used for inter applet
communication.
5. ALIGN =alignment (optional)
This specifies where on the page the applet will
appear. Values are : TOP,BOTTOM,LEFT,RIGHT
MIDDLE,ABSMIDDLE, ABSBOTTOM, TEXTTOP and
BASELINE
RAJESHREE KHANDE
Attribute of Applet Tag
27
6. HSPACE=pixel (optional)
Used only when align is set to LEFT or RIGHT.
Specifies horizontal blank spaces the browser should
leave surrounding the applet.
7. VSPACE=pixel (optional)
Used only when align is set to TOP or BOTTOM.
Specifies vertical blank spaces the browser should
leave surrounding the applet.
8. ALT=alternate_text (optinal)
Non-Java browser will display this text where the
applet would normally go.RAJESHREE KHANDE
Applet Class
28
 Applet extends the awt class Panel .
 Panel extends Container which
RAJESHREE KHANDE
The java.awt package
29
 The java.awt package includes classes for:
 Drawing lines and shapes
 Drawing letters
 Setting colors
 Choosing fonts
 If it’s drawn on the screen, then java.awt is probably
involved!
RAJESHREE KHANDE
The applet so far
30
import java.applet.Applet;
import java.awt.*;
RAJESHREE KHANDE
Your applet class
31
public class Drawing extends Applet {
… }
 Drawing is the name of your class
 Class names should always be capitalized
 extends Applet says that our Drawing is a kind of
Applet, but with added capabilities
 Java’s Applet just makes an empty window
 We are going to draw in that window
 The only way to make an applet is to extend Applet
RAJESHREE KHANDE
The applet so far
32
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
…we still need to put some code in here...
}
RAJESHREE KHANDE
The paint method
33
 Our applet is going to have a method to paint some
colored rectangles on the screen
 This method must be named paint
 paint needs to be told where on the screen it can draw
 This will be the only parameter it needs
 paint doesn’t return any result
RAJESHREE KHANDE
The applet so far
34
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
…we still need to put some code in here…
}
}
RAJESHREE KHANDE
New colors
35
 Every color is a mix of red, green, and blue
 You can make your own colors:
new Color( red , green , blue )
 Amounts range from 0 to 255
 Black is (0, 0, 0), white is (255, 255, 255)
 We are mixing lights, not pigments
 Yellow is red + green, or (255, 255, 0)
RAJESHREE KHANDE
Setting a color
36
 To use a color, we tell our Graphics g what color we want:
g.setColor(Color.RED);
 g will remember this color and use it for everything until
we tell it some different color
RAJESHREE KHANDE
The paint method so far
37
public void paint(Graphics g) {
g.setColor(Color.BLUE);
…draw a rectangle…
g.setColor(Color.RED);
…draw another rectangle…
}
}
RAJESHREE KHANDE
Pixels
38
 A pixel is a picture (pix) element
 one pixel is one dot on your screen
 there are typically 72 to 90 pixels per inch
 java.awt measures everything in pixels
RAJESHREE KHANDE
Java’s coordinate system
39
 Java uses an (x, y) coordinate system
 (0, 0) is the top left corner
 (50, 0) is 50 pixels to the right of (0, 0)
 (0, 20) is 20 pixels down from (0, 0)
 (w - 1, h - 1) is just inside the bottom right corner, where
w is the width of the window and h is its height
(0, 0)
(0, 20)
(50, 0)
(50, 20)
(w-1, h-1)
RAJESHREE KHANDE
Drawing rectangles
40
 There are two ways to draw rectangles:
 g.drawRect( left , top , width , height );
 g.fillRect(left , top , width , height );
RAJESHREE KHANDE
The complete applet
41
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
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);
}
} RAJESHREE KHANDE
Some more java.awt methods
42
 g.drawLine( x1 , y1 , x2 , y2 );
 g.drawOval( top, left , width , height );
 g.fillOval( top, left , width , height );
 g.drawRoundRect( top, left , width , height,xDiam,yDiam);
 g.fillRoundRect( top, left , width , height, xDiam, yDiam );
 g.drawArc( top, left, width , height ,startAngle, sweepAngle
);
 g.fillArc( top, left, width , height ,startAngle, sweepAngle );
 g.drawString( string , x , y );
RAJESHREE KHANDE
The HTML page
43
 You can only run an applet in an HTML page
 The HTML looks something like this:
 <html>
<body>
<h1>DrawingApplet Applet</h1>
<applet code="DrawingApplet.class"
width="250" height="200">
</applet>
</body>
</html>
 BlueJ will create this HTML for you
RAJESHREE KHANDE
44
 getDocumentBase()
 getCodeBase()
Example
RAJESHREE KHANDE
Working with Font
45
 AWT support multiple type of Fonts
 Font have a
1. Family name : A general name of the font such as
Courier
2. a logical font name : Specifies category of font, such
as Monospaced.
3. a face name : Specifies a specific font such a
Courier Italic
 Font have encapsulated by Font class
RAJESHREE KHANDE
Methods of Font Class
46
 String getFontName()
returns the face name of the invoking font
 String getName():
returns the logical name of the invoking font
 int getSize():
returns the size, in points of the invoking font
 int getStyle():
returns the Style value of the invoking font
RAJESHREE KHANDE
Methods of Font Class
47
 boolean isBold():
returns true if the font includes BOLD style value
o.w. returns false
 boolean isItalic():
returns true if the font includes ITALIC style value
o.w. returns false
 boolean isPlain()
returns true if the font includes PLAIN style value
o.w. returns false
RAJESHREE KHANDE
Methods of Font Class
48
 String toString() :
Returns the string equivalent of the invoking font
 Font class defines following variables
1. String name : name of the font
2. float pointSize : Size of the invoking font
3. int size : Size of the font in points
4. int style : Font style
RAJESHREE KHANDE
Determining available font
49
 To know which fonts are a available on your
machine we can use the
String[] getAvailableFomtFamilyNames()
defined by GraphicsEnvironment class
 To returns an array of Font object
Font[] getAllFont()
 Since these method belong to GraphicsEnvironment
we need GraphicsEnvironment reference to call
them
RAJESHREE KHANDE
Determining available font
50
 We can obtain the referenced them by
getLocalGraphicsEnvironment()
 This is static method
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment()
 Example

RAJESHREE KHANDE
Creating and Selecting Font
51
 To select new font first construct Font object
Font(String fontName, int fontStyle,
intfontSize)
logical /face Name
 To use the created font
setFont(Font obj)
 Example
RAJESHREE KHANDE
Event Model
52
Java.lang.Object
Java.util.EventObject
Java.awt.AWTEvent
ActionEvent
(Click button
/Menu Option)
AdujustmentEvent
(Moves Scrollbar)
ComponentEvent
(When awt component
are shown ,Hidden
moved /resized)
ItemEvent
(When list item, radio
Button, check box choice
menu option selected)
TextEvent
(when Text changes
for an object
ContainerEvent FocusEvent InputEvent PaintEvent WindowEvent
KeyEvent MouseEvent
RAJESHREE KHANDE
Java Event handling
53
 The delegation event model :
The modern approach to handle the event is based on
delegation event model, which defines standard and
consistent mechanism to generate the event.
1. A source generate an event & sent it to one or more listener,
in this listener wait until it receives an event.
2. Once received the listener process the event & return.
3. The advantages of this design is that the application logic is
separated from the user interface logic that generates an
event
RAJESHREE KHANDE
Java Event handling
54
4. A user element is able to delegates the processing of
an event to a separate piece of code.
5. In the delegation event model , listener must register
with a source in order to receive an event notification.
6. This provide a benefit that notification are sent only
to the listener that want to receive them.
RAJESHREE KHANDE
Event Delegation Model
55
 Event :
In event Delegation model an event is an Object
that describe state change in source
 Event Source:
1. A source is an object that generates an event. Source
may
generate more than one type of event.
2. A source must register listener in order for the
listener to
receive notification about a specific type of event.
3. Each type of Event has its own registration method
RAJESHREE KHANDE
56
format :
public void addTypeListener(TypeListener el)
Here
Type : name of the Event and
el : reference to event listener
 Event Listener :
A listener is an object that is notified when event is occur. It has two
major requirement
1. must have been registered with one or more source to receive
notification about the specific type of event.
2. Must Implement method to receive & process these notification
Event Delegation Model
RAJESHREE KHANDE
Event Classes : EventObject
57
 At the root of Java Event class hierarchy is
EventObject , Which is in java.util.
 EventObject contain two method
1. getSource() :returns the source of event
Object getSource()
2. toString(): returns the string equivalent of the
event
RAJESHREE KHANDE
Event Classes : AWTEvent
58
 Defined in java.awt package & is a Subclass of
EventObject
 It is super class of all AWT-based event used by
event delegation model
RAJESHREE KHANDE
Main Event classes in java.awt.event
Event Class Description
ActionEvent Generates when button is pressed, a list item is double-
Clicked or menu item is selected
AdjustmentEvent Generstes when Scrollbar is manipulated
ComponentEvent When component is hidden, moved,resized,or becomes
visible
ContainerEvent When container is added or removed from container
FocusEvent When component gain or losses focus
ItemEvent Enerates when Checkbox or list item is clicked also occur
when choice selection is made / checkable menu item is
selected or deselected
59
Main Event classes in java.awt.event
Event Class Description
KeyEvent Generates when input is received from keyboard
MouseEvent Generates when mouse is dragged, moved clicked
pressed/released. Also when mouse enters or exits a
component.
MouseWheelEvent Generates When mouse wheel is moved
TextEvent Generates When the value of text area or text field is
changed.
WindowEvent Generates When window is activated .closed, deactivated,
deiconified, iconified, opened or quit
60
Event Listener Interfaces
Interface Description
ActionListener Define one method to receive action event
void actionPerformed(ActionEvent ae)
AdjustmentListener Define one method to receive adjustment event
void adjustmentValueChange(AdjustmentEvent ae)
ComponentListener Define four method to recognize when component is hidden
, moved, resized or shown
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
61
Event Listener Interfaces
Interface Description
ContainerListener Define two method to recognize when component is
added or removed from container
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
FocusListener Define two method to recognize when component gain
or losses keyboard focus
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
ItemListener Define one method to recognize when the state of item is
changed
void itemStateChanged(ItemEvent ie)
62
Eevnt Listener Interfaces
Event Class Description
KeyListener Define three method to recognize when key is pressed,
released or typed
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
MouseListener Define five method to recognize when mouse is clicked,
enters a component, exit a component , is pressed or released.
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
63
Interface Description
MouseMotionListener Define two method to recognize when mouse is
dragged or moved.
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
MouseWheelListener Define one method to recognize when mouse wheel is
moved
void mouseWheelMoved(MouseWheelEvent
mwe)
TextListener One method when text value is changed
void textChanged(TextEvent te)
64
Eevnt Listener Interfaces
Interface Description
WindowFocusListener Defines Two method to recognize a window
gain or loses input focus
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
WindowListener Seven methods to recognize when window
is activated closed, deactivated, deiconified,
iconified, opened or quit
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing (WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowIconified(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowOpened(WindowEvent we) 65
Adapter Classes
66
1. Simplifies the creation of event handler.
2. These are useful when you want to receive and process only
some of the event that are handled by a particular event listener
interface
3. You can define a new class to act as an event listener by
extending one of the adapter classes and implementing only
those event in which you are interested.
RAJESHREE KHANDE
Adapter Classes
67
 The Adapter classes
are
1. ComponentAdap
ter
2. ContainerAdapte
r
3. FocusAdapter
4. KeyAdapter
5. MouseAdapter
6. MouseMotionAdapte
r
7. WindowAdapter
RAJESHREE KHANDE
WindowEvent Class
68
 The class has 7 type of
integer constant
1. WINDOW_ACTIVATED
2. WINDOW_CLOSED
3. WINDOW_CLOSING
4. WINDOW_DEACTIVATED
5. WINDOW_DEICONIFIED
6. WINDOW_ICONIFIED
7. WINDOW_OPENED
RAJESHREE KHANDE
WindowEvent Class- Methods
69
1. public void windowActivated(WindowEvent we)
2. public void windowClosed(WindowEvent we)
3. public void windowClosing(WindowEvent we)
4. public void windowDeactivated(WindowEvent we)
5. public void windowDeiconified(WindowEvent we)
6. public void windowOpened(WindowEvent we)
RAJESHREE KHANDE
Ad

More Related Content

What's hot (20)

Features of java
Features of javaFeatures of java
Features of java
WILLFREDJOSE W
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
JDK,JRE,JVM
JDK,JRE,JVMJDK,JRE,JVM
JDK,JRE,JVM
Cognizant
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
Rubaya Mim
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
Rubaya Mim
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 

Similar to Java Applet (20)

27 applet programming
27  applet programming27  applet programming
27 applet programming
Ravindra Rathore
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
Abhishek Khune
 
171-33B - Java Programming-Building Applets.ppt
171-33B - Java Programming-Building Applets.ppt171-33B - Java Programming-Building Applets.ppt
171-33B - Java Programming-Building Applets.ppt
BWUBTA21109
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
suraj pandey
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
rani marri
 
Java applet
Java appletJava applet
Java applet
GaneshKumarKanthiah
 
Applets in Java. Learn java program with applets
Applets in Java. Learn java program with appletsApplets in Java. Learn java program with applets
Applets in Java. Learn java program with applets
halaplay385
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
RamaPrabha24
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
arnold 7490
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
Java files and io streams
Java files and io streamsJava files and io streams
Java files and io streams
RubaNagarajan
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
mcanotes
 
Appletjava
AppletjavaAppletjava
Appletjava
DEEPIKA T
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq Hasan
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
GayathriRHICETCSESTA
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
GayathriRHICETCSESTA
 
Applet (1)
Applet (1)Applet (1)
Applet (1)
DEEPIKA T
 
Oops
OopsOops
Oops
RichaDasila
 
Applet ppt for higher understanding education
Applet ppt for higher understanding educationApplet ppt for higher understanding education
Applet ppt for higher understanding education
BhanuPriya93439
 
Applet
AppletApplet
Applet
Priyanka Pradhan
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
Abhishek Khune
 
171-33B - Java Programming-Building Applets.ppt
171-33B - Java Programming-Building Applets.ppt171-33B - Java Programming-Building Applets.ppt
171-33B - Java Programming-Building Applets.ppt
BWUBTA21109
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
rani marri
 
Applets in Java. Learn java program with applets
Applets in Java. Learn java program with appletsApplets in Java. Learn java program with applets
Applets in Java. Learn java program with applets
halaplay385
 
Java files and io streams
Java files and io streamsJava files and io streams
Java files and io streams
RubaNagarajan
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
mcanotes
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq Hasan
 
Applet ppt for higher understanding education
Applet ppt for higher understanding educationApplet ppt for higher understanding education
Applet ppt for higher understanding education
BhanuPriya93439
 
Ad

Recently uploaded (20)

How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Ad

Java Applet

  • 1. A Simple Applet 1 RAJESHREE KHANDE
  • 2. Applets 2  Applet are small program use in Internet Computing  Applet are run using Applet Viewer or Web Browser that support Java.  Applet can perform arithmetic operations, display graphics play sound, accept user input, create animations and play interactive games.  Applet do not use the main() method for initiating the execution of the code.  When loaded , automatically calls certain method of applet to start & execute the applet code. RAJESHREE KHANDE
  • 3. Applets 3  Applets are run from inside a Web Page using a special features know as applet tag  Applet can not read from or write to a file form local computer  Applet are restricted from using libraries. From other language like C & C++ . RAJESHREE KHANDE
  • 4. Applet Class 4  Applet code uses services of two classes namely Applet & Graphics  The Applet class provide life and behavior to applet through it’s method such as - init() - start() - paint() - stop() - destroy()  Applet class extends the AWT class Panel. RAJESHREE KHANDE
  • 5. Applet Class 5  When loaded , automatically calls series of Applet class methods for starting , running & stopping the applet code.  Therefore maintains a lifecycle of an applet.  The paint() method of Applet class when called, actually display the result of the applet code on the screen.  The paint() method requires a graphics object as an argument. syntax : public void paint(Graphics g) RAJESHREE KHANDE
  • 6. Skeleton of Applet 6 import java.awt.*; import java.applet.*; …………… …………… public class appletclassName extends Applet { public void init() { ……………. ……………. } public void paint(Graphics g) { ……………. ……………. } } RAJESHREE KHANDE
  • 7. Graphic class 7  public void paint(Graphics g) { … }  public says that anyone can use this method  void says that it does not return a result  A Graphics (short for “Graphics context”) is an object that holds information about a painting  It remembers what color you are using  It remembers what font you are using  You can “paint” on it (but it doesn’t remember what you have painted) RAJESHREE KHANDE
  • 8. Graphic class 8  The java.awt package defines a class named Color  There are 13 predefined colors—here are their fully- qualified names:  For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc. Color.BLACK Color.PINK Color.GREEN Color.DARK_GRAY Color.RED Color.CYAN Color.GRAY Color.ORANGE Color.BLUE Color.LIGHT_GRAY Color.YELLOW Color.WHITE Color.MAGENTA RAJESHREE KHANDE
  • 9. New Color 9  Every color is a mix of red, green, and blue  You can make your own colors: new Color( red , green , blue )  Amounts range from 0 to 255  Black is (0, 0, 0), white is (255, 255, 255)  We are mixing lights, not pigments  Yellow is red + green, or (255, 255, 0) RAJESHREE KHANDE
  • 10. Step to develop an Applet 10 1. Building an applet code ( .java file) 2. Creating an executable applet ( .class file) 3. Designing a Web page using HTML tag 4. Prepare an <APPLET> tag 5. Incorporating an <APPLET> tag into the Web page 6. Creating HTML file. 7. Testing an applet code. RAJESHREE KHANDE
  • 11. Example 11 /* applet code i.e. HelloApplet.java */ import java.awt.*; import java.applet.*; public void HelloApplet extend applet { public void paint(Graphics g) { g.drawString(“Hello applet”,10,10); } } /* HTML file */ <HTML> <HEAD> <TITLE> Welcome to Java Applet </TITLE> </HEAD> <BODY> <CENETR> <H1> Welcome to 1st applet </H1> </CENTER> <BR> <CENETR> <APPLET CODE =HelloApplet.class WIDTH =400 HEIGHT =200> </APPLET> </CENTER> </BODY> </HTML> RAJESHREE KHANDE
  • 12. Running the Applet 12  We must have the following file in the current directory 1. HelloApplet.java 2. HelloApplet.class 3. HelloApplet.html  To run an applet we require the one of the following 1 Java enable Web browser If we use this we will be able to see the entire Web Page containing the applet 2 Java appletviewer if we use appletviewer we will only see the applet output. RAJESHREE KHANDE
  • 13. Applet Life Cycle 13  Every java applet inherits a set of default behaviors from Applet Class.  When an applet is loaded, it undergoes a series of changes in it’s state.  The applet state includes 1. Born or Initialization state 2. Running State 3. Idle State 4. Dead or Destroyed state RAJESHREE KHANDE
  • 14. Applet Life Cycle ( An Applet State transition Diagram ) 14 Begin Born(Load Applet) Initialization Running Idle DeadDestroy Exit of Browser End destroy( ) start( ) paint( ) Display stop( ) start( ) Stopped RAJESHREE KHANDE
  • 15. Applet Life Cycle ( An Applet State transition Diagram ) 15 1. Initialization state - Enters in initialization state when it’s first loaded. - Achieved by calling init( ) method of an Applet class. - The applet is born (creation of object needed by applet, setting initial values, set up colors, load images and fonts. - Initialization occurs only once in the applet life cycle - Therefore override the init() method public void init() { -------- -------- -------- } Action RAJESHREE KHANDE
  • 16. Applet Life Cycle ( An Applet State transition Diagram ) 16 2. Running state - Enter in the running state when system call start( ) method of an Applet class. - This occurs automatically when applet is initialized - start() method may be called more than once. - We may override the start( ) method to create a thread to control the applet public void start() { -------- -------- -------- } Action RAJESHREE KHANDE
  • 17. Applet Life Cycle ( An Applet State transition Diagram ) 17 3. Idle or Stopped state - When applet stopped running it becomes idle - Stopping occurs when we leave the page containing the currently running applet. - We can also do so by calling stop() method explicitly. - If we use thread to run an applet then we must use stop() method to terminate the thread. - We can achieve this by overriding the stop() method. RAJESHREE KHANDE
  • 18. Applet Life Cycle ( An Applet State transition Diagram ) 18 public void stop() { -------- -------- -------- } Action RAJESHREE KHANDE
  • 19. Applet Life Cycle ( An Applet State transition Diagram ) 19 3. Dead state - An applet is said to be dead when it is removed from memory. - This occurs automatically by invoking destroy() method when we quit the browser. - It occurs only once in applet’s life cycle. - If applet has created any resource like thread, we may override destroy() method to clean up the resource. public void destroy() { -------- -------- -------- } Action RAJESHREE KHANDE
  • 20. Applet Life Cycle ( An Applet State transition Diagram ) 20 Display state - Applet moves to display state whenever some output operation has to perform on the screen. - It occurs after the applet enters into running state. - The paint( ) method is called to do this task. - Every applet will have a paint( ) method. - Override paint( ) method whenever we want to display anything on the screen. RAJESHREE KHANDE
  • 21. Applet Life Cycle ( An Applet State transition Diagram ) 21 public void paint(Graphics g) { -------- -------- -------- } Display Statement RAJESHREE KHANDE
  • 22. Difference between Applet & Application Applet Aplication Program An applet can be embedded in HTML Web page & easily downloaded over Internet An application program can not be embedded in HTML Web page. An applet runs inside java compatible container such as internet explorer or netscape navigator. Runs on command prompt using java interpreter , java.exe An applet executes under strict security limitation that reject some opreation such as accessing a file or downloading a file Has no security restriction. It is java program that is written for distribution over a network. i.e. applet is not standalone program It is standalone program It does not require main() method for it’s execution. Need main() method for it’s execution. 22
  • 23. Designing a Web Page 23 <HTML> </HTML> <! ……… > <HEAD> </HEAD> Title Tag <BODY> </BODY> <APPLET> Applet Tag </APPLET> Comment Section Head Section Body Section RAJESHREE KHANDE
  • 24. Applet Tag 24 <APPLET [ CODEBASE = codebase_URL] CODE =AppletFileName.class [ALT = alternate text] [NAME = applet_instance_name] WIDTH = pixel HEIGHT =pixel [ALIGNMENT = alignment] [VSPACE = pixel ] [HSPACE = pixel ] > [<PARAM NAME =name1 VALUE =value1 >] [<PARAM NAME =name2 VALUE =value2 >] …………………………………………. ………………………………………… </APPLET> RAJESHREE KHANDE
  • 25. Attribute of Applet Tag 25 1. CODE : =AppletFileName.class Specifies a name of the applet class to be loaded i.e the name of already compiled file 2. CODEBASE=codebase_URL(optional) Specifies the URL of the directory in which the applet resides. If applet resides in the same directory as HTML file, then this attribute is ommitted. 3. WIDTH=pixel HEIGHT=pixel This specifies width & height of the space on HTML page. RAJESHREE KHANDE
  • 26. Attribute of Applet Tag 26 4. NAME =applet_instance_name (optional) A name for the applet , so that other applet on the page may refer to this applet. Used for inter applet communication. 5. ALIGN =alignment (optional) This specifies where on the page the applet will appear. Values are : TOP,BOTTOM,LEFT,RIGHT MIDDLE,ABSMIDDLE, ABSBOTTOM, TEXTTOP and BASELINE RAJESHREE KHANDE
  • 27. Attribute of Applet Tag 27 6. HSPACE=pixel (optional) Used only when align is set to LEFT or RIGHT. Specifies horizontal blank spaces the browser should leave surrounding the applet. 7. VSPACE=pixel (optional) Used only when align is set to TOP or BOTTOM. Specifies vertical blank spaces the browser should leave surrounding the applet. 8. ALT=alternate_text (optinal) Non-Java browser will display this text where the applet would normally go.RAJESHREE KHANDE
  • 28. Applet Class 28  Applet extends the awt class Panel .  Panel extends Container which RAJESHREE KHANDE
  • 29. The java.awt package 29  The java.awt package includes classes for:  Drawing lines and shapes  Drawing letters  Setting colors  Choosing fonts  If it’s drawn on the screen, then java.awt is probably involved! RAJESHREE KHANDE
  • 30. The applet so far 30 import java.applet.Applet; import java.awt.*; RAJESHREE KHANDE
  • 31. Your applet class 31 public class Drawing extends Applet { … }  Drawing is the name of your class  Class names should always be capitalized  extends Applet says that our Drawing is a kind of Applet, but with added capabilities  Java’s Applet just makes an empty window  We are going to draw in that window  The only way to make an applet is to extend Applet RAJESHREE KHANDE
  • 32. The applet so far 32 import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { …we still need to put some code in here... } RAJESHREE KHANDE
  • 33. The paint method 33  Our applet is going to have a method to paint some colored rectangles on the screen  This method must be named paint  paint needs to be told where on the screen it can draw  This will be the only parameter it needs  paint doesn’t return any result RAJESHREE KHANDE
  • 34. The applet so far 34 import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } } RAJESHREE KHANDE
  • 35. New colors 35  Every color is a mix of red, green, and blue  You can make your own colors: new Color( red , green , blue )  Amounts range from 0 to 255  Black is (0, 0, 0), white is (255, 255, 255)  We are mixing lights, not pigments  Yellow is red + green, or (255, 255, 0) RAJESHREE KHANDE
  • 36. Setting a color 36  To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED);  g will remember this color and use it for everything until we tell it some different color RAJESHREE KHANDE
  • 37. The paint method so far 37 public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… } } RAJESHREE KHANDE
  • 38. Pixels 38  A pixel is a picture (pix) element  one pixel is one dot on your screen  there are typically 72 to 90 pixels per inch  java.awt measures everything in pixels RAJESHREE KHANDE
  • 39. Java’s coordinate system 39  Java uses an (x, y) coordinate system  (0, 0) is the top left corner  (50, 0) is 50 pixels to the right of (0, 0)  (0, 20) is 20 pixels down from (0, 0)  (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height (0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1) RAJESHREE KHANDE
  • 40. Drawing rectangles 40  There are two ways to draw rectangles:  g.drawRect( left , top , width , height );  g.fillRect(left , top , width , height ); RAJESHREE KHANDE
  • 41. The complete applet 41 import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { 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); } } RAJESHREE KHANDE
  • 42. Some more java.awt methods 42  g.drawLine( x1 , y1 , x2 , y2 );  g.drawOval( top, left , width , height );  g.fillOval( top, left , width , height );  g.drawRoundRect( top, left , width , height,xDiam,yDiam);  g.fillRoundRect( top, left , width , height, xDiam, yDiam );  g.drawArc( top, left, width , height ,startAngle, sweepAngle );  g.fillArc( top, left, width , height ,startAngle, sweepAngle );  g.drawString( string , x , y ); RAJESHREE KHANDE
  • 43. The HTML page 43  You can only run an applet in an HTML page  The HTML looks something like this:  <html> <body> <h1>DrawingApplet Applet</h1> <applet code="DrawingApplet.class" width="250" height="200"> </applet> </body> </html>  BlueJ will create this HTML for you RAJESHREE KHANDE
  • 45. Working with Font 45  AWT support multiple type of Fonts  Font have a 1. Family name : A general name of the font such as Courier 2. a logical font name : Specifies category of font, such as Monospaced. 3. a face name : Specifies a specific font such a Courier Italic  Font have encapsulated by Font class RAJESHREE KHANDE
  • 46. Methods of Font Class 46  String getFontName() returns the face name of the invoking font  String getName(): returns the logical name of the invoking font  int getSize(): returns the size, in points of the invoking font  int getStyle(): returns the Style value of the invoking font RAJESHREE KHANDE
  • 47. Methods of Font Class 47  boolean isBold(): returns true if the font includes BOLD style value o.w. returns false  boolean isItalic(): returns true if the font includes ITALIC style value o.w. returns false  boolean isPlain() returns true if the font includes PLAIN style value o.w. returns false RAJESHREE KHANDE
  • 48. Methods of Font Class 48  String toString() : Returns the string equivalent of the invoking font  Font class defines following variables 1. String name : name of the font 2. float pointSize : Size of the invoking font 3. int size : Size of the font in points 4. int style : Font style RAJESHREE KHANDE
  • 49. Determining available font 49  To know which fonts are a available on your machine we can use the String[] getAvailableFomtFamilyNames() defined by GraphicsEnvironment class  To returns an array of Font object Font[] getAllFont()  Since these method belong to GraphicsEnvironment we need GraphicsEnvironment reference to call them RAJESHREE KHANDE
  • 50. Determining available font 50  We can obtain the referenced them by getLocalGraphicsEnvironment()  This is static method GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment()  Example  RAJESHREE KHANDE
  • 51. Creating and Selecting Font 51  To select new font first construct Font object Font(String fontName, int fontStyle, intfontSize) logical /face Name  To use the created font setFont(Font obj)  Example RAJESHREE KHANDE
  • 52. Event Model 52 Java.lang.Object Java.util.EventObject Java.awt.AWTEvent ActionEvent (Click button /Menu Option) AdujustmentEvent (Moves Scrollbar) ComponentEvent (When awt component are shown ,Hidden moved /resized) ItemEvent (When list item, radio Button, check box choice menu option selected) TextEvent (when Text changes for an object ContainerEvent FocusEvent InputEvent PaintEvent WindowEvent KeyEvent MouseEvent RAJESHREE KHANDE
  • 53. Java Event handling 53  The delegation event model : The modern approach to handle the event is based on delegation event model, which defines standard and consistent mechanism to generate the event. 1. A source generate an event & sent it to one or more listener, in this listener wait until it receives an event. 2. Once received the listener process the event & return. 3. The advantages of this design is that the application logic is separated from the user interface logic that generates an event RAJESHREE KHANDE
  • 54. Java Event handling 54 4. A user element is able to delegates the processing of an event to a separate piece of code. 5. In the delegation event model , listener must register with a source in order to receive an event notification. 6. This provide a benefit that notification are sent only to the listener that want to receive them. RAJESHREE KHANDE
  • 55. Event Delegation Model 55  Event : In event Delegation model an event is an Object that describe state change in source  Event Source: 1. A source is an object that generates an event. Source may generate more than one type of event. 2. A source must register listener in order for the listener to receive notification about a specific type of event. 3. Each type of Event has its own registration method RAJESHREE KHANDE
  • 56. 56 format : public void addTypeListener(TypeListener el) Here Type : name of the Event and el : reference to event listener  Event Listener : A listener is an object that is notified when event is occur. It has two major requirement 1. must have been registered with one or more source to receive notification about the specific type of event. 2. Must Implement method to receive & process these notification Event Delegation Model RAJESHREE KHANDE
  • 57. Event Classes : EventObject 57  At the root of Java Event class hierarchy is EventObject , Which is in java.util.  EventObject contain two method 1. getSource() :returns the source of event Object getSource() 2. toString(): returns the string equivalent of the event RAJESHREE KHANDE
  • 58. Event Classes : AWTEvent 58  Defined in java.awt package & is a Subclass of EventObject  It is super class of all AWT-based event used by event delegation model RAJESHREE KHANDE
  • 59. Main Event classes in java.awt.event Event Class Description ActionEvent Generates when button is pressed, a list item is double- Clicked or menu item is selected AdjustmentEvent Generstes when Scrollbar is manipulated ComponentEvent When component is hidden, moved,resized,or becomes visible ContainerEvent When container is added or removed from container FocusEvent When component gain or losses focus ItemEvent Enerates when Checkbox or list item is clicked also occur when choice selection is made / checkable menu item is selected or deselected 59
  • 60. Main Event classes in java.awt.event Event Class Description KeyEvent Generates when input is received from keyboard MouseEvent Generates when mouse is dragged, moved clicked pressed/released. Also when mouse enters or exits a component. MouseWheelEvent Generates When mouse wheel is moved TextEvent Generates When the value of text area or text field is changed. WindowEvent Generates When window is activated .closed, deactivated, deiconified, iconified, opened or quit 60
  • 61. Event Listener Interfaces Interface Description ActionListener Define one method to receive action event void actionPerformed(ActionEvent ae) AdjustmentListener Define one method to receive adjustment event void adjustmentValueChange(AdjustmentEvent ae) ComponentListener Define four method to recognize when component is hidden , moved, resized or shown void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce) 61
  • 62. Event Listener Interfaces Interface Description ContainerListener Define two method to recognize when component is added or removed from container void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) FocusListener Define two method to recognize when component gain or losses keyboard focus void focusGained(FocusEvent fe) void focusLost(FocusEvent fe) ItemListener Define one method to recognize when the state of item is changed void itemStateChanged(ItemEvent ie) 62
  • 63. Eevnt Listener Interfaces Event Class Description KeyListener Define three method to recognize when key is pressed, released or typed void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) MouseListener Define five method to recognize when mouse is clicked, enters a component, exit a component , is pressed or released. void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) 63
  • 64. Interface Description MouseMotionListener Define two method to recognize when mouse is dragged or moved. void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me) MouseWheelListener Define one method to recognize when mouse wheel is moved void mouseWheelMoved(MouseWheelEvent mwe) TextListener One method when text value is changed void textChanged(TextEvent te) 64
  • 65. Eevnt Listener Interfaces Interface Description WindowFocusListener Defines Two method to recognize a window gain or loses input focus void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we) WindowListener Seven methods to recognize when window is activated closed, deactivated, deiconified, iconified, opened or quit void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing (WindowEvent we) void windowDeactivated(WindowEvent we) void windowIconified(WindowEvent we) void windowDeiconified(WindowEvent we) void windowOpened(WindowEvent we) 65
  • 66. Adapter Classes 66 1. Simplifies the creation of event handler. 2. These are useful when you want to receive and process only some of the event that are handled by a particular event listener interface 3. You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those event in which you are interested. RAJESHREE KHANDE
  • 67. Adapter Classes 67  The Adapter classes are 1. ComponentAdap ter 2. ContainerAdapte r 3. FocusAdapter 4. KeyAdapter 5. MouseAdapter 6. MouseMotionAdapte r 7. WindowAdapter RAJESHREE KHANDE
  • 68. WindowEvent Class 68  The class has 7 type of integer constant 1. WINDOW_ACTIVATED 2. WINDOW_CLOSED 3. WINDOW_CLOSING 4. WINDOW_DEACTIVATED 5. WINDOW_DEICONIFIED 6. WINDOW_ICONIFIED 7. WINDOW_OPENED RAJESHREE KHANDE
  • 69. WindowEvent Class- Methods 69 1. public void windowActivated(WindowEvent we) 2. public void windowClosed(WindowEvent we) 3. public void windowClosing(WindowEvent we) 4. public void windowDeactivated(WindowEvent we) 5. public void windowDeiconified(WindowEvent we) 6. public void windowOpened(WindowEvent we) RAJESHREE KHANDE