JavaRec CO5 2025
JavaRec CO5 2025
<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);
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;
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.*;
// Draw windows
g.setColor(Color.WHITE);
g.fillRect(120, 170, 40, 40); // Left window
g.fillRect(240, 170, 40, 40); // Right window
// 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)
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