You are on page 1of 5

Re

gister No: 95508205042


EX. NO : 04
Java Applet color palette

DATE

AIM:
To Write programs in Java to create applets for a color palette, Set
background and foreground of the control text area and set background images.

ALGORITHM:
Create a color palette with matrix of buttons by using java applet.
Set background and foreground of the control text area by selecting a
color from
color palette.

In order to select Foreground or background use check box control as


radio
Buttons.

To set background images for web page.

25

Re
gister No: 95508205042
PROGRAM:
Palette.html
<applet code="palette.java" width=500 height=500>
<param name="sunset" value="Sunset.jpg"width=1000 height=1000>
<param name="winter" value="Winter.jpg"width=1000 height=1000>
</applet>
Palette.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class palette extends Applet implements ActionListener,ItemListener
{
Button btnRed,btnGreen,btnBlue;
String str="";
CheckboxGroup cbgColor;
CheckboxGroup cbgImage;
Checkbox optFore,optBack;
Checkbox optMb,optWsb;
Image imgMb,imgWsb;
TextArea txtaComments=new TextArea("",5,30);
public void init()
{
setLayout(new GridLayout(4,3));
cbgColor=new CheckboxGroup();
cbgImage=new CheckboxGroup();
Label lblColor=new Label("Select the Area:");
Label lblImage=new Label("Select the image:");
optFore=new Checkbox("Foreground",cbgColor,true);
optBack=new Checkbox("Background",cbgColor,false);
optMb=new Checkbox("SUMMER",cbgImage,true);
optWsb=new Checkbox("WINTER",cbgImage,false);
btnRed=new Button("Red");
btnGreen=new Button("Green");
btnBlue=new Button("Blue");
imgMb=getImage(getDocumentBase(),getParameter("sunset"));
imgWsb=getImage(getDocumentBase(),getParameter("winter"));
add(btnRed);
add(btnGreen);
add(btnBlue);
add(lblColor);
add(optFore);
add(optBack);
add(lblImage);
add(optMb);
add(optWsb);
add(txtaComments);
optFore.addItemListener(this);
optBack.addItemListener(this);
optMb.addItemListener(this);
optWsb.addItemListener(this);
26

Re
gister No: 95508205042
btnRed.addActionListener(this);
btnGreen.addActionListener(this);
btnBlue.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
str=cbgColor.getSelectedCheckbox().getLabel();
if(ae.getSource()==btnRed&&str.equals("Background"))
{
txtaComments.setBackground(Color.RED);
}
if(ae.getSource()==btnRed&&str.equals("Foreground"))
{
txtaComments.setForeground(Color.RED);
}
if(ae.getSource()==btnGreen&&str.equals("Background"))
{
txtaComments.setBackground(Color.GREEN);
}
if(ae.getSource()==btnGreen&&str.equals("Foreground"))
{
txtaComments.setForeground(Color.GREEN);
}
if(ae.getSource()==btnBlue&&str.equals("Background"))
{
txtaComments.setBackground(Color.BLUE);
}
if(ae.getSource()==btnBlue&&str.equals("Foreground"))
{
txtaComments.setBackground(Color.BLUE);
}
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
if(optMb.getState()==true)
g.drawImage(imgMb,200,400,this);
if(optWsb.getState()==true)
g.drawImage(imgWsb,200,400,this);
}

OUTPUT:
Palette.html

27

Re
gister No: 95508205042

28

Re
gister No: 95508205042

CONCULSION:
Thus the java program to create applets for a color palette, Set
background and foreground of the control text area and set background images
was created successfully.

OBSERVATION
(10)
RECORD
(5)
TOTAL
(15)
REMARKS :

29

You might also like