[go: up one dir, main page]

0% found this document useful (0 votes)
15 views19 pages

Tutorial - 6

The document discusses basics of Java applet programming including the applet lifecycle of init(), start(), stop(), and destroy() methods, differences between applets and applications, parameter passing in applets using getParameter(), and using repaint() to redraw the applet. It also covers event delegation using listeners, input/output in applets, and layout managers for positioning buttons and text fields within an applet.

Uploaded by

Bimal Kanu
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)
15 views19 pages

Tutorial - 6

The document discusses basics of Java applet programming including the applet lifecycle of init(), start(), stop(), and destroy() methods, differences between applets and applications, parameter passing in applets using getParameter(), and using repaint() to redraw the applet. It also covers event delegation using listeners, input/output in applets, and layout managers for positioning buttons and text fields within an applet.

Uploaded by

Bimal Kanu
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/ 19

• Basics of applet programming,

• applet life cycle,


• difference between application & applet programming,
• parameter passing in applets,
• concept of delegation event model and listener,
• I/O in applets,
• use of repaint(), NetDocuments’s(),
• get Codebase() methods,
• layout manager (basic concept),
• creation of buttons (Button class only) & text fields.
Java Applet
An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works at client side. An applet is embedded in an HTML page using the
APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.

Advantage of Applet
There are many advantages of applet. They are as follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many plateforms,
including Linux, Windows, Mac Os etc.

Drawback of Applet
Plugin is required at client browser to execute applet.
Lifecycle of Java Applet

1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed.
1.public void init(): is used to initialized the Applet. It is invoked only

once.

2.public void start(): is invoked after the init() method or browser is

maximized. It is used to start the Applet.

3.public void stop(): is used to stop the Applet. It is invoked when

Applet is stop or browser is minimized.

4.public void destroy(): is used to destroy the Applet. It is invoked only


How to run an Applet?
There are two ways to run an applet
1.By html file.
2.By appletViewer tool (for testing purpose).

Simple example of Applet by html file: myapplet.html


import java.applet.Applet; 1.<html>
import java.awt.Graphics; 2.<body>
public class First extends Applet{ 3.<applet code="First.class" width="300" height="300">
4.</applet>
public void paint(Graphics g){ 5.</body>
g.drawString("welcome",150,150); 6.</html>
}

}
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome to applet",150,150);
}

}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Difference between a Java Application and a Java Applet

Parameters Java Application Java Applet


Applets are small Java
Applications are just like a Java programs that are designed to
program that can be executed be included with the HTML
Definition
independently without using web document. They require a
the web browser. Java-enabled web browser for
execution.

The applet does not require


The application program
the main() method for its
main () method requires a main() method for execution instead init() method
its execution. is required.

The “javac” command is used Applet programs are compiled


to compile application with the “javac” command and
Compilation programs, which are then run using either the
executed using the “java” “appletviewer” command or
command. the web browser.
Java application programs have
Applets don’t have local disk
File access full access to the local file
and network access.
system and network.
Applets can only access
Applications can access all browser-specific services. They
Access level kinds of resources available on don’t have access to the local
the system. system.
First and foremost, the
installation of a Java The Java applet does not need
Installation application on the local to be installed beforehand.
computer is required.
Applications can execute the Applets cannot execute
Execution programs from the local programs from the local
system. machine.
An application program is An applet program is needed
Program needed to perform some tasks to perform small tasks or part
directly for the user. of them.
It cannot start on its own, but
Run It cannot run on its own; it it can be executed using a Java-
needs JRE to execute. enabled web browser.
Connectivity with other servers It is unable to connect to other
Connection with servers is possible. servers.
It supports the reading and It does not support the reading
Read and Write Operation writing of files on the local and writing of files on the local
computer. computer.
Executed in a more restricted
Application can access the environment with tighter
Security system’s data and resources security. They can only use
without any security services that are exclusive to
limitations. their browser.
Java applications are self-
Applet programs cannot run on
contained and require no
Restrictions their own, necessitating the
additional security because
maximum level of security.
they are trusted.
parameter passing in applets

public String getParameter(String parameterName)

import java.applet.Applet;
import java.awt.Graphics;

public class UseParam extends Applet{

public void paint(Graphics g){


String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
/* <applet code=UseParam.class height=200 width=200>
<param name=“msg”>
</applet>*/
import java.applet.*;
import java.awt.*;
public class Sampleapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Roll no:102",20,10);
g.drawString("Name:Shreya Saha",20,20);
g.drawString("Sub name: Java programing",20,30);
g.drawString("Sem:3rd-BCA_G",20,40);
}
}

/*<applet code="Sampleapplet.java" height=300 width=400></applet>*/


import java.awt.*; // Draw triangle
import javax.swing.*; int[] xPoints = { 142, 170, 155 };
int[] yPoints = { 170, 170, 150 };
public class SmileyTriangleApplet extends JApplet { Polygon triangle = new Polygon(xPoints, yPoints, 3);
public void init() {
setBackground(Color.WHITE); g2d.setColor(Color.RED);
} g2d.fill(triangle);
}
public void paint(Graphics g) { }
super.paint(g); /*<applet code="SmileyTriangleApplet.java" height=500
Graphics2D g2d = (Graphics2D) g; width=500>
</applet>*/
// Draw smiley face
g2d.setColor(Color.YELLOW);
g2d.fillOval(50, 50, 200, 200);
g2d.setColor(Color.BLACK);
g2d.drawOval(50, 50, 200, 200);
g2d.setColor(Color.BLACK);
g2d.fillOval(100, 100, 30, 30);
g2d.fillOval(180, 100, 30, 30);
g2d.setColor(Color.BLACK);
g2d.drawArc(80, 170, 140, 50, 180, 180);
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Q2 extends Applet implements Actio
nListener
{ b.addActionListener(this);
TextField t1 = new TextField(10); }
TextField t2 = new TextField(10);
TextField t3 = new TextField(10); public void actionPerformed(ActionEvent e)
Label l1 = new Label("FIRST NO=:"); {
Label l2 = new Label("SECOND NO:"); if (e.getSource() == b)
Label l3 = new Label("SUM:"); {
Button b = new Button("ADD"); int n1 = Integer.parseInt(t1.getText());
public void init() int n2 = Integer.parseInt(t2.getText());
{ t3.setText(" " + (n1 + n2));
t1.setForeground(Color = Red); }
add(l1); }
add(t1); }
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
Repaint()

The repaint method is an asynchronous method of applet class. When call to repaint method is made, it performs a request
to erase and perform redraw of the component after a small delay in time.

Syntax for Repaint() Method


public void repaint()
public void repaint(long m)
public void repaint(int a, int b, int breadth, int length)
public void repaint(long m, int a, int b, int breadth, int length)

Example:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/*<applet code="RepaintJavaExample.class" width="350" height="150"> </applet>*/
public class RepaintJavaExample extends Applet implements MouseListener
{
private int mouseX, mouseY;
private boolean mouseclicked = false;
public void init()
{
setBackground(Color.CYAN);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
mouseX = e.getX();
mouseY=e.getY();
mouseclicked = true;
repaint();
}
public void mouseEntered(MouseEvent e){};
public void mousePressed(MouseEvent e){};
public void mouseReleased(MouseEvent e){};
public void mouseExited(MouseEvent e){};
public void paint( Graphics g )
{
String str;
g.setColor(Color.RED);
if (mouseclicked)
{
str = "X="+ mouseX + "," + "Y="+ mouseY ;
g.drawString(str,mouseX,mouseY);
mouseclicked = false;
}
}
}
getCodeBase():

The getCodeBase() method returns the complete URL of the . class file that contains the applet.

import java.awt.*;
import java.applet.*; import java.net.*;

/*<applet code="Bases" width=300 height=50> </applet>*/


public class Bases extends Applet
{
public void paint(Graphics g)
{
String msg;
URL url = getCodeBase(); // get code base
msg = "Code base: " + url.toString(); g.drawString(msg, 10, 20);
url = getDocumentBase(); // get document base
msg = "Document base: " + url.toString(); g.drawString(msg, 10, 40);
}

}
Java LayoutManagers
ava BorderLayout
The BorderLayout is used to arrange the
components in five regions: north, south, east,
The Java LayoutManagers facilitates us to control the west, and center. Each region (area) may contain
positioning and size of the components in GUI forms. one component only. It is the default layout of a
LayoutManager is an interface that is implemented by all the frame or window. The BorderLayout provides five
classes of layout managers. There are the following classes that constants for each region:
represent the layout managers: 1.public static final int NORTH
1.java.awt.BorderLayout 2.public static final int SOUTH
2.java.awt.FlowLayout 3.public static final int EAST
3.java.awt.GridLayout 4.public static final int WEST
4.java.awt.CardLayout 5.public static final int CENTER
5.java.awt.GridBagLayout
6.javax.swing.BoxLayout
7.javax.swing.GroupLayout Constructors of BorderLayout class:
8.javax.swing.ScrollPaneLayout
9.javax.swing.SpringLayout etc. BorderLayout(): creates a border layout but with no gaps between
the components.
BorderLayout(int hgap, int vgap): creates a border layout with the
given horizontal and vertical gaps between the components.
import java.awt.*;
import javax.swing.*;

public class Border


{
JFrame f;
Border()
{
f = new JFrame();

// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center

f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}

You might also like