Course Outcome 5 (CO5):
1. Program to draw Circle, Rectangle, Line in Applet.
2. Program to find maximum of three numbers using AWT.
3. Find the percentage of marks obtained by a student in 5 subjects. Display a happy
face if he secures above 50% or a sad face if otherwise.
4. Using 2D graphics commands in an Applet, construct a house. On mouse click event,
change the color of the door from blue to red.
5. Implement a simple calculator using AWT components.
6. Develop a program that has a Choice component which contains the names of shapes
such as rectangle, triangle, square and circle. Draw the corresponding shapes for
given parameters as per user’s choice.
-----------------------------------------------------------------------------------------------------------------------------
CO5.1. Program to draw Circle, Rectangle, Line in Applet
//D:\S2 JAVA lab\Java Lab 2025\DrawShapes.java
import java.applet.Applet;
import java.awt.*;
/* <applet code="DrawShapes" width="400" height="400"></applet> */
public class DrawShapes extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.GREEN); // Set color
g.drawLine(50, 200, 300, 200); //draw a line
g.setColor(Color.RED); // Set color
g.drawRect(50, 50, 150, 100); //draw a rectangle
g.setColor(Color.BLUE); // Set color
g.drawOval(250, 50, 100, 100); //draw a circle (Oval with equal width & height)
}
}
CO5.1. Program to draw Circle, Rectangle, Line in Applet
//D:\S2 JAVA lab\Java Lab 2025\DrawShapes1.java
import java.applet.Applet;
import java.awt.*;
public class DrawShapes1 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.GREEN); // Set color
g.drawLine(50, 200, 300, 200); //draw a line
g.setColor(Color.RED);
g.drawRect(50, 50, 150, 100); //draw a rectangle
g.setColor(Color.BLUE);
g.drawOval(250, 50, 100, 100); //draw a circle (Oval with equal width & height)
}
}
//D:\S2 JAVA lab\Java Lab 2025\DrawShapes.html
<html>
<body>
<applet code="DrawShapes1.class" width="400" height="400">
</applet>
</body>
</html>
CO5.2 Program to find maximum of three numbers using AWT.
import java.awt.*;
import java.net.*;
import java.awt.event.*;
class maxAwt extends Frame
{
TextField t1,t2,t3,t4;
Label la1,la2,la3,la4;
Button b1,b2;
maxAwt()
{
setTitle("Maximum of 3 Numbers");
t1 = new TextField(); t1.setBounds(100,45,145,20);
la1= new Label("Enter 1st No:"); la1.setBounds(100,75,145,20);
t2 = new TextField(); t2.setBounds(100,145,145,20);
la2= new Label("Enter 2nd No:"); la2.setBounds(100,110,145,20);
t3 = new TextField(); t3.setBounds(100, 200, 145, 20);
la3= new Label("Enter 3rd NO:"); la3.setBounds(100,170, 145, 20);
t4 = new TextField(); la4.setBounds(100,240,145,20);
la4 = new Label(""); t4.setBounds(100,240,135,20);
b1 = new Button("Max"); b1. setBounds(105,278,70,40);
b2 = new Button("Exit"); b2.setBounds(195,278,70,40);
add(b1); add(b2);
add(la1); add(t1);
add(la2); add(t2);
add(la3); add(t3);
add(la4); add(t4);
setSize(400,400);
setVisible(true);
b1.addActionListener ( new ActionListener ( )
{
public void actionPerformed(ActionEvent e)
{
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int c = Integer.parseInt(t3.getText());
if (a>b && a>c)
{
l4.setText("MAXIMUM =" + String.valueOf(a));
}
else if (b>c)
{
l4.setText("MAXIMUM =" + String.valueOf(b));
}
else
{
l4.setText("MAXIMUM =" + String.valueOf(c));
}
}
} );
b2.addActionListener (new ActionListener ( )
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
} );
}
public static void main(String []args)
{
new maxAwt ( );
}
}
D:\S2 JAVA lab\Java Lab 2025>javac maxAwt.java
D:\S2 JAVA lab\Java Lab 2025>java maxAwt
CO5.3. Find the percentage of marks obtained by a student in 3 subjects.
Display a happy face if he secures above 50% or a sad face if otherwise
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="SMark.class" width="400" height="400"></applet> */
public class SMark extends Applet implements ActionListener
{
TextField t1, t2, t3, t4;
Button b;
Label l1, l2, l3, l4;
double percentage = 0.0;
public void init()
{
l1 = new Label("Mark1:"); t1 = new TextField(5);
l2 = new Label("Mark2:"); t2 = new TextField(5);
l3 = new Label("Mark3:"); t3 = new TextField(5);
l4 = new Label("Percentage:"); t4 = new TextField(5);
t4.setEditable(false);
b = new Button("Calculate");
b.addActionListener(this);
setLayout(null);
l1.setBounds(50, 40, 100, 20); add(l1);
t1.setBounds(150, 40, 100, 20); add(t1);
l2.setBounds(50, 80, 100, 20); add(l2);
t2.setBounds(150, 80, 100, 20); add(t2);
l3.setBounds(50, 120, 100, 20); add(l3);
t3.setBounds(150, 120, 100, 20); add(t3);
l4.setBounds(50, 160, 100, 20); add(l4);
t4.setBounds(150, 160, 100, 20); add(t4);
b.setBounds(100, 200, 80, 30); add(b);
}
public void actionPerformed(ActionEvent e)
{
try
{
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int z = Integer.parseInt(t3.getText());
percentage = ((x + y + z)*100 / 300);
t4.setText(String.format("%.2f", percentage));
repaint(); // Call paint() to update the face
}
catch (NumberFormatException ex)
{
t4.setText("Error");
}
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.YELLOW);
g.fillOval(150, 250, 100, 100); // Face
g.setColor(Color.BLACK);
g.fillOval(170, 275, 10, 10); // Left eye
g.fillOval(210, 275, 10, 10); // Right eye
if (percentage > 50)
{
g.drawArc(175, 300, 50, 20, 180, 180); // Happy face
}
else
{
g.drawArc(175, 310, 50, 20, 0, 180); // Sad face
}
}
}
javac SMark.java
appletviewer SMark.java
CO5.4. Using 2D graphics commands in an Applet, construct a house. On mouse click event,
change the color of the door from blue to red.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="HouseApplet.class" width="400" height="400"></applet> */
public class HouseApplet extends Applet implements MouseListener
{
private Color doorColor = Color.BLUE; // Initial door color
public void init()
{ addMouseListener(this); // Register mouse listener }
public void paint(Graphics g)
{
// Draw the house body
g.setColor(Color.YELLOW);
g.fillRect(100, 150, 200, 150); // House body
// Draw the roof
g.setColor(Color.RED);
int[] xPoints = {100, 200, 300}; // Triangle X coordinates
int[] yPoints = {150, 50, 150}; // Triangle Y coordinates
g.fillPolygon(xPoints, yPoints, 3);
// Draw windows
g.setColor(Color.WHITE);
g.fillRect(120, 170, 40, 40); // Left window
g.fillRect(240, 170, 40, 40); // Right window
// Draw the door
g.setColor(doorColor);
g.fillRect(180, 220, 40, 80); // Door
// Draw the door knob
g.setColor(Color.BLACK);
g.fillOval(215, 260, 5, 5);
}
public void mouseClicked(MouseEvent e) // Change door color on click
{
doorColor = (doorColor == Color.BLUE) ? Color.RED : Color.BLUE;
repaint(); // Redraw the applet
}
// Unused mouse event methods (must be implemented)
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
CO5.5 Implement a simple calculator using AWT (Abstract Window Toolkit) components.
// Import AWT and Event libraries for GUI components and event handling
import java.awt.*;
import java.awt.event.*;
class CalcAwt extends WindowAdapter implements ActionListener
{
Frame f;
Label l1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button badd, bsub, bmult, bdiv, bcalc, bpts;
double xd;
double num1,num2,check;
CalcAwt()
{
f= new Frame("MY CALCULATOR"); // Create a new Frame with a title "MY CALCULATOR"
l1=new Label(); //Create a new label to display results
l1.setBackground(Color.LIGHT_GRAY); //set its background color & position
l1.setBounds(50,50,250,50); //set position & size of the label (x, y,width,height)
b7=new Button("7"); b7.setBounds(50,120,50,50);
b4=new Button("4"); b4.setBounds(50,180,50,50);
b1=new Button("1"); b1.setBounds(50,240,50,50);
b0=new Button("0"); b0.setBounds(50,300,50,50);
b8=new Button("8"); b8.setBounds(120,120,50,50);
b5=new Button("5"); b5.setBounds(120,180,50,50);
b2=new Button("2"); b2.setBounds(120,240,50,50);
bpts=new Button("."); bpts.setBounds(120,300,50,50);
b9=new Button("9"); b9.setBounds(190,120,50,50);
b6=new Button("6"); b6.setBounds(190,180,50,50);
b3=new Button("3"); b3.setBounds(190,240,50,50);
bcalc=new Button("="); bcalc.setBounds(190,300,50,50);
bdiv=new Button("/"); bdiv.setBounds(260,120,50,50);
bmult=new Button("*"); bmult.setBounds(260,180,50,50);
bsub=new Button("-"); bsub.setBounds(260,240,50,50);
badd=new Button("+"); badd.setBounds(260,300,50,50);
//button presses are handled by the actionPerformed method
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bpts.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);
bcalc.addActionListener(this);
//Adds the current instance as the action listener for each button to handle click events
f.addWindowListener(this);
// Adding components to the frame
f.add(l1); f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6); f.add(b7);
f.add(b8); f.add(b9); f.add(b0);
f.add(badd); f.add(bsub); f.add(bmult); f.add(bdiv);
f.add(bcalc); f.add(bpts);
f.setSize(360,400); f.setLayout(null); f.setVisible(true);
}
public void windowClosing(WindowEvent e) // close the frame & the appln
{ f.dispose(); }
public void actionPerformed(ActionEvent e)
{
String z, zt;
if (e.getSource()==b1) //Number Button
{
zt=l1.getText();
z=zt+"1";
l1.setText(z);
}
if(e.getSource()==b2)
{
zt=l1.getText();
z=zt+"2";
l1.setText(z);
}
if (e.getSource()==b3)
{
zt=l1.getText();
z=zt+"3";
l1.setText(z);
}
if (e.getSource()==b4)
{
zt=l1.getText();
z=zt+"4";
l1.setText(z);
}
if (e.getSource()==b5)
{
zt=l1.getText();
z=zt+"5";
l1.setText(z);
}
if (e.getSource()==b6)
{
zt=l1.getText();
z=zt+"6";
l1.setText(z);
}
if (e.getSource()==b7)
{
zt=l1.getText();
z=zt+"7";
l1.setText(z);
}
if (e.getSource()==b8)
{
zt=l1.getText();
z=zt+"8";
l1.setText(z);
}
if (e.getSource()==b9)
{
zt=l1.getText();
z=zt+"9";
l1.setText(z);
}
if (e.getSource()==b0)
{
zt=l1.getText();
z=zt+"0";
l1.setText(z);
}
if (e.getSource()==bpts) //Add decimal points
{
zt=l1.getText();
z=zt+".";
l1.setText(z);
}
if (e.getSource()==badd) //For addition
{
try
{
num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z =" ";
l1.setText(z);
check=1;
}
if(e.getSource()==bsub) //For subtraction
{
try
{ num1=Double.parseDouble(l1.getText()); }
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=2;
}
if(e.getSource()==bmult) //For multiplication
{
try
{ num1=Double.parseDouble(l1.getText()); }
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=3;
}
if(e.getSource()==bdiv) //For division
{
try
{ num1=Double.parseDouble(l1.getText());
}
catch(NumberFormatException f)
{
l1.setText("Invalid Format");
return;
}
z="";
l1.setText(z);
check=4;
}
if(e.getSource()==bcalc) //RESULT BUTTON
{
try
{ num2=Double.parseDouble(l1.getText()); }
catch(Exception f)
{
l1.setText("ENTER NUMBER FIRST");
return;
}
if(check==1)
xd =num1+num2;
if(check==2)
xd =num1-num2;
if(check==3)
xd =num1*num2;
if(check==4)
xd =num1/num2;
if(check==5)
xd =num1%num2;
l1.setText(String.valueOf(xd));
}
}
public static void main(String args[])
{
new CalcAwt();
}
}
E:\S2 JAVA LAB 2024\JavaRec>javac CalcAwt.java
E:\S2 JAVA LAB 2024\JavaRec>java CalcAwt
CO5.6 .Develop a program that has a Choice component which contains the names of shapes
such as rectangle, triangle, square, and circle. Draw the corresponding shapes for given
parameters as per user’s choice.
import java.awt.*;
import java.awt.event.*;
public class ShapeDraw extends Frame implements ItemListener
{
Choice shapeChoice;
Canvas canvas;
public ShapeDraw ( )
{
setTitle("Shape Draw");
setSize(500, 500);
setLayout(new BorderLayout());
// Creating choice component
shapeChoice = new Choice();
shapeChoice.add("Rectangle");
shapeChoice.add("Triangle");
shapeChoice.add("Square");
shapeChoice.add("Circle");
shapeChoice.addItemListener(this);
Add (shapeChoice, BorderLayout.NORTH); // Adding choice component to top
canvas = new Canvas() // Creating canvas for drawing
{
public void paint(Graphics g)
{
drawShape(g, shapeChoice.getSelectedItem ( ) );
}
};
canvas.setBackground(Color.WHITE); add(canvas, BorderLayout.CENTER);
// Closing event
addWindowListener ( new WindowAdapter ( )
{
public void windowClosing(WindowEvent e)
{
dispose();
}
} );
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
canvas.repaint();
}
private void drawShape(Graphics g, String shape)
{
g.setColor(Color.BLUE);
switch (shape)
{ case "Rectangle": g.drawRect(100, 100, 200, 100);
break;
case "Triangle": int[] x = {150, 250, 350};
int[] y = {200, 100, 200};
g.drawPolygon(x, y, 3);
break;
case "Square": g.drawRect(150, 100, 100, 100);
break;
case "Circle": g.drawOval(150, 100, 100, 100);
break;
}
}
public static void main(String[] args)
{
new ShapeDraw();
}
}
D:\S2 JAVA lab\Java Lab 2025>javac ShapeDraw.java
D:\S2 JAVA lab\Java Lab 2025>java ShapeDraw