© 2012 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies
« NS2  Projects »

Java Program To Create Color Palette With Matrix Of Buttons

Algorithm Steps:

Step 1: Write a html applet tag with code set class name and comment the tag

Step 2: Import all necessary packages and classes

Step 3: Define a class that extends applet and implements action listener and item listener

Step 4: Declare an array of buttons to set colors, two checkboxes for foreground and background colors

Step 5: Declare a text area to hold the text, a checkbox group for checkboxes

Step 6: Declare three panels, buttonpanel, palettepanel and checkpanel

Step 7: Declare a string, color.

Step 8: In the init() method, do the following:

            i) Create the buttonpanel and set the layout to gridlayout of 3 X 3

           ii) Create an array of 9 buttons for various colors

           iii) Add action listener to each button and add all of them to button panel

           iv) Create the checkbpanel and set its layout to flow layout

           v) Create a checkbox group. Then create forground and background checkboxes

          vi) Add item listener to each checkbox and add them to the checkpanel

          vii) Create a text area and change its font to desired one.

          viii) Create the palettepanel and set is layout to border layout

          ix) Add the text area, buttonpanel, and checkpanel to various zones of the border layout

          x) Add the palettepanel to the applet

Step 9: Write an empty itemStateChanged() method

Step 10: In the actionPerformed() method, do the following:

          i) Get the action command in the string, color

         ii) If foreground is checked then set the foreground color to the selected color

         iii) If background is checked then set the background color to the selected color

Java Program To Create Color Palette With Matrix Of Buttons

/* */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class palette extends Applet implements ActionListener,ItemListener { Button[] colors; Checkbox foreground,background; TextArea workarea; CheckboxGroup cbg; Panel buttonpanel,checkpanel,palettepanel; String colour; public void init() { buttonpanel=new Panel(); buttonpanel.setLayout(new GridLayout(3,3)); colors=new Button[9]; colors[0]=new Button("RED"); colors[1]=new Button("GREEN"); colors[2]=new Button("BLUE"); colors[3]=new Button("CYAN"); colors[4]=new Button("ORANGE"); colors[5]=new Button("WHITE"); colors[6]=new Button("BLACK"); colors[7]=new Button("YELLOW"); colors[8]=new Button("PINK"); for(int i=0;i<9;i++) { colors[i].addActionListener(this); buttonpanel.add(colors[i]); } checkpanel=new Panel(); checkpanel.setLayout(new FlowLayout()); cbg=new CheckboxGroup(); foreground=new Checkbox("ForeGround",cbg,true); background=new Checkbox("BackGround",cbg,false); foreground.addItemListener(this); background.addItemListener(this); checkpanel.add(foreground); checkpanel.add(background); workarea=new TextArea(8,40); workarea.setFont(new Font("Garamond",Font.BOLD,20)); palettepanel=new Panel(); palettepanel.setLayout(new BorderLayout()); palettepanel.add(workarea,BorderLayout.CENTER); palettepanel.add(checkpanel,BorderLayout.EAST); palettepanel.add(buttonpanel,BorderLayout.SOUTH); add(palettepanel); } public void itemStateChanged(ItemEvent ie) { } public void actionPerformed(ActionEvent ae) { colour=ae.getActionCommand(); if(foreground.getState()==true) workarea.setForeground(getColour()); if(background.getState()==true) workarea.setBackground(getColour()); } public Color getColour() { Color mycolor=null; if(colour.equals("RED")) mycolor=Color.red; if(colour.equals("GREEN")) mycolor=Color.green; if(colour.equals("BLUE")) mycolor=Color.blue; if(colour.equals("CYAN")) mycolor=Color.cyan; if(colour.equals("ORANGE")) mycolor=Color.orange; if(colour.equals("WHITE")) mycolor=Color.white; if(colour.equals("BLACK")) mycolor=Color.black; if(colour.equals("YELLOW")) mycolor=Color.yellow; if(colour.equals("PINK")) mycolor=Color.pink; return mycolor; } }



SLogix Student Projects

⇓ Student Projects ⇓
⇑ Student Projects ⇑