Docsity

Java Applets: Understanding and Implementation, Lecture notes of Applications of Computer Sciences

Partial preview of the text

Download Java Applets: Understanding and Implementation and more Lecture notes Applications of Computer Sciences in PDF only on Docsity!

APPLET

An applet is a Java program that runs in a Web browser.

Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Any applet in Java is a class that extends the java.applet.Applet class.

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 platforms, including Linux, Windows, Mac Os etc.

Lifecycle of Applet

There are 5 lifecycle methods of Applet, Those are

public void init(): is used to initialized the Applet. It is invoked only once.

public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.

public void paint(Graphics g): is invoked immediately after the start() method, and this method helps to create Applet’s GUI such as a colored background, drawing and writing.

public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.

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

Remember:

java.applet.Applet class provides 4 methods ( init,start,stop & destroy ) and java.awt.Graphics class provides 1 method ( paint ) to create Applet.

Simple example of Applet

 To execute an Applet, First Create an applet and compile it just like a simple java program.

First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet { public void paint(Graphics g){ g.drawString(“Welcome to Applet",50,150); } } Compile: D:> javac First.java D:> After successful compilation, we get First.class file.

Displaying Graphics in Applet

java.awt.Graphics class provides many methods for graphics programming.

The Commonly used methods of Graphics class:

  • drawString(String str, int x, int y): is used to draw the specified string.
  • drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
  • fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height.
  • drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height.
  • fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
  • drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2).
  • setColor(Color c): is used to set the graphics current color to the specified color.
  • setFont(Font font): is used to set the graphics current font to the specified font.

Example: GraphicsDemo.java

import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); } }

Components of Applets

 The components of AWT are the components of Applet ,i.e we can use AWT components (Button,TextField,Checkbox, TextArea,Choice & etc.…) in applet.

 As we perform event handling in AWT or Swing, we can perform it in applet also.

 Let's see the simple example of components and event handling in applet that prints a message by click on the button.

JApplet Class

  • As we prefer Swing to AWT.
  • Now we can use JApplet that can have all the controls of swing.
  • The JApplet class extends the Applet class.
  • The components of swing are the components of JApplet ,i.e we can use swing components (JButton,JTextField,JCheckBox, JTextArea,JList & etc.…) in JApplet.