You are on page 1of 10

AJAVA (3360701) 1

Experiment:- 1A
Aim:- Develop an applet that draws a circle. The dimension of the applet should be 500 x 300 pixels.
The circle should be centered in the applet and have a radius of 100 pixels. Display your name centered
in a circle. (using drawOval( )method)
Code:
import java.applet.*;
import java.awt.*
public class e1 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillOval(100,100,200,200);// Main Oval

g.setColor(Color.black);
g.drawOval(99,99,202,202);// outer Oval

g.drawOval(140,140,50,30);// Eye1
g.drawOval(220,140,50,30);// Eye2

g.fillOval(155,140,20,31);
g.fillOval(235,140,20,31);

g.drawLine(200,190,180,220);//Nose Line1
g.drawLine(200,190,220,220);//Nose Line2
g.drawLine(180,220,220,220);//Nose Line3

g.drawArc(150, 220, 100, 60, 0, -180); // smile

g.drawLine(156,244,142,259);// Smile line1


g.drawLine(244,246,258,261);// Smile line2
}
}
/* <applet code="e1.class" width="400" height="400"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 2

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 3

Experiment:- 2
Aim:- Write an Applet to receive the value of the parameter message from the html file and display it
on the webpage.
Code:
import java.applet.*;
import java.awt.*;
public class e2 extends Applet
{
public void paint(Graphics g)
{
String name = getParameter("name");
g.drawString(name,85,100);
}
}
/* <applet code="e2.class" width="200" height="200">
<param name="name" value="Vishv">
</applet>*/
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 4

Experiment:- 3
Aim:- Develop an applet to display the message "Happy New Year" within a text field.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class e3 extends Applet
{
TextField t1;
public void init()
{
setLayout(new FlowLayout());
t1 = new TextField(30);
t1.setText("Happy New Year");
t1.setEditable(false);
add(t1);
}
}
/* <applet code="e3.class" width="300" height="300"></applet>*/
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 5

Experiment:- 4
Aim:- Create four checkboxes with the captions "GROCERIES","COSMETICS","PLASTICS" and
"STATIONARY". Create an instance of TextArea control. As and when any of the four check boxes is
clicked, report the event in TextArea.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class e4 extends Applet implements ItemListener {
JCheckBox c1,c2,c3,c4;
JTextArea t1;
public e4()
{
setLayout(new FlowLayout());

c1 = new JCheckBox("GROCERIES");
c2 = new JCheckBox("COSMETICS");
c3 = new JCheckBox("PLASTICS");
c4 = new JCheckBox("STATIONARY");
t1 = new JTextArea("Your Item List is : ");

c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);

add(c1);
add(c2);
add(c3);
add(c4);
add(t1);

}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource() == c1)
{
t1.append("1.GROCERIES ");
}
if(e.getSource() == c2)
{
t1.append("2.COSMETICS ");
}
if(e.getSource() == c3)
{
t1.append("3.PLASTICS ");
}
if(e.getSource() == c4)
{
t1.append("4.STATIONARY ");

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 6

}
}
public static void main(String arg[])
{
}
}
/* <applet code="e4.class" width="400" height="300"></applet>*/
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 7

Experiment:- 5
Aim:- Create user interface using swing which accepts two numbers and calculates addition, subtraction
and division .
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class e5 extends Applet implements ActionListener
{
JButton add,sub,mul,div;
JTextField n1,n2,ans;
public e5()
{
setLayout(new FlowLayout());
add = new JButton("+");
sub = new JButton("-");
mul = new JButton("*");
div = new JButton("/");

n1 = new JTextField("Enter Number 1",10);


n2 = new JTextField("Enter Number 2",10);
ans = new JTextField("Your Answer",10);

add(n1);
add(n2);

add(add);
add(sub);
add(mul);
add(div);

add(ans);

add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
Integer a,b,x;
a = Integer.parseInt(n1.getText());
b = Integer.parseInt(n2.getText());
if(s == "+")
{
x = a + b;
ans.setText(x.toString());
}
if(s == "-")
{

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 8

x = a - b;
ans.setText(x.toString());
}
if(s == "*")
{
x = a * b;
ans.setText(x.toString());
}
if(s == "/")
{
if(b == 0)
{
ans.setText("Infinite");
}
else
{
x = a / b;
ans.setText(x.toString());
}
}
}
public static void main(String arg[])
{
}
}
/* <applet code="e5.class" width="250" height="300"></
applet>*/
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 9

Experiment:- 6
Aim:- Write servlet application which display context and init parameter from web.xml file
index.html
<html>
<body>
<form action="#" method="get">
<input type="submit" value="init" formaction="init">
<input type="submit" value="Context" formaction="context">
</form>
</body>
</html>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>context</servlet-class>
</servlet>
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>init</servlet-class>
<init-param>
<param-name>init</param-name>
<param-value>init Parameter</param-value>
</init-param>
</servlet>
<context-param>
<param-name>context</param-name>
<param-value>Context Parameter</param-value>
</context-param>
<servlet-mapping>
<servlet-name>context</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>init</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>
</web-app>
init.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class init extends HttpServlet{
String c;
public void init(){

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 10

ServletConfig conf=getServletConfig();
c=conf.getInitParameter("init");
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("Your init Parameter: "+c);
}
}
context.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class context extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
ServletContext context=getServletContext();
String c=context.getInitParameter("context");
pw.println("Your Context Parameter: "+c);
pw.close();
}
}
Output

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045

You might also like