Ex No:1 Implementing Gui Components
Ex No:1 Implementing Gui Components
Ex No:1 Implementing Gui Components
EX NO:1
IMPLEMENTING GUI COMPONENTS
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class first extends Applet implements ActionListener
{
int i=0;
TextField tf1=new TextField();
TextField tf2=new TextField();
TextField tf3=new TextField();
CheckboxGroup grp1=new CheckboxGroup();
Checkbox r1=new Checkbox("Male",grp1,true);
Checkbox r2=new Checkbox("Female",grp1,false);
CheckboxGroup grp2=new CheckboxGroup();
Checkbox c1=new Checkbox("1gb",grp2,true);
Checkbox c2=new Checkbox("2gb",grp2,false);
Checkbox[ ] ck=new Checkbox[5];
Scrollbar s=new Scrollbar();
List l=new List(2);
int k=1;
Button b=new Button("Click me");
public void init()
{
add(s);
setLayout(new FlowLayout(1));
l.add("engineer");
l.add("business");
l.add("student");
setBackground(Color.green);
add(new Label("name"));
add(tf1);
add(new Label(" "));
add(new Label("username"));
add(tf2);
add(new Label(" "));
add(new Label("password"));
add(tf3);
add(new Label(""));
add(new Label("gender"));
add(r1);
add(r2);
add(new Label("capacity"));
add(c1);
add(c2);
add(new Label("choose interest"));
add(ck[0]=new Checkbox("interest"));
add(ck[1]=new Checkbox("painting"));
add(ck[2]=new Checkbox("dancing"));
add(ck[3]=new Checkbox("singing"));
2
add(new Label("choose occupation"));
add(l);
add(new Label(" "));
add(b);
b.addActionListener(this);
}
public void paint(Graphics g)
{
tf3.setEchoChar('*');
if(i==1)
{
System.out.println("------------------------------------");
System.out.println("detaild submitted");
System.out.println("username"+tf2.getText());
System.out.println("gender"+grp1.getSelectedCheckbox().getLabel());
System.out.println("capacity"+grp2.getSelectedCheckbox().getLabel());
System.out.println("------------------------------------");
i=0;
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
i=1;
repaint();
}
}
}
3
EX NO:2
BORDER LAYOUT
import java.awt.*;
import java.applet.*;
import java.util.*;
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This is across the top"),BorderLayout.NORTH);
add(new Label("The footer message might to here"),BorderLayout.SOUTH);
add(new Button("Right"),BorderLayout.EAST);
add(new Button("Left"),BorderLayout.WEST);
String msg="the reasonable man adapths"+"himselfto the worldn"+" -George Bernad
Shawn\n";
add(new TextArea(msg),BorderLayout.CENTER);
}
}
4
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FlowLayoutDemo extends Applet implements ItemListener
{
String msg="";
Checkbox win95,winNT,solaris,mac;
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
win95=new Checkbox("windows95",null,true);
winNT=new Checkbox("windowsNT");
solaris=new Checkbox("Solaris");
mac=new Checkbox("Mac OS");
add(win95);
add(winNT);
add(solaris);
add(mac);
win95.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Current state";
g.drawString(msg,6,80);
msg="Windows95/XP:"+win95.getState();
g.drawString(msg,6,100);
msg="WindowsNT/2000:"+winNT.getState();
g.drawString(msg,6,120);
msg="solaris:"+solaris.getState();
g.drawString(msg,6,140);
msg="mac:"+mac.getState();
g.drawString(msg,6,160);
}
}
5
CARD LAYOUT DEMO
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CardLayoutDemo" width=300 height=100>
</applet>
*/
public class CardLayoutDemo extends Applet implements
ActionListener, MouseListener {
Checkbox chkVB, chkASP, chkJ2EE, chkJ2ME;
Panel pnlTech;
CardLayout cardLO;
Button btnMicrosoft, btnJava;
public void init() {
btnMicrosoft = new Button("Microsoft Products");
btnJava = new Button("Java Products");
add(btnMicrosoft);
add(btnJava);
cardLO = new CardLayout();
pnlTech = new Panel();
pnlTech.setLayout(cardLO);
chkVB = new Checkbox("Visual Basic");
chkASP = new Checkbox("ASP");
chkJ2EE = new Checkbox("J2EE");
chkJ2ME = new Checkbox("J2ME");
Panel pnlMicrosoft = new Panel();
pnlMicrosoft.add(chkVB);
pnlMicrosoft.add(chkASP);
Panel pnlJava = new Panel();
pnlJava.add(chkJ2EE);
pnlJava.add(chkJ2ME);
pnlTech.add(pnlMicrosoft, "Microsoft");
pnlTech.add(pnlJava, "Java");
add(pnlTech);
btnMicrosoft.addActionListener(this);
btnJava.addActionListener(this);
addMouseListener(this);
}
public void mousePressed(MouseEvent me) {
cardLO.next(pnlTech);
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void actionPerformed(ActionEvent ae) {
6
if(ae.getSource() == btnMicrosoft) {
cardLO.show(pnlTech, "Microsoft");
}
else {
cardLO.show(pnlTech, "Java");
}
}
}
import java.awt.*;
import java.applet.*;
public class GridLayoutDemo extends Applet
{
static final int n=4;
public void init()
{
setLayout(new GridLayout(n,n));
setFont(new Font("name",Font.BOLD,24));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int k=i*n+j;
if(k>0)
add(new Button(""+k));
}}}}
7
EX NO:3
COLOR PALATTE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ColorPalette extends Applet implements ActionListener,ItemListener
{
Button btnRed,btnGreen,btnBlue;
String str="";
CheckboxGroup cbgColor;
CheckboxGroup cbgImage;
Checkbox optFore,optBack;
Checkbox optMb,optWsb;
Image imgMb,imgWsb;
TextArea txtaComments =new TextArea("",5,30);
public void init()
{
setLayout(new GridLayout(4,3));
cbgColor=new CheckboxGroup();
cbgColor=new CheckboxGroup();
Label lblColor=new Label("Select the Area:");
Label lblImage=new Label("Select the Image:");
optFore=new Checkbox("Foreground",cbgColor,true);
optBack=new Checkbox("Background",cbgColor,false);
optMb=new Checkbox("hce-mainblock",cbgImage,true);
optWsb=new Checkbox("hce-workshop block",cbgImage,false);
btnRed=new Button("Red");
btnGreen=new Button("Green");
btnBlue=new Button("Blue");
imgMb=getImage(getDocumentBase(),getParameter("hcemb"));
imgWsb=getImage(getDocumentBase(),getParameter("hcewsb"));
add(btnRed);
add(btnGreen);
add(btnBlue);
add(lblColor);
add(optFore);
add(optBack);
add(lblImage);
add(optMb);
add(optWsb);
add(txtaComments);
optFore.addItemListener(this);
optBack.addItemListener(this);
optMb.addItemListener(this);
optWsb.addItemListener(this);
btnRed.addActionListener(this);
btnGreen.addActionListener(this);
btnBlue.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
8
{
str=cbgColor.getSelectedCheckbox().getLabel();
if(ae.getSource()==btnRed && str.equals("Background"))
{
txtaComments.setBackground(Color.red);
}
if(ae.getSource()==btnRed && str.equals("Foreground"))
{
txtaComments.setForeground(Color.red);
}
if(ae.getSource()==btnGreen && str.equals("Background"))
{
txtaComments.setBackground(Color.green);
}
if(ae.getSource()==btnGreen && str.equals("Background"))
{
txtaComments.setForeground(Color.green);
}
if(ae.getSource()==btnBlue && str.equals("Background"))
{
txtaComments.setBackground(Color.blue);
}
if(ae.getSource()==btnBlue && str.equals("Foreground"))
{
txtaComments.setForeground(Color.blue);
}
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
if(optMb.getState()==true)
g.drawImage(imgMb,200,400,this);
if(optWsb.getState()==true)
g.drawImage(imgMb,200,400,this);
}
}
9
EX NO:4
DOWNLOADING HOMEPAGE
import java.net.*;
import java.io.*;
public class SourceViewer
{
public static void main(String[] args)
{
if(args.length>0);
{
try
{
URL u=new URL(args[0]);
InputStream in=u.openStream();
in=new BufferedInputStream(in);
Reader r=new InputStreamReader(in);
int c;
while((c=r.read())!=-1)
{
System.out.print((char) c);
}
}
catch(MalformedURLException ex)
{
System.err.println(args[0] +"is not a parseable URL");
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
}
10
DISPLAYING THE CONTENTS OF HOMEPAGE
HEADER VIEWER
import java.net.*;
import java.io.*;
public class HeaderViewer
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
{
try
{
URL u=new URL(args[0]);
URLConnection uc=u.openConnection();
System.out.println("Content-type:"+uc.getContentType());
System.out.println("Content-encoding:"+uc.getContentEncoding());
System.out.println("Date:"+new Date(uc.getDate()));
System.out.println("Late modified:"+new Date(uc.getLastModified()));
System.out.println("Expiration date:"+new Date(uc.getExpiration()));
System.out.println("ContentLength:"+uc.getContentLength());
}
catch(MalformedURLException ex)
{
System.err.println(args[i]+"is not aURL I understand");
}
catch(IOException ex)
{
System.err.println(ex);
}
System.out.println();
}
}
}
11
EX NO:5A
HTTP REQUEST
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class sourceviewer3
{
public static void main(String[] args)
{
for(int i=0;i<args.length;i++)
{
try
{
URL u=new URL(args[i]);
HttpURLConnection uc=(HttpURLConnection)
u.openConnection();
int code=uc.getResponseCode();
String response=uc.getResponseMessage();
System.out.println("HTTP/1.x" + code +" " +response);
for(int j=1;;j++)
{
String header=uc.getHeaderField(j);
String key=uc.getHeaderFieldKey(j);
if(header==null || key==null) break;
System.out.println(uc.getHeaderFieldKey(j) + ":" +header);
}
InputStream in=new BufferedInputStream(uc.getInputStream());
Reader r=new InputStreamReader(in);
int c;
while((c=r.read())!=-1)
{
System.out.println((char) c);
}
}
catch(MalformedURLException ex)
{
System.err.println(args[0] + "is not parseable URL");
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
}
12
EX NO:5B
SMTP REQUEST
import java.util.Date;
import java.util.Properties;
import javax.naming.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Assi
{
public static void main(String[] args)
{
try
{
javax.mail.Session mailSession = null;
Context ctx = new InitialContext ( ) ;
Context myEnv = ( Context ) ctx.lookup ( "java:comp/env" ) ;
String smtpHost = ( String ) myEnv.lookup ( "smtpHost" ) ;
Properties props = new Properties ( ) ;
props.put ( "mail.smtp.host", smtpHost ) ;
mailSession = javax.mail.Session.getDefaultInstance ( props, null ) ;
javax.mail.Message msg = new MimeMessage ( mailSession ) ;
msg.setFrom ( new javax.mail.internet.InternetAddress
( "[email protected]" )) ;
InternetAddress [ ] addresses = { new InternetAddress
( "[email protected]" ) } ;
msg.setRecipients ( javax.mail.Message.RecipientType.TO, addresses ) ;
msg.setSubject ( "Subject" ) ;
msg.setSentDate ( new Date ( ) ) ;
Multipart parts = new MimeMultipart ( ) ;
MimeBodyPart mainBody = new MimeBodyPart ( ) ;
mainBody.setText ( "Hello Body" ) ;
parts.addBodyPart ( mainBody ) ;
POP3(RECEIVING MAIL
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
public class pop3client
{
public static void main(String[] args)
{
Properties props=new Properties();
String host="mail.Hindustan.org";
String username="[email protected]";
String password="hcecse";
String provider="pop3";
try
{
Session session=Session.getDefaultInstance(props,null);
Store store=session.getStore(provider);
store.connect(host,username,password);
Folder inbox=store.getFolder("INBOX");
if(inbox==null)
{
System.out.println("NO INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
Message[] messages=inbox.getMessages();
for(int i=0;i<messages.length;i++)
{
System.out.println("----------------------------Message"+(i+1)
+"-------------------------------");
messages[i].writeTo(System.out);
}
inbox.close(false);
store.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
14
EX NO:6
UDP CLIENT
import java.io.*;
import java.net.*;
class UDPClient
{
public static DatagramSocket clientsocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789, sport = 790;
public static void main(String[] a) throws IOException
{
clientsocket = new DatagramSocket(cport);
dp = new DatagramPacket(buf, buf.length);
dis = new BufferedReader(new InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Client is Running... Type 'STOP' to Quit");
while(true)
{
String str = new String(dis.readLine());
buf = str.getBytes();
if(str.equals("STOP"))
{
System.out.println("Terminated...");
clientsocket.send(new DatagramPacket(buf,str.length(), ia,sport));
break;
}
clientsocket.send(new DatagramPacket(buf,str.length(), ia, sport));
clientsocket.receive(dp);
String str2 = new String(dp.getData(), 0,dp.getLength());
System.out.println("Server: " + str2);
}
}
}
15
UDP SERVER
import java.io.*;
import java.net.*;
class UDPServer
{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[] = new byte[1024];
public static int cport = 789,sport=790;
public static void main(String[] a) throws IOException
{
serversocket = new DatagramSocket(sport);
dp = new DatagramPacket(buf,buf.length);
dis = new BufferedReader
(new InputStreamReader(System.in));
ia = InetAddress.getLocalHost();
System.out.println("Server is Running...");
while(true)
{
serversocket.receive(dp);
String str = new String(dp.getData(), 0,
dp.getLength());
if(str.equals("STOP"))
{
System.out.println("Terminated...");
break;
}
System.out.println("Client: " + str);
String str1 = new String(dis.readLine());
buf = str1.getBytes();
serversocket.send(new
DatagramPacket(buf,str1.length(), ia, cport));
}
}
}
16
EX NO:7
POSTPARAM
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParam extends genericServlet
{
public void service(ServletRequest request,ServletResponse response)throws
ServletException,IOException
{
PrintWriter pw=response.getWriter();
Enumeration e=request.getParameterNames();
while(e.hasMoreElements())
{
String pname=(String)e.nextElement();
pw.print(pname + " = ");
String pvalue=request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}
}
POSTPARAM.HTML
<HTML>
<BODY>
<CENTER>
<Form name="postparam" method="post"
action="http://localhost:8080/postparam/postparam">
<TABLE>
<tr>
<td><B>Employee</B></td>
<td><input type="textbox" name="ename" size="25" value=" "></td>
</tr>
<tr>
<td><B>Phone</B></td>
<td><input type="textbox" names="phoneno" size="25" value=""></td>
</tr>
</TABLE>
<INPUT type="submit" value="submit">
</body>
</html>
17
EX NO:8
STUDENT.HTML
<HTML>
<BODY>
<CENTER>
<FORM name = "students" method = "post"
action="http://localhost:8080/Student/Student">
<TABLE>
<tr>
<td><B>Roll No. </B> </td>
<td><input type = "textbox" name="rollno" size="25"
value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</FORM>
<CENTER>
</BODY>
</HTML>
IMAGE MAP.HTML
<HTML>
<HEAD>
<TITLE>Image Map</TITLE>
</HEAD>
<BODY>
<MAP id = "picture">
<AREA href = "TamilNadu.html" shape = "circle"
coords = "170, 490, 30" alt = "Tamil Nadu" />
<AREA href = "Karnataka.html" shape = "rect"
coords = "115, 390, 150, 450" alt = "Karnataka" />
<AREA href = "AndhraPradesh.html" shape = "poly"
coords = "165, 355, 200, 355, 220, 380, 170, 425, 165,
355" alt = "Andhra Pradesh" />
<AREA href = "Kerala.html" shape = "poly"
coords = "115, 455, 160, 470, 140, 485, 150, 505, 150,
530, 135, 500, 115, 455" alt = "Kerala" />
</MAP>
<IMG src = "india.Jpg" alt = "india" usemap = "#picture" />
</BODY>
</HTML>
TAMIL NADU.HTML
<HTML>
<HEAD>
<TITLE>About Tamil Nadu</TITLE>
</HEAD>
<BODY>
<CENTER><H1>Tamil Nadu</H1></CENTER>
<HR>
<UL>
<LI>Area : 1,30,058 Sq. Kms.</LI>
<LI>Capital : Chennai</LI>
<LI>Language : Tamil</LI>
<LI>Population : 6,21,10,839</LI>
</UL>
</BODY>
</HTML>
21
ANDRA PRADESH.HTML
<HTML>
<HEAD>
<TITLE>About Andhra Pradesh</TITLE>
</HEAD>
<bg color="Green">
<BODY>
<CENTER><H1><font color="Red">Andhra Pradesh</font></H1></CENTER>
<HR>
<UL>
<LI>Area : 2,75,068 Sq. Kms</LI>
<LI>Capital : Hyderabad</LI>
<LI>Language : Telugu</LI>
<LI>Population : 7,57,27,541</LI>
</bg>
</UL>
</BODY>
</HTML>
KARNATAKA.HTML
<HTML>
<HEAD>
<TITLE>About Karnataka</TITLE>
</HEAD>
<BODY>
<CENTER><H1>Karnataka</H1></CENTER>
<HR>
<UL>
<LI>Area : 1,91,791 Sq. Kms</LI>
<LI>Capital : Bangalore</LI>
<LI>Language : Kannada</LI>
<LI>Population : 5,27,33,958</LI>
</UL>
</BODY>
</HTML>
22
KERALA.HTML
<HTML>
<HEAD>
<TITLE>About Kerala</TITLE>
</HEAD>
<BODY>
<CENTER><H1>Kerala</H1></CENTER>
<HR>
<UL>
<LI>Area : 38,863 Sq. Kms.</LI>
<LI>Capital : Thiruvananthapuram</LI>
<LI>Language : Malayalam</LI>
<LI>Population : 3,18,38,619</LI>
</UL>
</BODY>
</HTML>
EX NO:10
MAIN.HTML
<html>
<head>
<title>Cascading Style Sheet</title>
</head>
<frameset rows"200,*" frameborder="no" framespacing="0">
RIGHT.HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body><font color="purple">HCE Welcome you to the Department of computer Science
and Engineering....</font></body>
</html>
23
LEFT.HTML
<html>
<head>
<style type="text/css">
body
{
background-color:magenta;
}
a:link
{
text-decoration:none;
color:#435161;
}
a:hover
{
text-decoration:none;
cursor:hand;
font-weight:bold;
}
a:visited
{
text-decoration:none;
color:red;
}
a:active
{
text-decoration:none;
}
</style>
</head>
<body>
<A ref="aboutus.html" target="right">About us</A>
<BR><BR>
<A ref="faculties.html" target="right">faculties</A>
<BR><BR>
<A ref="Facilities.html" target="right">Lab facilities</A>
<BR><BR>
</body>
</html>
24
ABOUT.HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<p class="margin01">This is Hindusthan College of Engineering Chennai</p>
</body>
</html>
FACULITIES.HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<p class="caps16"><font color="red">Faculty Members</font></p>
<table border=1>
<tr><th><font color="Blue">NO.</font></th><th><font
color="Blue">Name</font></th><th><font color="Blue">desgination</font></th></tr>
<td>1</td><td>Prof.M.Robert Masilamani</td><td>rofessor and head</td></tr>
<td>2</td><td>Professor Satayalakhsmi</td><td>Professor</td></tr>
<td>3</td><td>Prof.R.krisnnaveni</td><td>Assitant Professor</td></tr>
<td>4</td><td>Prof.D.John Aravindhan</td><td>Assitant Professor</td></tr>
</table>
</body>
</html>
LABS.HTML
<html>
<head>
<link rel="Stylesheet" type="text/css" href="style.css">
</head>
<body>
<p class="Caps16"><font color="purple">LAB FACILITIES</font></p>
<font color="Green">The department has the follwoing well eqiuoed facilities:</font>
<BR>
<BR>1.Data Structures Lab<BR>
<BR>2.Linux Lab<BR>
<BR>3.C and office Lab<BR>
<BR>4.RDBMS and CASE Lab<BR>
<BR>5.Research Lab<BR>
<BR>6.Internet Progrmming Lab<BR>
</body>
</html>
25
STYLE.CSS
body{
background-color:silver;
}
a:link
{
text-decoration:none;
color:#435161;
}
a:hover{
text-decoration:none;
cursor:hand;
font-weight:bold;
}
a:visited
{
text-decoration:none;
color:blue;
}
a:active
{
text-decoration:none;
}
.Margin01
{
padding:10px;
}
.BodyTextBigNoColor
{
font-family:Verdana,Arial,Andulus,sans-serif,Halvetica;
font-size:13px;
line-height:18px;
}
.Margin02
{
padding:5px 5px 5px 15px;
}
.BodyTextsmlNoColor
{
font-family:Verdana,Arial,Andulus,sans-serif,Halvetica;
font-size:10px;
line-height:18px;
}
.Caps16
{
font-family:Verdana,Arial,Andulus,sans-serif,Halvetica;
font-size:20px;
text-transform:Uppercase;
font-weight:normal; }