0% found this document useful (0 votes)
16 views

Lab 5

This Java code creates a color mixer applet with the following functionality: 1. It displays a panel of 50 randomly colored buttons, the first 10 set the foreground color and the next 10 set the background color when clicked. 2. It includes a text area to display the selected colors, changing based on which button is clicked. 3. It allows switching between selecting the foreground and background color panels by clicking buttons for each.

Uploaded by

Kishan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab 5

This Java code creates a color mixer applet with the following functionality: 1. It displays a panel of 50 randomly colored buttons, the first 10 set the foreground color and the next 10 set the background color when clicked. 2. It includes a text area to display the selected colors, changing based on which button is clicked. 3. It allows switching between selecting the foreground and background color panels by clicking buttons for each.

Uploaded by

Kishan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab5: Write a Java applet/AWT to display the Application Program screen i.e. calculator and other.

JAVA Code:

import java.awt.*; add(dot);


import java.awt.event.*; add(button0);
import java.applet.*; add(eql);
public class calculator extends Applet add(add);
implements ActionListener {
TextField t1; t1.setBounds(30, 30, 200, 40);
Button button1, button2, button3, button4, button7.setBounds(30, 80, 44, 44);
button5, button6, button7, button8, button9, button8.setBounds(82, 80, 44, 44);
button0; button9.setBounds(134, 80, 44, 44);
Button add, sub, mul, div, eql, dot; button4.setBounds(30, 132, 44, 44);
String msg = "", tmp; button5.setBounds(82, 132, 44, 44);
int a, b; button6.setBounds(134, 132, 44, 44);
public void init() { button1.setBounds(30, 184, 44, 44);
setLayout(null); button2.setBounds(82, 184, 44, 44);
button3.setBounds(134, 184, 44, 44);
t1 = new TextField(20); dot.setBounds(30, 236, 44, 44);
button0.setBounds(82, 236, 44, 44);
button1 = new Button("1"); eql.setBounds(134, 236, 44, 44);
button2 = new Button("2"); add.setBounds(186, 236, 44, 44);
button3 = new Button("3"); sub.setBounds(186, 184, 44, 44);
button4 = new Button("4"); mul.setBounds(186, 132, 44, 44);
button5 = new Button("5"); div.setBounds(186, 80, 44, 44);
button6 = new Button("6");
button7 = new Button("7"); button0.addActionListener(this);
button8 = new Button("8"); button1.addActionListener(this);
button9 = new Button("9"); button2.addActionListener(this);
button0 = new Button("0"); button3.addActionListener(this);
button4.addActionListener(this);
add = new Button("+"); button5.addActionListener(this);
sub = new Button("-"); button6.addActionListener(this);
div = new Button("/"); button7.addActionListener(this);
mul = new Button("*"); button8.addActionListener(this);
dot = new Button("."); button9.addActionListener(this);
eql = new Button("=");
div.addActionListener(this);
add(t1); mul.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
add(button7); eql.addActionListener(this);
add(button8); }
add(button9); public void actionPerformed(ActionEvent ae)
add(div); {
String str = ae.getActionCommand();
add(button4); if (str.equals("+") || str.equals("-") ||
add(button5); str.equals("*") || str.equals("/")) {
add(button6); String str1 = t1.getText();
add(mul); tmp = str;
a = Integer.parseInt(str1);
add(button1); msg = "";
add(button2); } else if (str.equals("=")) {
add(button3); String str2 = t1.getText();
add(sub); b = Integer.parseInt(str2);
t1.setText("" + str1);
msg = "";
int sum = 0 } else {
msg += str;
if (tmp == "+") t1.setText("" + msg);
sum = a + b; }
else if (tmp == "-") }
sum = a - b; public void paint(Graphics g) {
else if (tmp == "*") g.setColor(Color.cyan);
sum = a * b; g.fillRect(20, 20, 220, 270);
else if (tmp == "/") }
sum = a / b; }
String str1 = String.valueOf(sum);
Output :
Lab5: Write a Java applet to display the Application Program screen i.e. Color mixer pallet .

import java.io.*; barr[i]=new Button();


import java.applet.*; p3.add(barr[i]);
import java.awt.*; barr[i].addActionListener(this);
import java.awt.event.*; }
import java.util.*; p1.add(p2,”panel2″);
p1.add(p3,”panel3″);
public class cpal1 extends Applet implements add(p1);
ActionListener { for(int i=0;i<20;i++)
Button barr[] = new Button[50]; {
Random r = new Random(); val1=r.nextInt(255);
Button fg, bg; val2=r.nextInt(255);
TextArea ta; val3=r.nextInt(255);
int val1, val2, val3; Color c=new Color(val1,val2,val3);
Panel p1, p2, p3; barr[i].setBackground(c);
CardLayout cr; }
}
public void init()
{ public void actionPerformed(ActionEvent ae)
cr=new CardLayout(); {
fg=new Button(“foreground”); for(int i=0;i<10;i++)
bg=new Button(“Background”); {
ta=new TextArea(5,10); if(ae.getSource()==barr[i])
add(ta); {
add(fg); Color c=barr[i].getBackground();
add(bg); ta.setBackground(c);
fg.addActionListener(this); }
bg.addActionListener(this); }
p1=new Panel(); for(int i=10;i<20;i++)
p2=new Panel(); {
p3=new Panel(); if(ae.getSource()==barr[i])
p2.setLayout(new GridLayout(5,5)); {
p3.setLayout(new GridLayout(5,5)); Color c=barr[i].getBackground();
p1.setLayout(cr); ta.setForeground(c);
for(int i=0;i<10;i++) }
{ }
barr[i]=new Button(); if(ae.getSource()==bg)
p2.add(barr[i]); cr.show(p1,”panel2″);
barr[i].addActionListener(this); if(ae.getSource()==fg)
} cr.show(p1,”panel3″);
for(int i=10;i<20;i++) }
}

You might also like