AIM:
To write a Applet program to create
a color palette and create radio button for selecting the foreground and background colors.
ALGORITHM:
Step 1: Create
a color palette with a matrix of Buttons.
Step 2: Create
a TextArea control.
Step 3: Create
two Radiobutton for selecting the foreground and background colors.
Step 4: Set
the foreground of the TextArea with color specified by the Button which is
clicked only if the
radiobutton selected is
foreground
Step 5: Otherwise
set the background color of the TexeArea with color clicked.
PROGRAM:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MyApplet extends Applet
implements ActionListener
{
Panel
p1,p2;
Checkbox c1,c2;
CheckboxGroup cbg;
TextArea ta;
Button
b1,b2,b3;
public
void init()
{
setLayout(new BorderLayout());
p1=new
Panel(new FlowLayout());
p2=new
Panel(new GridLayout(2,2));
cbg=new CheckboxGroup();
c1=new
Checkbox("FOREGROUND",cbg,true);
c2=new
Checkbox("BACKGROUND",cbg,false);
p1.add(c1);
p1.add(c2);
add(p1,BorderLayout.NORTH);
b1=new
Button("RED");
b2=new
Button("BLUE");
b3=new
Button("GREEN");
p2.add(b1);
p2.add(b2);
p2.add(b3);
add(p2,BorderLayout.CENTER);
ta=new
TextArea("DEMO FOR COLOR PALETTE",15,20);
p2.add(ta);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public
void actionPerformed(ActionEvent ae)
{
String
str=ae.getActionCommand();
String
str1=cbg.getSelectedCheckbox().getLabel();
if(str1.equals("FOREGROUND"))
{
if(str=="RED")
ta.setForeground(Color.red);
if(str=="BLUE")
ta.setForeground(Color.blue);
if(str=="GREEN")
ta.setForeground(Color.green);
}
else
{
if(str=="RED")
ta.setBackground(Color.red);
if(str=="BLUE")
ta.setBackground(Color.blue);
if(str=="GREEN")
ta.setBackground(Color.green);
}
}
}
Comment your feedback :)