Upload
ashwin-devendran
View
231
Download
0
Embed Size (px)
Citation preview
8/3/2019 Goutham Raam (1)
1/75
S.Goutham Raam
2009115031
1
WEB TECHNOLOGY RECORD
Name :- S. GOUTHAM RAAM
Roll No :- 2009115031
Class :- IT-G BATCH
Exercises Signature
APPLETS-I
6.1 Shapes in Applet
6.2 Parameter Passing
6.3 Smiley
APPLETS-II
7.1 Message Display
7.2 Rectangle (Parameter passing)
7.3 Scrolling Text
NETWORKING CLASSES
8.1 URL Class
8.2 InetAddress Class
8.3 URLConnection Class
8.4 Chat Program
UDP PROTOCOL
9.1 File Sharing
REMOTE METHOD INVOCATION
10.1 Calculator
10.2 File Transfer
8/3/2019 Goutham Raam (1)
2/75
S.Goutham Raam
2009115031
2
APPLETS
6.1 Shapes using applets
AIM :
To draw simple shapes in applet window.
CODING :
import java.awt.*;
import java.applet.*;
/*
*/
public class Shapes extends Applet
{
public void init()
{
repaint();
}
public void paint(Graphics g)
{
//Rectangle inside Oval
g.drawOval(25,75,40,40);
8/3/2019 Goutham Raam (1)
3/75
S.Goutham Raam
2009115031
3
g.drawRect(20,70,50,50);
//Oval inside Rectangle
g.drawOval(80,70,50,50);
g.drawRect(90,80,30,30);
//Cube
g.drawRect(50,150,70,70);
g.drawRect(20,180,70,70);
g.drawLine(20,180,50,150);
g.drawLine(20+70,180,50+70,150);
g.drawLine(20,180+70,50,150+70);
g.drawLine(20+70,180+70,50+70,150+70);
//Cone
g.drawOval(150,75,50,20);
g.drawLine(150,85,175,125);
g.drawLine(175,125,200,85);
}
}
OUTPUT :
8/3/2019 Goutham Raam (1)
4/75
S.Goutham Raam
2009115031
4
6.2 Parameter Passing
AIM :
To write a simple program to demonstrate Parameter
passing from HTML code to an applet code.
CODING :
import java.applet.*;
import java.awt.*;
/*
*/
public class ParamDemo extends Applet
{
String s;
8/3/2019 Goutham Raam (1)
5/75
S.Goutham Raam
2009115031
5
public void init()
{
s=getParameter("str");
if(s==null)
s="Java";
}
public void paint(Graphics g)
{
g.drawString("Hai " + s,50,50);
}
}
OUTPUT :
Hello WebTech
6.3 SMILEY
AIM :
To draw Smiley in the applet window.
CODING :
8/3/2019 Goutham Raam (1)
6/75
S.Goutham Raam
2009115031
6
import java.awt.*;
import java.applet.*;
/*
*/
public class Smiley extends Applet
{
public void init()
{
setBackground(Color.white);
}
public void paint(Graphics g)
{
g.drawString("I am Smiling",100,90);
g.setColor(Color.yellow);
g.fillOval(100,100,500,500);
g.setColor(Color.black);
g.fillOval(230,230,30,30);
g.fillOval(420,230,30,30);
g.fillArc(235,400,220,120,0,-180);
g.setColor(Color.white);
g.fillOval(700,100,500,500);
g.setColor(Color.black);
g.fillOval(830,230,30,30);
8/3/2019 Goutham Raam (1)
7/75
S.Goutham Raam
2009115031
7
g.fillOval(1020,230,30,30);
g.fillArc(850,450,200,120,0,180);
}
}
OUTPUT :
8/3/2019 Goutham Raam (1)
8/75
S.Goutham Raam
2009115031
8
APPLETS - II
7.1 Message Display
AIM :
To display a message at the centre of the applet window
which is passed as a parameter to the code.
CODING :
import java.applet.*;
import java.awt.*;
/*
*/
public class demoapp extends Applet
{
8/3/2019 Goutham Raam (1)
9/75
S.Goutham Raam
2009115031
9
public void paint(Graphics g)
{
String s;
s=getParameter("str");
g.drawString(s,246,250);
}
}
OUTPUT :
Anandh
7.2 Rectangle (Parameter Passing)
AIM :
To draw a rectangle in an applet filled with red color. The
dimensions and starting point of the rectangle should be
accepted from the HTML file. If no dimensions are given, draw
the rectangle having a width of 250 and height of 150 at 10, 10
positions.
CODING :
8/3/2019 Goutham Raam (1)
10/75
S.Goutham Raam
2009115031
10
import java.applet.*;
import java.awt.*;
/*
*/
public class RectDemo extends Applet
{
public void init()
{
setBackground(Color.black);
setForeground(Color.cyan);
}
public void paint(Graphics g)
{
int a,b,c,d;
String p,q,r,s;
p=getParameter("x");
q=getParameter("y");
r=getParameter("l");
8/3/2019 Goutham Raam (1)
11/75
S.Goutham Raam
2009115031
11
s=getParameter("b");
if(p==null || q==null || r==null || s==null)
g.fillRect(10,10,250,100);
else
{
a=Integer.parseInt(getParameter("x"));
b=Integer.parseInt(getParameter("y"));
c=Integer.parseInt(getParameter("l"));
d=Integer.parseInt(getParameter("b"));
g.fillRect(a,b,c,d);
}
}
}
OUTPUT :
8/3/2019 Goutham Raam (1)
12/75
S.Goutham Raam
2009115031
12
7.3 Scrolling Text
AIM :
To display a text in the applet window which scrolls
horizontally to and fro along the window
CODING :
import java.awt.*;
import java.applet.*;
/*
*/
public class ex extends Applet implements Runnable
{
int i=350;
boolean v=false;
8/3/2019 Goutham Raam (1)
13/75
S.Goutham Raam
2009115031
13
Thread t;
public void init()
{
setBackground(Color.black);
setForeground(Color.pink);
t=new Thread(this);
t.start();
}
public void run()
{
for(;;)
{
repaint();
}
}
public void paint(Graphics g)
{
g.drawString(Scrolling Text,i,50);
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
8/3/2019 Goutham Raam (1)
14/75
S.Goutham Raam
2009115031
14
System.out.println(e);
}
if(!v)
i--;
if(v)
i++;
if(i==690-s.length())
v=false;
if(i==0)
v=true;
}
}
OUTPUT :
8/3/2019 Goutham Raam (1)
15/75
S.Goutham Raam
2009115031
15
NETWORK PROGRAMMING
8.1 URL Class
AIM :
To write a Java program which demonstrates the URL
class.
CODING :
import java.io.*;
import java.net.*;
public class URLDemo
{
public static void main(String args[]) throws Exception
Goutham Raam
8/3/2019 Goutham Raam (1)
16/75
S.Goutham Raam
2009115031
16
{
URL u = new
URL("http://www.gmail.com:80/java/books/tutotial/#down");
System.out.println("Protocol : " + u.getProtocol());
System.out.println("Port : " + u.getPort());
System.out.println("File : " + u.getFile());
System.out.println("Reference: " + u.getRef());
System.out.println("Host : " + u.getHost());
}
}
OUTPUT :
Protocol : http
Port : 80
File : /java/books/tutotial/
Reference: down
Host : www.gmail.com
8.2 InetAddress Class
8/3/2019 Goutham Raam (1)
17/75
S.Goutham Raam
2009115031
17
AIM :
To write a Java program which demonstrates the
InetAddress class.
CODING :
import java.net.*;
import java.io.*;
public class InetDemo
{
public static void main(String args[])throws IOException
{
InetAddress addr=InetAddress.getLocalHost();
System.out.println("Inet Address : "+addr);
System.out.println("Host Address : "+addr.getHostAddress());
addr=InetAddress.getByName("120.32.23.6");
InetAddress ia[]=(InetAddress.getAllByName("255.1.2.42"));for(int i=0;i
8/3/2019 Goutham Raam (1)
18/75
S.Goutham Raam
2009115031
18
}
}
OUTPUT :
Inet Address : localhost/127.0.0.1
Host Address : 127.0.0.1
/255.1.0.8/10.7.8.2
8.3 URL Connection Class
AIM :
To write a Java program which demonstrates the
URLConnection class.
CODING :
import java.io.*;
import java.net.*;
import java.util.*;
class URLConnDemo
{
8/3/2019 Goutham Raam (1)
19/75
S.Goutham Raam
2009115031
19
public static void main(String args[])throws Exception
{
URL u=new URL("file:///C:/Java/Week 9/sample.html");
URLConnection uc=u.openConnection();
BufferedReader br = new BufferedReader(new
InputStreamReader
(uc.getInputStream()));
String str;
System.out.println("SOURCE CODE - \n");
while((str=br.readLine())!=null)
System.out.println(str);
System.out.println("\nContent type is - " +
uc.getContentType());
System.out.println("Content length is - " +
uc.getContentLength());
long d=uc.getDate();
if(d!=0)
System.out.println("Creation Date is:- " + new Date(d));
else
System.out.println("Creation Date information not
provided");
long e=uc.getExpiration();
if(e!=0)
System.out.println("Expiration Date is:- " + new Date(e));
8/3/2019 Goutham Raam (1)
20/75
S.Goutham Raam
2009115031
20
else
System.out.println("Expiration Date inforamtion not
provided");
long l=uc.getLastModified();
if(l!=0)
System.out.println("Last Modified Date is - " + new Date(l));
else
System.out.println("Last Modified Date information not
provided");
System.out.println();
}
}
OUTPUT:
Content type is - text/html
Content length is - 372
Creation Date information not provided
Expiration Date inforamtion not provided
Last Modified Date is - Tue Dec 14 15:02:00 IST 2010
8/3/2019 Goutham Raam (1)
21/75
S.Goutham Raam
2009115031
21
8.4 Chat Program
AIM :
To write a Java program for chatting between Server and
Client using socket.
CODING :
Server:-
import java.net.*;
import java.io.*;
public class Server2
{
public static void main(String args[]) throws Exception
{
ServerSocket Ser = new ServerSocket(55202);
System.out.println("Server is waiting for connection to be
accepted");
Socket s = Ser.accept();
System.out.println("Connection accepted");
BufferedReader fromsoc,fromkbd;
PrintStream tosoc;
8/3/2019 Goutham Raam (1)
22/75
S.Goutham Raam
2009115031
22
String line;
while(true)
{
fromsoc = new BufferedReader(new
InputStreamReader(s.getInputStream()));
line = fromsoc.readLine();
if(line.equals("exit"))
System.exit(0);
System.out.println("Client : " + line);
tosoc = new PrintStream(s.getOutputStream());
System.out.print("Server : ");
fromkbd = new BufferedReader(new
InputStreamReader(System.in));
line=fromkbd.readLine();
tosoc.println(line);
}
}
}
Client :-
import java.io.*;
import java.net.*;
public class Client2
8/3/2019 Goutham Raam (1)
23/75
S.Goutham Raam
2009115031
23
{
public static void main(String args[]) throws Exception
{
Socket s = new
Socket(InetAddress.getByName("localhost"),55202);
BufferedReader fromsoc,fromkbd;
PrintStream tosoc;
fromkbd = new BufferedReader(new
InputStreamReader(System.in));
fromsoc = new BufferedReader(new
InputStreamReader(s.getInputStream()));
String line;
System.out.println("Type 'exit' to terminate connection");
for(;;)
{
System.out.print("Client : ");
line = fromkbd.readLine();
if(line.equals("Exit"))
System.exit(0);
tosoc = new PrintStream(s.getOutputStream());
tosoc.println(line);
line = fromsoc.readLine();
System.out.println("Server : " + line);
}
8/3/2019 Goutham Raam (1)
24/75
S.Goutham Raam
2009115031
24
}
OUTPUT :
SERVER
Server is waiting for
connection to be accepted
Connection accepted
Client : hi
Server : hii
Client : busy??
Server : i am always vetti....
CLIENT
Type 'exit' to terminate
connection
Client : hi
Server : hii
Client : busy??
Server : i am always vetti
Client : exit
8/3/2019 Goutham Raam (1)
25/75
S.Goutham Raam
2009115031
25
UDP PROTOCOL
10.1 File Sharing Between Server & Client
AIM :
To write a Java program for file sharing between Server &
Client. Server has the directory in which the files are going to
be searched. And client mentions the file name to be accessed
and the files contents are transferred to client by server.
CODING :
Server:-
import java.net.*;
import java.io.*;
public class FileSer
{
public static void main(String args[])
{
BufferedReader fromsoc,fromfile;
8/3/2019 Goutham Raam (1)
26/75
S.Goutham Raam
2009115031
26
PrintStream tosoc;
FileInputStream fis;
ServerSocket server;
Socket s;
String name,line;
File obj,obj2;
try
{
server = new ServerSocket(6000);
System.out.println("Server is waiting");
s=server.accept();
System.out.println("Connection accepted");
fromsoc = new BufferedReader(new
InputStreamReader(s.getInputStream()));
tosoc = new PrintStream(s.getOutputStream());
obj = new File(args[0]);
if(obj.isDirectory())
{
String st[] = obj.list();
while(true)
{
name = fromsoc.readLine();
8/3/2019 Goutham Raam (1)
27/75
S.Goutham Raam
2009115031
27
if(name.equals("index"))
{
for(int i=0 ; i
8/3/2019 Goutham Raam (1)
28/75
S.Goutham Raam
2009115031
28
}
}
else
{
System.out.println("It is not a directory");
}
s.close();
server.close();
}
catch(Exception e) { }
}
}
Client:-
import java.net.*;
import java.io.*;
public class FileClient
{
public static void main(String args[])
{
BufferedReader fromsoc,fromkbd;
PrintStream tofile,tosoc;
FileOutputStream fos;
Socket s;
8/3/2019 Goutham Raam (1)
29/75
S.Goutham Raam
2009115031
29
String cmd,line,ch;
try
{
s = new Socket(InetAddress.getByName("localhost"),6000);
fromkbd = new BufferedReader(new
InputStreamReader(System.in));
tosoc = new PrintStream(s.getOutputStream());
fromsoc = new BufferedReader(new
InputStreamReader(s.getInputStream()));
while(true)
{
System.out.println("\nEnter the command : ");
cmd = fromkbd.readLine();
if(cmd.equals("index"))
{
tosoc.println(cmd);
System.out.println("\nThe files are :");
while(!(line = fromsoc.readLine()).equals("end"))
System.out.println(line);
}
else if(cmd.startsWith("get "))
{
tosoc.println(cmd.substring(4));
if((fromsoc.readLine()).equals("nofile"))
8/3/2019 Goutham Raam (1)
30/75
S.Goutham Raam
2009115031
30
{
System.out.println("\nIt is not a file");
break;
}
else
{
System.out.println("\nFILE:");
while(!(line=fromsoc.readLine()).equals("eof"))
System.out.println(line);
break;
}
}
else
{
System.out.println("\nWrong command");
}
}
s.close();
}
catch(Exception e) { }
}
}
OUTPUT :
8/3/2019 Goutham Raam (1)
31/75
S.Goutham Raam
2009115031
31
SERVER
Server is waiting
Connection accepted
CLIENT
Enter the command :
hi
Wrong command
Enter the command :
index
The files are :
File1.txt
File2.txt
Enter the command :
get File1.txt
FILE:
hi this is gouthamroshini
8/3/2019 Goutham Raam (1)
32/75
S.Goutham Raam
2009115031
32
REMOTE METHOD INVOCATION
10.1 CALCULATOR
AIM:-
To write a Java program for Simple Calculator using RMI
technique.
CODING:-
1) Interface -
import java.rmi.*;
public interface calcinter extends Remote
{public int add(int a,int b) throws RemoteException;
public int sub(int a,int b) throws RemoteException;
public int mul(int a,int b) throws RemoteException;
public float div(int a,int b) throws RemoteException;
}
2) Implementation
import java.rmi.*;
import java.rmi.server.*;
public class calcimpl extends UnicastRemoteObject implements
8/3/2019 Goutham Raam (1)
33/75
S.Goutham Raam
2009115031
33
calcinter
{
public calcimpl() throws RemoteException { }
public int add(int a,int b) throws RemoteException
{
return a+b;
}
public int sub(int a,int b) throws RemoteException
{
return a-b;
}
public int mul(int a,int b) throws RemoteException
{
return a*b;
}
public float div(int a,int b) throws RemoteException
{
return (float)a/(float)b;
}
}
3) Server
import java.rmi.*;
8/3/2019 Goutham Raam (1)
34/75
S.Goutham Raam
2009115031
34
import java.net.*;
public class calcserver
{
public static void main(String args[]) throws Exception
{
calcimpl obj=new calcimpl();
Naming.rebind("calcserver",obj);
}
}
4) Client
import java.rmi.*;
public class calcclient
{
public static void main(String args[]) throws Exception
{
calcinter
robj=(calcinter)Naming.lookup("rmi://localhost/calcserver");
int a=10,b=3;
System.out.println("Sum is " + robj.add(a,b));
System.out.println("Difference is " + robj.sub(a,b));
System.out.println("Product is " + robj.mul(a,b));
System.out.println("Quotient is " + robj.div(a,b));
8/3/2019 Goutham Raam (1)
35/75
S.Goutham Raam
2009115031
35
}
}
OUTPUT
Addition : 13
Difference : 7
Product : 30
Division : 3.333333
10.2 FILE TRANSFER
AIM:-
To write a Java program to download a file from a Remote
Machine.
CODING:-
1) Interface
import java.rmi.*;
public interface FileInterface extends Remote
{
public String download(String file) throws Exception;
}
8/3/2019 Goutham Raam (1)
36/75
S.Goutham Raam
2009115031
36
2) Implementation
import java.rmi.*;
import java.io.*;
import java.util.*;
import java.rmi.server.*;
public class FileImpl extends UnicastRemoteObject implements
FileInterface
{
public FileImpl() throws RemoteException { }
public String download(String name) throws Exception
{
String line;
File obj = new File(name);
if(!obj.isFile())
{
System.out.println("No such file exits...");
return null;
}
else
{
FileInputStream fis;
BufferedReader fromfile;
fis = new FileInputStream(obj);
8/3/2019 Goutham Raam (1)
37/75
S.Goutham Raam
2009115031
37
fromfile = new BufferedReader(new InputStreamReader(fis));
String str = new String();
while((line=fromfile.readLine())!=null)
{
str = str + " " + line;
}
return str;
}
}
}
3) Server
import java.rmi.*;
import java.net.*;
public class FileServer
{
public static void main(String args[]) throws Exception
{
FileImpl obj=new FileImpl();
Naming.rebind("FileServer",obj);
}
}
8/3/2019 Goutham Raam (1)
38/75
S.Goutham Raam
2009115031
38
4) Client
import java.rmi.*;
public class FileClient
{
public static void main(String args[]) throws Exception
{
FileInterface
robj=(FileInterface)Naming.lookup("rmi://localhost/FileServer")
;
String s=new String();
s=robj.download("hi.txt");
System.out.println(The files contents is - + \n);
System.out.println(s);
}
}
OUTPUT:-
File received . Contents :-
Hi this is goutham raam I am a pandu boy
8/3/2019 Goutham Raam (1)
39/75
S.Goutham Raam
2009115031
39
AWT & EVENT HANDLING
11.1 ADD & SUBTRACT
AIM:-
To create an applet with three TextFields and two buttons
Add and Subtract. User will enter two values in the TextFields.
When the button Add is pressed, the addition of the two values
should be displayed in the third TextField. Same the Subtract
button should perform the subtraction operation.
CODING:-
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class AritApp extends Applet implements ActionListener
{
TextField tf1,tf2,tf3;
Button b1,b2;
public void start()
{
tf1=new TextField();
tf2=new TextField();
add(tf1);
add(tf2);
b1=new Button("Add");
b2=new Button("Subtract");
b1.addActionListener(this);
b2.addActionListener(this);add(b1);
8/3/2019 Goutham Raam (1)
40/75
S.Goutham Raam
2009115031
40
add(b2);
tf3=new TextField();
add(tf3);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
int n1,n2;
n1=Integer.parseInt(tf1.getText());
n2=Integer.parseInt(tf2.getText());
if(s.equals("Add"))
tf3.setText(Integer.toString(n1+n2));else if(s.equals("Subtract"))
tf3.setText(Integer.toString(n1-n2));
}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
41/75
S.Goutham Raam
2009115031
41
11.2 TABLE
AIM:-
To create an applet/application with TextField, a Button
and a Listbox. User has to enter a number in the TextField.
When user clicks on the button, the arithmetic table for thatnumber should be displayed in the ListBox. If the user repeats
this process the ListBox should be cleared and refilled by the
latest values.
CODING:-
8/3/2019 Goutham Raam (1)
42/75
S.Goutham Raam
2009115031
42
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*
*/
public class Table extends Applet implements ActionListener
{
TextField NumBox;
Button b1;
List disp;
public void start(){
NumBox=new TextField();
add(NumBox);
b1=new Button("Show Table");
add(b1);
disp = new List(10);
add(disp);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
disp.removeAll();
String str=ae.getActionCommand();
int num,i;
int table[] = new int[11];num=Integer.parseInt(NumBox.getText());
for(i=1;i
8/3/2019 Goutham Raam (1)
43/75
S.Goutham Raam
2009115031
43
repaint();
}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
44/75
S.Goutham Raam
2009115031
44
11.3 MENU BAR COLOR & FONT
AIM:-
To develop an application with a Menu File and twomenuitems Color and Font. The submenu of the menuitem
Color will contain different colors which when selected should
change the background of the applet. The submenu of the
menuitem Font should contain the list of Fonts. Create a
TextField in the center of the container. When the font is
selected from the font list of menu, the TextField text should be
appeared in that font.
CODING:-
import java.awt.event.*;
/*
*/
public class MenuBarDemo extends Applet implementsActionListener
{
Font f1,f2,f3;
TextField t=new TextField(20);
public void start()
{
Frame f=new Frame();
f.setVisible(true);f.setSize(300,300);
f.setTitle("Font & Color ");
add(t);
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
Menu font=new Menu("Font");MenuItem font1=new MenuItem("Times New Roman");
8/3/2019 Goutham Raam (1)
45/75
S.Goutham Raam
2009115031
45
MenuItem font2=new MenuItem("Garamond");
MenuItem font3=new MenuItem("Arial");
font1.addActionListener(this);
font2.addActionListener(this);
font3.addActionListener(this);
font.add(font1);
font.add(font2);
font.add(font3);
Menu color=new Menu("Color");
MenuItem color1=new MenuItem("Black");
MenuItem color2=new MenuItem("Blue");
MenuItem color3=new MenuItem("Green");color1.addActionListener(this);
color2.addActionListener(this);
color3.addActionListener(this);
color.add(color1);
color.add(color2);
color.add(color3);
mb.add(font);
mb.add(color);
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if(st.equals("Black"))
setBackground(Color.black);
else if(st.equals("Blue"))setBackground(Color.blue);
else if(st.equals("Green"))
setBackground(Color.green);
if(st.equals("Times New Roman"))
{
f1=new Font("TimesNewRoman",Font.ITALIC,12);
t.setFont(f1);
8/3/2019 Goutham Raam (1)
46/75
S.Goutham Raam
2009115031
46
}
else if(st.equals("Garamond"))
{
f2=new Font("Garamond",Font.BOLD,12);
t.setFont(f2);
}
else if(st.equals("Arial"))
{
f3=new Font("Arial",Font.BOLD,12);
t.setFont(f3);
}
repaint();}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
47/75
S.Goutham Raam
2009115031
47
11.4 LOGIN SCREEN
AIM:-To develop an Login Screen GUI using appropriate layout
managers.
CODING:-
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
*/
public class LogDemo extends Applet
{
Label name,pass;
Button ok,can;TextField namet,passt;
public void start()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
name=new Label("Login",Label.LEFT);
add(name);
namet=new TextField(20);
add(namet);pass=new Label("Password",Label.LEFT);
add(pass);
passt=new TextField(20);
add(passt);
passt.setEchoChar('*');
ok=new Button("OK");
add(ok);can=new Button("Cancel");
8/3/2019 Goutham Raam (1)
48/75
S.Goutham Raam
2009115031
48
add(can);
}
}
OUTPUT:-
11.5 CHECK BOX & RADIO BUTTONS
AIM:-
To develop a simple GUI screen with Checkboxes and
Radio buttons.
CODING:-
import java.awt.*;
8/3/2019 Goutham Raam (1)
49/75
S.Goutham Raam
2009115031
49
import java.applet.*;
/*
*/
public class awt6 extends Applet
{
Label l1,l2,l3,l4,l5,l6;
TextField tf,tf2;
Checkbox c1,c2,c3,c4,c5,c6;
CheckboxGroup cgp;
public void start()
{
setLayout(new GridLayout(0,2));l1=new Label("Your Name:",Label.LEFT);
add(l1,FlowLayout.LEFT);
tf=new TextField(10);
add(tf);
l2=new Label("Your Class:");
add(l2);
l3=new Label("Your Hobbies:");
add(l3);
cgp=new CheckboxGroup();
c1=new Checkbox("FY",cgp,false);
c2=new Checkbox("SY",cgp,false);
c3=new Checkbox("TY",cgp,false);
c4=new Checkbox("Music",false);
c5=new Checkbox("Dance",false);
c6=new Checkbox("Sports",false);add(c1);
add(c4);
add(c2);
add(c5);
add(c3);
add(c6);
tf2=new TextField("Name:---, Class:---, Hobbies---",20);
add(tf2);
8/3/2019 Goutham Raam (1)
50/75
S.Goutham Raam
2009115031
50
}
}
OUTPUT:-
11.6 MENUS & SUB MENUS
AIM:-
To develop a Menu bar with Menu Items and Sub Items.
8/3/2019 Goutham Raam (1)
51/75
S.Goutham Raam
2009115031
51
CODING:-
import java.applet.*;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
*/
public class awt7
{public static void main(String args[])
{
Frame f=new Frame("Frame Menu");
f.setVisible(true);
f.setSize(500,500);
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
Menu m=new Menu("File");
Menu n=new Menu("Edit");
Menu v=new Menu("View");
Menu m1=new Menu("New..");
MenuItem m2=new MenuItem("Open");
MenuItem m3=new MenuItem("Save");
MenuItem m4=new MenuItem("Exit");
MenuItem m5=new MenuItem("Cut");MenuItem m6=new MenuItem("Paste");
MenuItem m7=new MenuItem("File");
MenuItem m8=new MenuItem("Folder");
Menu c1=new Menu("Ruler");
CheckboxMenuItem m9=new CheckboxMenuItem("Toolbars");
CheckboxMenuItem c2=new CheckboxMenuItem("Standard");
CheckboxMenuItem c3=new CheckboxMenuItem("Format");
CheckboxMenuItem c4=new CheckboxMenuItem("Drawing");
8/3/2019 Goutham Raam (1)
52/75
S.Goutham Raam
2009115031
52
m1.add(m7);
m1.add(m8);
m.add(m1);
m.add(m2);
m.add(m3);
m.addSeparator();
m.add(m4);
n.add(m5);
n.add(m6);
c1.add(c2);
c1.add(c3);
c1.add(c4);v.add(c1);
v.add(m9);
v.add(new CheckboxMenuItem("Drawing",false));
mb.add(m);
mb.add(n);
mb.add(v);
}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
53/75
S.Goutham Raam
2009115031
53
11.7 FIND & REPLACE
AIM:-
To write a Java program to design a screen containing
four text fields. First for the text, second for what to find andthird for replace. Display result in the fourth text field. Also
display the count of total no. of occurrences. The button clear
to clear the text boxes.
CODING:-
import java.applet.*;import java.awt.*;
8/3/2019 Goutham Raam (1)
54/75
S.Goutham Raam
2009115031
54
import java.awt.event.*;
import java.util.*;
/*
*/
public class FNR2 extends Applet implements ActionListener
{
Label l1,l2,l3,l4;
Button b1,b2,b3;
TextField t1,t2,t3,t4,t5,t6;
public void start()
{
setLayout(new FlowLayout(FlowLayout.LEFT));t1=new TextField(" Find and Replace",35);
add(t1);
l1=new Label("Enter Text",Label.LEFT);
add(l1,FlowLayout.CENTER);
t2=new TextField(30);
add(t2);
l2=new Label("Text To Find",Label.LEFT);
add(l2);
t3=new TextField(25);
add(t3);
l3=new Label("Text To Replace",Label.LEFT);
add(l3);
t4=new TextField(22);
add(t4);
l4=new Label("No.of Occurences",Label.LEFT);add(l4);
t5=new TextField(5);
add(t5);
t6=new TextField(25);
add(t6);
b1=new Button("FIND");
add(b1);
b2=new Button("REPLACE");
8/3/2019 Goutham Raam (1)
55/75
S.Goutham Raam
2009115031
55
add(b2);
b3=new Button("CLEAR");
add(b3);
t2.addActionListener(this);
t3.addActionListener(this);
t4.addActionListener(this);
t5.addActionListener(this);
t6.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}public void actionPerformed(ActionEvent ae)
{
int c=0;
String st=ae.getActionCommand();
String m=t2.getText();
String n=t3.getText();
String r=t4.getText();
StringTokenizer s=new StringTokenizer(m);
String mm;
String p=new String();
if(st.equals("FIND"))
{
if(m!=null)
{
while(s.hasMoreTokens()){
mm=s.nextToken();
if(n.equals(mm))
{
c++;
}
}
}
8/3/2019 Goutham Raam (1)
56/75
S.Goutham Raam
2009115031
56
t5.setText(Integer.toString(c));
}
if(st.equals("REPLACE"))
{
if(m!=null)
{
while(s.hasMoreTokens())
{
mm=s.nextToken();
if(n.equals(mm))
{
p=p+r;p=p+" ";
}
else
{
p=p+mm;
p=p+" ";
}
}
}
t6.setText(p);
}
if(st.equals("CLEAR"))
{
t2.setText("");
t3.setText("");t4.setText("");
t5.setText("");
t6.setText("");
}
repaint();
}
}
8/3/2019 Goutham Raam (1)
57/75
S.Goutham Raam
2009115031
57
OUTPUT:-
8/3/2019 Goutham Raam (1)
58/75
S.Goutham Raam
2009115031
58
SWINGS12.1. LOGIN SCREEN
AIM:-
Create a login form which contains a user id, password
field and two buttons, submit and reset. If the user id and
password field is left blank, then on click of submit button,
show a message to the user to fill in the fields. On click of reset
button, clear the fields.
CODE:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
/*
*/
public class JDemo1 extends JApplet implements ActionListener
{
JTextField id;
JPasswordField pw;
JLabel pass,userid;
8/3/2019 Goutham Raam (1)
59/75
S.Goutham Raam
2009115031
59
JButton s,r;
public void start()
{
setLayout(new GridLayout(0,2));
userid=new JLabel("UserID : ");
id=new JTextField(20);
pass=new JLabel("Password : ");
pw=new JPasswordField(20);
pw.setEchoChar('*');
s= new JButton("Submit");
r= new JButton("Reset");
s.addActionListener(this);
r.addActionListener(this);
add(userid);
add(id);
add(pass);
add(pw);
add(s);
add(r);
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if(st.equals("Submit"))
8/3/2019 Goutham Raam (1)
60/75
S.Goutham Raam
2009115031
60
{
JOptionPane jp=new JOptionPane();
if( (id.getText().equals("")) || (pw.getText().equals("")) )
{
jp.showMessageDialog(null,"Empty
Fields","",JOptionPane.INFORMATION_MESSAGE);
}
}
if(st.equals("Reset"))
{
id.setText("");
pw.setText("");
}
repaint();
}
}
8/3/2019 Goutham Raam (1)
61/75
S.Goutham Raam
2009115031
61
OUTPUT:-
12.2 COPYING LIST
AIM:-
To create two lists using JList class with a button. On click
of that button, all selected items in one list are copied to the
other list.
8/3/2019 Goutham Raam (1)
62/75
S.Goutham Raam
2009115031
62
CODE:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
/*
*/
public class JDemo2 extends JApplet implements ActionListener{
JList list1,list2;
JButton copy;
String a[]={"One","Two","Three","Four"};
public void start(){
setLayout(new FlowLayout(FlowLayout.LEFT));
list1=new JList(a);
add(list1);
copy=new JButton("Copy");
add(copy);
copy.addActionListener(this);
list2=new JList();
add(list2);
}
8/3/2019 Goutham Raam (1)
63/75
S.Goutham Raam
2009115031
63
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Copy"))
{
Object o[]=list1.getSelectedValues();
String b[]=new String[o.length];
for(int i=0;i
8/3/2019 Goutham Raam (1)
64/75
S.Goutham Raam
2009115031
64
12.3. TABBED PANE
AIM:-
To create a tabbed pane and place Login Screen in first
pane and CopyList screen in second pane.
CODE:-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class JDemo3 extends JApplet
{
public void init()
{
JTabbedPane jtp=new JTabbedPane();
jtp.addTab("Login",new LoginPanel());
jtp.addTab("Tabs",new TabsPanel());
add(jtp);
}
8/3/2019 Goutham Raam (1)
65/75
S.Goutham Raam
2009115031
65
}
class LoginPanel extends JPanel implements ActionListener
{
JTextField id;
JPasswordField pw;
JLabel pass,userid;
JButton s,r;
public LoginPanel()
{
setLayout(new FlowLayout(FlowLayout.LEFT,100,100));
userid=new JLabel("UserID : ");
id=new JTextField(20);
pass=new JLabel("Password : ");
pw=new JPasswordField(20);
pw.setEchoChar('*');
s= new JButton("Submit");
r= new JButton("Reset");
s.addActionListener(this);
r.addActionListener(this);
add(userid);
add(id);
add(pass);
add(pw);
add(s);
8/3/2019 Goutham Raam (1)
66/75
S.Goutham Raam
2009115031
66
add(r);
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if(st.equals("Submit"))
{
JOptionPane jp=new JOptionPane();
if( (id.getText().equals("")) || (pw.getText().equals("")) )
{
jp.showMessageDialog(null,"Empty
Fields","",JOptionPane.INFORMATION_MESSAGE);
}
}
if(st.equals("Reset"))
{
id.setText("");
pw.setText("");
}
repaint();
}
}
class TabsPanel extends JPanel implements ActionListener
8/3/2019 Goutham Raam (1)
67/75
S.Goutham Raam
2009115031
67
{
JList list1,list2;
JButton copy;
String a[]={"One","Two","Three","Four"};
public TabsPanel()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
list1=new JList(a);
add(list1);
copy=new JButton("Copy");
add(copy);
copy.addActionListener(this);
list2=new JList();
add(list2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Copy"))
{
Object o[]=list1.getSelectedValues();
String b[]=new String[o.length];
for(int i=0;i
8/3/2019 Goutham Raam (1)
68/75
S.Goutham Raam
2009115031
68
}
list2.setListData(b);
}
repaint();
}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
69/75
S.Goutham Raam
2009115031
69
SWINGS & RMI
13.1. CALCULATOR
AIM:-
To design a Simple Calculator using swings and RMI.
CODE:-
1) Interface-
import java.rmi.*;
public interface inter extends Remote
{
public int add(int a,int b) throws RemoteException;
public int sub(int a,int b) throws RemoteException;
public int mul(int a,int b) throws RemoteException;
public double div(int a,int b) throws RemoteException;
}
2) Implementation-
import java.rmi.*;
import java.rmi.server.*;
8/3/2019 Goutham Raam (1)
70/75
S.Goutham Raam
2009115031
70
public class impl extends UnicastRemoteObject implements
inter
{
public void impl() throws RemoteException { }
public int add(int a,int b) throws RemoteException
{
return (a+b);
}
public int sub(int a,int b) throws RemoteException
{
return (a-b);
}
public int mul(int a,int b) throws RemoteException
{
return (a*b);
}
public double div(int a,int b) throws RemoteException
{
return ((double)a/(double)b);
}
}
8/3/2019 Goutham Raam (1)
71/75
S.Goutham Raam
2009115031
71
3) Server
import java.rmi.*;
import java.net.*;
public class server
{
public static void main(String args[]) throws Exception
{
impl implobj=new impl();
Naming.rebind("CALC",implobj);
}
}
4) Client
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
public class Client1 extends Frame implements ActionListener
{
Button b1,b2,b3,b4;
TextField t1,t2,t3;
Label l1,l2,l3;
inter obj;
public Client1() throws Exception
{
8/3/2019 Goutham Raam (1)
72/75
S.Goutham Raam
2009115031
72
obj=(inter)Naming.lookup("rmi://localhost/CALC");
setLayout(new FlowLayout());
l1=new Label("number 1");
add(l1);
t1=new TextField(10);
add(t1);
l2=new Label("number 2");
add(l2);
t2=new TextField(10);
add(t2);
l3=new Label("answer");
add(l3);
t3=new TextField(10);
add(t3);
b1=new Button("Add");
add(b1);
b2=new Button("SUb");
add(b2);
b3=new Button("MUl");
add(b3);
b4=new Button("Div");
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
8/3/2019 Goutham Raam (1)
73/75
S.Goutham Raam
2009115031
73
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
int a,b;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
String s;
if(ae.getSource()==b1)
{
s=Integer.toString(obj.add(a,b));
t3.setText(s);
}
if(ae.getSource()==b2)
{
s=Integer.toString(obj.sub(a,b));
t3.setText(s);
}
if(ae.getSource()==b3)
{
s=Integer.toString(obj.mul(a,b));
8/3/2019 Goutham Raam (1)
74/75
S.Goutham Raam
2009115031
74
t3.setText(s);
}
if(ae.getSource()==b4)
{
s=Double.toString(obj.div(a,b));
t3.setText(s);
}
}
catch(Exception e) {}
}
public static void main(String args[]) throws Exception
{
Client1 c=new Client1();
c.setVisible(true);
}
}
OUTPUT:-
8/3/2019 Goutham Raam (1)
75/75
S.Goutham Raam
2009115031