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);
}
}
);
}
}

Post a Comment

0 Comments