You are on page 1of 16

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 p1a extends Applet
{
public void paint(Graphics g)
{
g.drawOval(150,50,200,200);
g.drawString("Vishv",235,155);
}
}
/* <applet code="p1a.class" width="500" height="300"></applet>*/

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 2

Experiment:- 1B
Aim:- Draw ten red circles in a vertical column in the center of the applet.
Code:
import java.applet.*;
import java.awt.*;
public class p1b extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
for(int i=0;i<10;i++)
{
g.fillOval(135,i*30,30,30);
}

}
}
/* <applet code="p1b.class" width="300" height="300"></applet>*/

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 3

Experiment:- 2A
Aim:- Built an applet that displays a horizontal rectangle in its center. Let the rectangle fill with color
from left to right.
Code:
import java.applet.*;
import java.awt.*;
public class p2a extends Applet
{
Thread t =new Thread();
public void run(){

}
public void paint(Graphics g)
{
g.drawRect(19,89,161,21);
g.setColor(Color.green);
for(int i=20;i<180;)
{
try{
g.fillRect(i,90,5,20);
i+=5;
t.sleep(100);
}
catch(Exception e)
{
g.drawString("Error",10,10);
}

}
}
/* <applet code="p2a.class" width="200" height="200"></applet>*/

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 4

Experiment:- 2B
Aim:- Develop an applet that display the position of the mouse at the upper left corner of the applet
when it is dragged or moved. Draw a 10x10 pixel rectangle filed with black at the current mouse position.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class p2b extends Applet implements MouseMotionListener
{
String msg="";
int x,y;
public void init()
{
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
msg="X="+x+"Y="+y;
repaint();
}
public void mouseMoved(MouseEvent e)
{
x=e.getX();
y=e.getY();
msg="X="+x+"Y="+y;
repaint();
}
public void paint(Graphics g)
{
showStatus(msg);
g.fillRect(x,y,10,10);
g.drawString("("+x+","+y+")",0,10);
}
}
/* <applet code="p2b.class" width="300" height="300"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 5

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 6

Experiment:- 3A
Aim:- Develop an applet that contains one button. Initialize the label on the button to “start”, when the
user presses the button, which changes the label between these two values each time the button is pressed.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class p3a extends Applet implements ActionListener
{
Button btn;
public void init()
{
btn=new Button("Start");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent ae)
{
if(btn.getLabel()=="Start")
{
btn.setLabel("Stop");
}
else
{
btn.setLabel("Start");
}
}
}
/* <applet code="p3a.class" width="250" height="250"></applet>*/

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 7

Experiment:- 3B
Aim:- Develop an applet that uses the mouse listener, which overrides only two methods which are
mousePressed and mouseReleased.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class p3b extends Applet implements MouseListener{
String msg="";
public void init()
{
addMouseListener(this);
}
public void paint(Graphics s)
{
showStatus(msg);
s.drawString(msg,25,25);
}
public void mouseClicked(MouseEvent e)
{
msg="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent e)
{
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent e)
{
msg="Mouse Released";
repaint();
}
public void mouseEntered(MouseEvent e)
{
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
msg="Mouse Exited";
repaint();
}
}
/* <applet code="p3b.class" width="200" height="200"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 8

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 9

Experiment:- 4
Aim:- Develop a program that has only one button in the frame, clicking on the button cycles through
the colors: red->green->blue and so on. One color changes per click.(use getBackGround() method to
get the current color).
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class p4a extends Applet implements ActionListener
{
Button btn;
public void init()
{
btn=new Button("Click");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent c)
{
Color bg = getBackground();
if(bg.equals(Color.WHITE))
{
setBackground(Color.RED);
}
else if(bg.equals(Color.RED))
{
setBackground(Color.GREEN);
}
else if(bg.equals(Color.GREEN))
{
setBackground(Color.BLUE);
}
else if(bg.equals(Color.BLUE))
{
setBackground(Color.WHITE);
}
}

}
/* <applet code="p4a.class" width="300" height="300"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 10

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 11

Experiment:- 5A
Aim:- Develop an program that contains three check boxes and 30 x 30 pixel canvas.The three
checkboxes should be labeled “Red”, “Green”,”Blue”. The selection of the check boxes determine the
color of the canvas. For example, if the user selects both “Red” and “Blue”, the canvas should be purple.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class p5a extends Applet implements ItemListener
{
JCheckBox cb1, cb2, cb3;
Canvas c;
int r=0,g=0,b=0;
public p5a()
{
c=new Canvas();
c.setSize(30,30);
c.setBackground(Color.black);
add(c);

cb1=new JCheckBox("Red");
cb2=new JCheckBox("Green");
cb3=new JCheckBox("Blue");

cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);

add(cb1);
add(cb2);
add(cb3);
}
public void itemStateChanged(ItemEvent ie)
{
if(cb1.isSelected())
{
r=255;
canvasColor();
}
else if(!cb1.isSelected()){
r=0;
canvasColor();
}
if(cb2.isSelected())
{
g=255;
canvasColor();
}
else if(!cb2.isSelected()){
g=0;

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 12

canvasColor();
}
if(cb3.isSelected())
{
b=255;
canvasColor();
}
else if(!cb3.isSelected()){
b=0;
canvasColor();
}
}
public void canvasColor()
{
c.setBackground(new Color(r,g,b));
}
public static void main(String arg[])
{
}
}
/* <applet code="p5a.class" width="300" height="100"></applet>*/

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 13

Experiment:- 5B
Aim:- Create an application that displays a frame with a menu bar. When a user selects any menu or
menu item, display that selection on a text area in the center of the frame
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class p5b extends Applet implements ActionListener
{
static JMenuBar mb;
static JMenu menu;
static JMenuItem m1,m2,m3;
static JTextArea txt;
public void init()
{
mb=new JMenuBar();
add(mb);
menu=new JMenu("File");
mb.add(menu);
txt=new JTextArea(null,1,5);
add(txt);

m1 = new JMenuItem("Save");
m2 = new JMenuItem("Open");
m3 = new JMenuItem("Save AS");

menu.add(m1);
menu.add(m2);
menu.add(m3);

m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== m1)
{
txt.setText("Save");
}
else if(e.getSource()== m2)
{
txt.setText("Open");
}
else if(e.getSource()== m3)
{
txt.setText("saveAS");
}
}
}
/* <applet code="p5b.class" width="200" height="200"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 14

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 15

Experiment:- 6
Aim:- Develop a program that draws two sets of ever-decreasing rectangles one in outline form and one
filled alternately in black and white.
Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class p6a extends Applet
{
int x, y, h, w;
public void init()
{
super.init();
x = 10;
y = 100;
h = 300;
w = 200;
}
public void paint(Graphics g)
{
super.paint(g);
for (int i = 0; i < 20; i++)
{
x = x + 5;
y = y + 5;
h = h - 10;
w = w - 10;
if (i % 2 == 0)
{
g.setColor(Color.black);
g.fillRect(x, y, h, w);
}
else
{
g.setColor(Color.white);
g.fillRect(x, y, h, w);
}
}
}
}
/* <applet code="p6a.class" width="400" height="400"></applet>*/

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 16

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045

You might also like