









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of applets, their inheritance hierarchy, and the life cycle methods in java. It includes examples of writing and executing applets using html and the appletviewer program. This information is useful for students in computer science courses focusing on java programming.
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Applets
java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet
Example Code 20.1: Writing a Simple Applet
// File HelloApplet.java
//step 1: importing required packages import java.awt.; import javax.swing.;
// extending class from JApplet so that our class also becomes an //applet public class HelloApplet extends JApplet {
// overriding paint method public void paint(Graphics g) {
// write code here u want to display & draw by using // Graphics object g.drawString(“Hello World”, 30 , 30);
} // end class
Simple Applet
Applet Life Cycle Methods
Let’s take a look on each method in detail and find out what they do
init()
start()
stop()
destroy()
paint
Applet Life Cycle Methods
Example Code 20.3: Animated Java Word
// method chooseColor
public Color chooseColor() {
// choosing red color value randomly int r = (int) (Math.random() * 255);
// choosing green color value randomly int g = (int) (Math.random() * 255);
// choosing blue color value randomly int b = (int) (Math.random() * 255);
// constructing a color by providing R-G-B values Color c = new Color(r, g, b);
// returning color return c; }
// method chooseFont
public Font chooseFont() {
// generating a random value that helps in choosing a font int fontChoice = (int) (Math.random() * 4) + 1;
// declaring font reference Font f = null;
// using switch based logic for selecting font switch (fontChoice) {
case 1: f = new Font("Serif", Font.BOLD + Font.ITALIC, 20); break;
case 2: f = new Font("SansSerif", Font.PLAIN, 17); break;
case 3: f = new Font("Monospaced", Font.ITALIC, 23); break;
case 4: f = new Font("Dialog", Font.ITALIC, 30); break;
} // end switch
// returns Font object return f;
} //end chooseFont
// overriding method paint public void paint(Graphics g) {
// incrementing clear counter variable. clearCounter++;
// printing 40 “java” strings on different locations by // selcting random font & color for (int i = 1; i <= 40; i++) {
// choosing random color by calling chooseColor() method Color c = chooseColor();
// setting color g2.setColor(c);
// choosing random Font by calling chooseColor() method Font f = chooseFont(); g2.setFont(f);
// drawing string “java” by calling drawJava() method drawJava(g2);
} // end for loop
// overriding paint method – discussed above public void paint(Graphics g) {
clearCounter++;
Graphics2D g2 = (Graphics2D) g;
if (clearCounter == 10) { g2.clearRect(0, 0, 1000, 700); clearCounter = 0; }
for (int i = 1; i <= 40; i++) {
Color c = chooseColor(); g2.setColor(c);
Font f = chooseFont(); g2.setFont(f);
drawJava(g2); } }
// overriding actionPerformed()of ActionListener interface // called by Timer object public void actionPerformed(ActionEvent ae) {
repaint();
// chooseColor method – discussed above public Color chooseColor() {
int r = (int) (Math.random() * 255); int g = (int) (Math.random() * 255); int b = (int) (Math.random() * 255);
Color c = new Color(r, g, b); return c;
}
// chooseFont method – discussed above public Font chooseFont() {
int fontChoice = (int) (Math.random() * 4) + 1;
Font f = null;
switch (fontChoice) {
case 1: f = new Font("Serif", Font.BOLD + Font.ITALIC, 20); break;
case 2: f = new Font("SansSerif", Font.PLAIN, 17); break;
case 3: f = new Font("Monospaced", Font.ITALIC, 23); break;
case 4: f = new Font("Dialog", Font.ITALIC, 30); break;
return f; }
// drawJava() method – discussed above public void drawJava(Graphics2D g2) {
int x = (int) (Math.random() * 1000); int y = (int) (Math.random() * 700);
g2.drawString("java", x, y);
}
} // end class