0% found this document useful (0 votes)
23 views

Java Lab 9

The document provides examples of Java applet programs. It includes 6 examples of applets that demonstrate printing text, drawing shapes, handling parameters, and creating a simple moving banner. The examples are intended to teach basic applet programming concepts.
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)
23 views

Java Lab 9

The document provides examples of Java applet programs. It includes 6 examples of applets that demonstrate printing text, drawing shapes, handling parameters, and creating a simple moving banner. The examples are intended to teach basic applet programming concepts.
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
You are on page 1/ 5

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1

1. Simple Applet program to print “Hello Applet”


import java.applet.*;
import java.awt.*;
public class AppletLifeCycle extends Applet {
public void init() {
Color c=new Color(50,30,150);
System.out.println("Applet INIT");
setBackground(Color.RED);
setForeground(c);
}
public void start() {
System.out.println("Applet Start");
}
public void stop() {
System.out.println("Applet Stop");
}
public void destroy() {
System.out.println("Applet Destroy");
}
public void paint(Graphics g) {
//Font f=new Font("Tahoma",Font.PLAIN+Font.ITALIC,50);
Font f=new Font("Tahoma",1|2,50);
g.drawString("Hello Applet", 100, 180);
g.setFont(f);
g.setColor(Color.YELLOW);
g.drawString("Hello OMU", 30,100);
}
}

2. Simple Applet program to print applet stages on applet using applet life cycle
import java.applet.*;
import java.awt.*;
public class AppletDemo extends Applet {
String str = "";
public void init() {
str = str + " INIT";
}
public void start() {
str = str + " START";
}
public void stop() {
str = str + " STOP";
}

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


public void destroy() {
str = str + " DESTROY";
System.out.println("Destroyed");
}

public void paint(Graphics g) {


str = str + " PAINT";
g.setColor(Color.BLUE);
g.drawLine(5, 15, 3, 4);
g.drawRect(23, 56, 34, 34);
g.setColor(Color.RED);
g.drawRect(56, 56, 34, 34);
g.fillOval(68, 67, 23, 22);
g.setColor(Color.BLACK);
g.drawString(str, 5, 50);
}
}

3. Applet program to draw some shape using DrawLine() method


import java.applet.*;
import java.awt.*;
public class DrawLine1 extends Applet{
public void paint(Graphics g) {
for (int i=10;i<200;i+=10){
int x1=200-i;
int x2=190;
int y1=190;
int y2=i;
g.drawLine(x1,y1,x2,y2);
}
}
}

4. Adding to numbers by passing parameters in Applet programming


import java.applet.*;
import java.awt.*;
public class ParamEx extends Applet {
int a, b, sum,sub,mul,div;
public void init() {
a = Integer.parseInt(getParameter("x"));
b = Integer.parseInt(getParameter("y"));
}
public void paint(Graphics g) {
sum = a + b;
g.drawString("Addition of a,b=" + sum, 40, 60);
sub = a - b;
g.drawString("Subtraction of a,b=" + sub, 40, 80);
div = a / b;
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
g.drawString("Division of a,b=" + div, 40, 100);
mul = a *b;
g.drawString("Multiplication of a,b=" + mul, 40, 120);
}
}

5. Program to display applet by taking parameters


import java.awt.*;
import java.applet.*;
/*
<applet code="ParamDemo1.class" width=300 height=80>
<param name=fontName value=Courier>
<param name=fontSize value=14>
<param name=leading value=2>
<param name=accountEnabled value=true>
</applet>
*/
public class ParamDemo1 extends Applet {
String fontName;
int fontSize;
float leading;
boolean active;
// Initialize the string to be displayed.
public void start() {
String param;
fontName = getParameter("fontName");
if (fontName == null)
fontName = "Not Found";
param = getParameter("fontSize");
try {
if (param != null) // if not found
fontSize = Integer.parseInt(param);
else
fontSize = 0;
} catch (NumberFormatException e) {
fontSize = -1;
}
param = getParameter("leading");
try {
if (param != null) // if not found
leading = Float.valueOf(param).floatValue();
else
leading = 0;
} catch (NumberFormatException e) {
leading = -1;
}
param = getParameter("accountEnabled");
if (param != null)
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
active = Boolean.valueOf(param).booleanValue();
}
// Display parameters.
public void paint(Graphics g) {
g.drawString("Font name: " + fontName, 0, 10);
g.drawString("Font size: " + fontSize, 0, 26);
g.drawString("Leading: " + leading, 0, 42);
g.drawString("Account Active: " + active, 0, 58);
}
}

6. Program to generate a simple moving banner applet


/* This applet creates a thread that scrolls the message contained in message right to
left across the applet's window. */
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleBanner" width=100 height=50>
</applet>
*/
public class Banner extends Applet implements Runnable {
String msg = " A Simple Moving Banner.";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
}
// Start thread
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
// Entry point for the thread that runs the banner.
public void run() {
char ch;
// Display banner
for (;;) {
try {
repaint();
//paint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
if (stopFlag)
break;
} catch (InterruptedException e) {
}
}
}

// Pause the banner.


public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {
g.drawString(msg, 50, 30);
}
}
Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like