[Go to site: main page, start]

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

Javamodule 3

The document provides an overview of Java Applets, including their structure, lifecycle methods, and how to create and run them using HTML. It explains how to draw graphics, display images, and pass parameters to applets, along with various exercises for practical implementation. Additionally, it covers simple animation techniques for applets using threads and image loading.

Uploaded by

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

Javamodule 3

The document provides an overview of Java Applets, including their structure, lifecycle methods, and how to create and run them using HTML. It explains how to draw graphics, display images, and pass parameters to applets, along with various exercises for practical implementation. Additionally, it covers simple animation techniques for applets using threads and image loading.

Uploaded by

aibinshibuc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 3

Devika Suresh R
Asst. Prof
Dept. of CA & AI
• A Java Applet is a small Java program that runs inside a web browser or the appletviewer
tool.
• It was originally used to create interactive web pages (like animations, calculators, games,
etc.) — before JavaScript and modern web frameworks took over.
1. Basic structure of an Applet:

• import [Link]; → lets you use the Applet class.


• import [Link]; → used for drawing text, shapes, etc. (awt –abstract window toolkit)
• public class HelloApplet extends Applet → your applet inherits from Applet.
• paint(Graphics g) → automatically called by the system to draw on the screen.
2. Creating an HTML file to run the applet

"C:\Program Files\Java\jdk1.8.0_202\bin\javac" [Link]


"C:\Program Files\Java\jdk1.8.0_202\bin\appletviewer" [Link]

javac [Link] cd D:\JavaPrograms\JavaApplets


appletviewer [Link] javac [Link]
appletviewer [Link]

D:\JavaPrograms\JavaApplets

├── [Link]
└── [Link]
Applet Lifecycle
Method Description

init() Called once when the applet is first loaded. Initialize things here.

Called every time the applet becomes active (like reopening the
start()
page).

paint(Graphics g) Called to draw things on screen.

stop() Called when the user leaves the page.

destroy() Called when the applet is completely removed.


import [Link];
import [Link];

public class LifecycleApplet extends Applet {


public void init() {
[Link]("Applet Initialized");
}

public void start() {


[Link]("Applet Started");
}
javac [Link]
public void paint(Graphics g) { appletviewer [Link]
[Link]("Applet is running...", 20, 20);
}

public void stop() {


[Link]("Applet Stopped");
}

public void destroy() {


[Link]("Applet Destroyed");
}
}
• You can use Graphics methods to draw text, lines,
• Try to visualize this. shapes, etc.

public void paint(Graphics g) { public void paint(Graphics g) {


[Link]("Top Left", 10, 20); [Link]("Drawing Example", 50, 20);
[Link]("Middle", 100, 100); [Link](50, 30, 200, 30);
[Link]("Bottom Right", 200, 180); [Link](50, 40, 100, 60);
} [Link]([Link]);
[Link](50, 120, 100, 60);
• (0,0) : top –left corner }
• (20, 20) : 20 pixels to the right , 20 pixels down
• (40,80) : 40 pixels from the left , 80 pixels from the top

Feature Applet Java Application


Runs in Browser or AppletViewer JVM directly
Entry point init(), start() main() method
GUI AWT or Swing AWT, Swing, JavaFX
Execution Controlled by browser Controlled by user
[Link] your name in an applet.
Write a Java applet that displays your name in the applet window using drawString().
[Link] text color.
Modify the above program to print your name in three different colors at three different positions.
[Link] your college or department name.
Create an applet that displays your college name and department name in two different fonts and colors.

4. Draw basic shapes.


Write an applet to draw a line, rectangle, oval, and circle using the Graphics class methods.
5. Fill shapes with colors.
Create an applet that draws and fills three rectangles of different colors using setColor() and fillRect().
6. Draw a smiley face.
Use circles and arcs to draw a simple smiley face using drawOval() and drawArc() methods.
Passing Parameters to an Applet
• You can send values from the HTML file to your Java applet using the <param> tag.
• Inside your Java applet, you can read those values using the getParameter() method.
You can send multiple parameters using more
<param> tags
• The <param> tag must be inside the <applet> tag.
• The name in <param name="..."> should match the string inside getParameter("...").
• If a parameter is not passed, getParameter() returns null.

Displaying Images in an Applet


Applets can display images (like .jpg, .png, or .gif) using the following steps:
[Link] an image using the getImage() method.
[Link] the image using the drawImage() method of the Graphics class.
Make sure the image file ([Link]) is kept in the
same folder as your .java and .html files.
Method Purpose
getDocumentBase() Gets the URL of the HTML file that loaded the applet
getCodeBase() Gets the URL of the .class file
getImage(URL, String) Loads the image from a specified path
drawImage(Image, x, y, ImageObserver) Displays the image on the applet window

1. getDocumentBase()
This method tells the applet where the HTML file is located (its URL or folder path).
2. getCodeBase()
This gives the location (URL/path) of the compiled .class file.
Usually, getDocumentBase() and getCodeBase() are the same — unless your .class file is stored in a
different folder from your HTML.
3. getImage(URL, String)
This method actually loads an image from the given URL (folder) and filename.

img = getImage(getDocumentBase(), "[Link]");

4. drawImage(Image, x, y, ImageObserver)
This method displays the image on the applet screen.

[Link](img, 50, 50, this);

This draws the image img


50 pixels from the left and
50 pixels from the top.
The last argument ‘this’ tells Java that the applet itself will “observe” the image as it’s being drawn
(used internally).
You can even pass the image name from the HTML file as a parameter!
Exercises(try by yourself)
• Write a Java applet that displays an image centered on the screen.
• Write an applet that accepts an image file name from <param> and displays it.
• Modify the applet to display a custom message under the image.
• Write an applet that loads two images and alternates between them (hint: use repaint() and [Link]()).

Simple animation/slideshow — where multiple images change automatically, one after another.

Animation in an applet is usually done by:


[Link] multiple images.
[Link] a thread (so images can change after a small delay).
[Link] repaint() repeatedly to refresh the applet display.
Exercises
• Change the delay to make the slideshow faster or slower.
• Add 5 or more images.
• Display image names below each picture.
• Make it loop back to the first image when the last one ends (already done in % [Link]).

You might also like