Color Palette with matrix of buttons | Background and Foreground in control text area by selecting form color palette & checkbox as radio button

Aim
To write a java code to implement
Color palette with matrix of Buttons
Background and Foreground in control text area by selecting form color palette and selecting from the check box CS1404 INTERNET PROGRAMMING LABORATORY
Algorithm for color palette
Start color palette the program
Declare the color palette class with applet
And implement Action Listener and Item Listener
Then create Check box Group
Create Image object
In init() function declare the necessary layout, checkbox and image
Then in the action performed function to implement their appropriate action
Finally terminate Color palette the program



Source Code Color Palette in java program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ColorApplet" width=500 height=500>
<param name = "recmb" value = "recmb.jpg">
<param name = "recwsb" value = "recwsb.jpg">
</applet>
*/
public class ColorPalette 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("REC-Main Block", cbgImage,true);
optWsb = new Checkbox("REC-Workshop Block",cbgImage, false);
btnRed = new Button("Red");
btnGreen = new Button("Green");
btnBlue = new Button("Blue");
BB / REC - 17
imgMb = getImage(getDocumentBase(),
getParameter("recmb"));
imgWsb = getImage(getDocumentBase(),
getParameter("recwsb"));
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);
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);
}
BB / REC - 18
if(ae.getSource() == btnBlue &&
str.equals("Foreground")) {
txtaComments.setForeground(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:- CS1404 INTERNET PROGRAMMING LABORATORY
Complie color palette program
C:\IPLAB>javac ColorPalette.java
Run the color palette program
C:\IPLAB>appletviewer ColorPalette.java

Color Palette with matrix of buttons  Background and Foreground in control text area by selecting form color palette & checkbox as radio button

Related post