Showing posts with label COLOR PALETTE USING JAVA. Show all posts
Showing posts with label COLOR PALETTE USING JAVA. Show all posts

Tuesday, January 29, 2013

COLOR PALETTE USING JAVA

                              COLOR PALETTE USING JAVA

AIM:
To write the java program to create applets incorporating the following features:

ALGORITHM:

(1)  Step 1: Import all necessary packages and classes.
(2)  Step 2: Define a class CPalette that extends Applet and implements the interfaces ActionListener and ItemListener.
(3)  Step 3: Define the objects for the controls such as Button, CheckboxGroup, Checkbox and TextArea.
(4)  Step 4: Add all the controls by using the init() method of Applet class.
(5)  Step 5: Arrange all the controls by using the method setBounds().
(6)  Step 6: Set the background color for each buttons by using the method setBackground().
(7)  Step 7: Make the controls to listen the action by using the method addActionListener().
(8)  Step 8: Make the frame visible by using the method setVisible().
(9)  Step 9:If the item state value s=1, change the background of the control TextArea.
(10 Step 10:If the item state value s=2, change the foreground of the control TextArea.
(11 Step 11:Create the <applet> code and save it as “ColorPalette.html”
Step 12: Run the HTML file (ColorPalette.html) in Internet Explorer.

PROGRAM:
CPalette.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CPalette extends Applet implements ActionListener,ItemListener
{
 Button b1,b2,b3,b4;
 CheckboxGroup cg;
 Checkbox c1,c2;
 TextArea ta;
 int s;
 public void init()
 {
  setLayout(null);
  b1=new Button();
  add(b1);
  b1.setBounds(50,50,50,25);
  b1.addActionListener(this);
  b1.setBackground(Color.blue);
  b2=new Button();
  add(b2);
  b2.setBounds(120,50,50,25);
  b2.addActionListener(this);
  b2.setBackground(Color.black);
  b3=new Button();
  add(b3);
  b3.setBounds(50,95,50,25);
  b3.addActionListener(this);
  b3.setBackground(Color.red);
  b4=new Button();
  add(b4);
  b4.setBounds(120,95,50,25);
  b4.addActionListener(this);
  b4.setBackground(Color.cyan);
  cg=new CheckboxGroup();
  c1=new Checkbox("Background",cg,false);
  add(c1);
  c1.setBounds(50,200,100,25);
  c1.addItemListener(this);
  c2=new Checkbox("Foreground",cg,false);
  add(c2);
  c2.setBounds(50,250,100,25);
  c2.addItemListener(this);
  ta=new TextArea();
  add(ta);
  ta.setBounds(150,150,120,120);
  setVisible(true);
 }
 public void itemStateChanged(ItemEvent ie)
 {
  if(c1.getState()==true)
  s=1;
  if(c2.getState()==true)
  s=2;
 }
 public void actionPerformed(ActionEvent ae)
 {
  if(s==1)
  {
   if(ae.getSource()==b1)
   ta.setBackground(Color.blue);
   else if(ae.getSource()==b2)
   ta.setBackground(Color.black);
   else if(ae.getSource()==b3)
   ta.setBackground(Color.red);
   else
   ta.setBackground(Color.cyan);
  }
  if(s==2)
  {
   if(ae.getSource()==b1)
   ta.setForeground(Color.blue);
   else if(ae.getSource()==b2)
   ta.setForeground(Color.black);
   else if(ae.getSource()==b3)
   ta.setForeground(Color.red);
   else
   ta.setForeground(Color.cyan);
  }
 }
}

ColorPalette.html
<html>
<head>
<title>Color Palette Demonstration</title>
</head>
<body bgcolor="lightblue">
<applet code="CPalette.class" width="300" height="400">
</applet>
</body>
</html>

RESULT:
Thus the java program to create applets is written and the output is verified.