[Go to site: main page, start]

0% found this document useful (0 votes)
6 views26 pages

Advanced Java Applet Programs Guide

The document contains a series of Java applet programs demonstrating various functionalities such as lifecycle management, time display, shape drawing, bar chart rendering, sound playback, factorial calculation, window management, server-client communication, and remote method invocation (RMI). Each program is designed to showcase specific Java features and includes code snippets for implementation. The document serves as a practical guide for advanced Java programming techniques.

Uploaded by

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

Advanced Java Applet Programs Guide

The document contains a series of Java applet programs demonstrating various functionalities such as lifecycle management, time display, shape drawing, bar chart rendering, sound playback, factorial calculation, window management, server-client communication, and remote method invocation (RMI). Each program is designed to showcase specific Java features and includes code snippets for implementation. The document serves as a practical guide for advanced Java programming techniques.

Uploaded by

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

ADVANCED JAVA LAB PROGRAMS

Program1:
import java . awt.*;

import java applet.*;

public Class life cycle extends Applets

Public void paint (Graphics g)

Font f = new Font ("Times New Roman", 1,15);

g. drawString("Begin", 10,500);

g. drawLine (50, 45, 110,45);

g. drawOval (120, 25, 100, 50);

g. drawString("Initialization"240,50);

g. drawLine (170,85,170,170);
g. draw String("start()", 120, 145);
g. draw Oval (120, 175, 100, 50);

g. drawArc (180, 175, 150, 75, 45,85);

[Link] (300,175, 100, 500);

[Link] ("stopped", 420,200);


g. drawString ("Stop()", 235, 100);

g. drawArc(180, 163, 150, 75, 185,180);

g. drawLine (350, 230, 350, 320);

g. drawString ("destroy()" 370,280);

[Link] Arc(65, 190, 65, 65,55,300);

[Link]("Display", 10, 2300);

[Link] ("paint()" 70,280);

g. drawString ("start()" 235, 280);

g. drawString(" Destroyed", 230, 360);

[Link] (300, 330, 100,500);

g. downline (410, 355, 500, 355);

g. drawString("End", 520, 3607);

g. set font (f);

g. drawstring(" APPlET LIFECYCLE "100,20);

g. drawString (" BORN", 150, 50);

[Link] ("Running" 150, 200);


g. drawString ("Idle", 340, 200);

g. drawString ("Dead", 335, 355);

/* <applet code:"lifecycle. Class" height = soo width=50


</applet>

Program2:

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class appdemo extends Applet implements Runnable
{
Thread t = null;
int hours=0,minute;
int s=0,seconds=0;
String timeString="";
public void init()
{
t=new Thread(this);
[Link]();
}
public void run()
{
try
{
while(true)
{
Calendar cal=[Link]();
hours=[Link](Calendar.HOUR_OF_DAY);
if(hours>12)hours=12;
minutes=[Link]([Link]);
seconds=[Link]([Link]);
SimpleDateFormat formatter=new
SimpleDateFormat("hm:mm:ss");
Date date=[Link]();
timeString=[Link](date);
repaint();
[Link](1000);// Interval given in milliseconds
}
}
catch (Exception e) {}
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](timeString,50,50);
}
}
//<applet code="[Link]" width="300"height
"300"></applet>

program3:

import [Link].*;

import [Link].*;

public class shape extends Applet{

public void point(Graphics g)


{
[Link](25, 25, 100, 25);

[Link](25,40,100,50);

[Link](145, 40, 100, 50);


[Link] (265, 40, 50, 50);

[Link] (25, 125, 100, 50, 15,15);

[Link] (145, 125, 100, 50, 15, 15);

[Link] (25,205,100, 50);

[Link] (145, 205, 100, 50);

g.draw3DRect (25, 280, 100, 50, true);

g.draw3DRect (145, 280, 100, 50, false);

[Link] (25, 345, 100, 50, 25,75);

[Link] (145, 345, 100, 50, 125, 15);

}
}

Program4:
import java. applet.*;
import [Link].*;

<applet code = "[Link]" width = "400" height =


"400">

<param name="year 0"value="2001">

<param name="year 1"value="2002">

<param name="year 2"value="2003">

<param name "year 3"value="2004">

<param name="year 4"value="2005">

<param name="result 0"value="80">

<param name="result 1"value="90">

<param name="result 2"value="100">

<param name="result 3"value="100">


<param name="result 4"value="98">
</applet>
public class barchart extends Applet
{
int n;
String year[];
int value [];
public void int ()
{
n=5;
year = new String [n];
value = new int[n];
year [0] = get parameter ("year 0");
year [1]= get Parameter ("year 1");
year [2]= get parameter ("year 2");
year [3]= get Parameter ("year 3");
year [4] = get Parameter (" year 4");
value [0] = Integer. paresInt(get Parameter("result 0"));
Value [1]=[Link](get Parameter ("result 1"));
Value [2]= [Link](get parameter("result 2"));
value [3]= Integer. paresInt(get parameter("result 3"));
Value [4] = [Link](get Parameter("result 4"));
}

Public void Paint (Graphis g)


{
Font font = new Font("Arial", Font, BOLD, 15);

[Link](font);

for(int i=0;i<n;i++)

[Link] ([Link]);

[Link] (Year [1], 20; 50+30);

[Link]([Link]);

[Link](70,i*50+10, value [i], 40);

[Link] ([Link] of (value [i]+"%", 180, 1*50+35);


}

String msg = "Barchart from year 2001,2005";


g. setcolor([Link]);

g. setFont(font);

[Link](msg, 50, 300);

}
}

Program5:

import [Link].*;

import java. awt.*;

import java. awt. event.*;

public class Sound extends Applet implements


ActionListener
{
Button bl, b2;

Audio Clip ac;

String str =" ";

public void init()


{
b1 = new Button ("PLAY AUDIO1");

b2= new Button("PLAY AUDIO2");

add (b1);
add(b2),

[Link] ActionListener (this);

[Link] ActionListener(this);
}
public void action perforamed (ActionEvent ae)

if ([Link] Source() == b1)


{
ac=get AudioClip (get code Base()," 1.mp3");

Str+=" Audio 2",

[Link]();

Str +="played!",

repaint();
}
Public void paint (Graphics g)
{
g. draw String (Str,100,100);
}
}
/* <applet code =" Sound Class" height=500 width=500>

</applet>
Program6:

import [Link].*;

import [Link].*;

Public Class factorial Calculator extrends frame implements


Action Listener
{
Private TextField inputField;

Private Label result Label;

Public Factorial Calculator ()

SetTitle ("Factorial calculator");

SetSize (300,200);

SetLayout (new FlowLayout());

Label inputLabel: new Label (" Enter a number");


Input field = new Text field (6);

Button Calculate Button=new Button(" Calculate");

Calculate [Link] ActionListener (this);

result Label = new Label();

add (input Label);

add (int putField);

Add (Calculate Button);

Add (result Label);

add window Listener (new window Adapter()

public void window Closing (WindowEvent e)

System exit(0);
}

});

}
public void action performed (ActionEvent e)

int n = Integer. parseInt (input [Link]());

int factorial =1;

for (int=1;i<n; i++)

factorial *=i;

result Label. SetText("factorial of "+n +” is" + factorial);

Public static void main(String (] args)

Factorial Calculator Calculator = newfactorial Calculator();


Calculator. Setvisible (true);
}
}

Program7:

import java awt.*;

import java. [Link];


import java. [Link];

import java. [Link] windowAdapter;

public class window example extends frame

window example ()

add window Listener new window Adapter ()

public void window closing (window Event e)

dispose();

});

set size (400, 400);

set Layout (null);

Set visible (true);


}

public static void main(String [] args)

new window example ();


}

Program 8:

import [Link].*;

import [Link].*;

Public Class Myserver


{
Public void static void main(String[] args)
{
try
{
ServerSocket ss=new Server Socket (6666);

Socket s=ss. accept();

Data Input [Link] = new DataInputStream (s.


getinputstream)

String str= (String)dis. readLine (TFC);


[Link](“message =" + str);

Ss. Close();
}
Catch(Exception e)

{
[Link](e);

}
}

Client:

import [Link].*;

import [Link].*;

public class My client

Public static void main(String[] args)

try
{

Socket s = new Socket("localhost", 6666);


Data Input Stream dout = new DataInputStream ([Link]
Stream())

[Link] UTP(“Hello server");

[Link]();

[Link]();

Catch (Exception e)

[Link](e);

Program9:

import [Link].*;

public class sender

Public Static void main(String []args) throws Exception


{

DatagramSocket ds = new Datagram Socket();

String str = "welcome java";

Inet Address ip = InetAddress, get By name ("[Link]");

Data gram packet dp = [Link] packet ([Link] 3()

Str length, ip, 3000);

[Link] (dp);

[Link]();

}
}

server:

import [Link].*;

public Class receiver

Public static void main(String []args) throws Exception

Datagram Socket ds = new Datagram Socket (3000);

byte[] buf = new byte [1024];


Datagram packed dp = new Datagram packet (buf, 1024);

d.s receive (dp);

String str = new String ([Link](), 0, dp. get length());

[Link] (Str);

ds. Close()

program10:

import [Link].*;

import java. rmi. registry. *;

import [Link].*;

public class Myserver extends unicastRemote object


implements MyInterface.
{
public Myserver() throws RemoteException {}

public int Sum of series (into) throws Exception


{
int res;

res_(n* (n+1))/2;

return res;
}

public Static void main (String args[] throws Exception


{
MyServer ms = new Myserver();
Naming .bind (rmi. /// Peace: ms);

[Link]" sorver Ready !!!");


}
}

client:

import java rmi.",

import [Link].*;

Public Class Myclient


{
public Static void main(String args[]) throws Exception.
{
try
{
My Interface obj = (MyInterface)
[Link](“rmi://peace “))
int res a,b,c;

DataInputStream din = new DataInputStream ([Link])


System. Out. Println(" Enter the number n to find the sum:”);
a = [Link] (din readLine());

[Link] println(" The Sum of " + a”+is" + obj sum of


Series(a));

Catch (Exception e)
{
[Link]("Enter input :!!!");

}
}
}

My inter face:

Import java. rmi.*;

interface MyInterface extends Remote


{
public int Sum of Series (int n) throws Remote Exception;

Program11:

<!DOCTYPE html>

<html>
<body>

<script>

Function myfun()
{
var x, len, temp;

x= document, get Element ById("Str").value;

len = x. length;

temp = document. getElementById(" respara");

temp [Link]="block";

document. get Element ById(" res") innerHTM = len;


}
</script>

<p>Enter a String <input id="Str"> <BR>

<button Find length </button> </p>


<p id="respara" style="display: none;">
length of Given String is : <span id="res">
</span> </p>

</body>

</html>
Program12:

import [Link].*;

import java. lang. x;

import [Link] registry.*;

import java .rmi. Spiver.*;

Public class Myserver extends unicast Remote Object


implemonts My interface
{
Public Myserver() throws RomoleException

{}
Public int Strlen (String n) throws Remote Exception

String Mystr = n;

int Strsize = Mystr. length();


return Strsize;
Public Static void main(String args[]) throws exception
{
My Sever ms = new myseava();

[Link](“rmi.///peace 1”, m3);

[Link](“ server Ready!!!”);


}
}
import [Link].*;

import [Link].*;

import [Link].*;

public class Myclient

public static void main(String args[]) throws Exception


{
Try
{
My interface Obj=(Myinterface) Naming. Lookup(“rmi
peace”);

int res;

String str;

Date Input Stream din = new DataInputStream (System in);

System out println( "Enter the string to find length:”);


Str=[Link] line();

System out paintln(" The length of the


string"+str+”is”+[Link](str));

}
Catch(Exception e)
{
[Link]. Println(" Error in input!!!");
}
}
}

import [Link].*;

interface Myinterface extends Remote


{
public int strlen (String n) throws Remote Exception;
}

You might also like