Monday, December 19, 2011

How to Create Color Palette In Java Coding IT1403

To create a color palette in Java.
Algorithm -IT1403 Software Components LAB
Step 1: Import necessary packages.
Step 2: Write applet code which should specify width and height of applet window.
Step 3: Create a class ColorPalette extend JFrame
Step 4: Create a button and named as Change Color.
Step 5: Create a constructor
Step 6: In that constructor, define the logic for event in actionPerformed method.
Step 7: In main function invoke the constructor.
Step 8: Compile and run the program.

Source code Programming Algorithm
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ColorPalatte extends JFrame
{
private JButton chgCor;
private Color color=Color.lightGray;
private Container c;
public ColorPalatte()
{
super("Using JColorChosser");
c=getContentPane();
c.setLayout(new FlowLayout());
chgCor=new JButton("Change Color");
chgCor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
color=JColorChooser.showDialog(ColorPalatte.this,"Choose a color",color);
if(color==null)
color=color.lightGray;
c.setBackground(color);
c.repaint();
}
}
);
c.add(chgCor);
setSize(400,130);
show();
}
public static void main(String arg[])
{
ColorPalatte app=new ColorPalatte();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
read more "How to Create Color Palette In Java Coding IT1403"

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.*;
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 LABORATORYComplie 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
read more "Color Palette with matrix of buttons | Background and Foreground in control text area by selecting form color palette & checkbox as radio button"

Flag counter

free counters