Thursday, November 11, 2010

Create an application that shows the usage of SliderBar to imlpement the RGBcolored label

/* Program : 7
   Create an application that shows the usage of SliderBar to imlpement the RGBcolored label
   Developed by : Malhar Vora
   Developed on : 9-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : www.malhar2010.blogspot.com
*******************************************************************************************************************/
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;

public class P7 extends JFrame implements ChangeListener
{

 JSlider sliderRed=null;
 JSlider sliderGreen=null;
 JSlider sliderBlue=null;
 JLabel lblRed=null;
 JLabel lblGreen=null;
 JLabel lblBlue=null;
 JLabel lblColor=null;
 Color colorFrame=null;
 
 P7()
 {
  
 
  sliderRed = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
  sliderGreen = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
  sliderBlue = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
  
  lblRed = new JLabel("Red");
  lblGreen = new JLabel("Green");
  lblBlue = new JLabel("Blue");
  lblColor = new JLabel("My color changes");
  
  //Setting properties of JFrame
  setTitle("Program 7 - Developed by Malhar Vora");
  setSize(400,500);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true); 
  setResizable(false);
  this.getContentPane().setLayout(new GridLayout(7,1));
  
  
  //Adding controls to JFrame
  this.getContentPane().add(lblColor);
  this.getContentPane().add(lblRed);
  this.getContentPane().add(sliderRed);
  this.getContentPane().add(lblGreen);
  this.getContentPane().add(sliderGreen);
  this.getContentPane().add(lblBlue);
  this.getContentPane().add(sliderBlue);
  
  
  //Setting properties of JSliders
  sliderRed.setMajorTickSpacing(25); //For sliderRed
  sliderRed.setPaintLabels(true);
  sliderRed.setPaintTicks(true);
  sliderRed.setPaintTrack(true);
  sliderGreen.setMajorTickSpacing(25);  //For sliderGreen
  sliderGreen.setPaintLabels(true);
  sliderGreen.setPaintTicks(true);
  sliderGreen.setPaintTrack(true);
  sliderBlue.setMajorTickSpacing(25);  //For sliderBlue
  sliderBlue.setPaintLabels(true);
  sliderBlue.setPaintTicks(true);
  sliderBlue.setPaintTrack(true);
  
  sliderRed.addChangeListener(this);
  sliderGreen.addChangeListener(this);
  sliderBlue.addChangeListener(this);
   
 }
 
 public void stateChanged(ChangeEvent ce)
 {
   colorFrame = new Color(sliderRed.getValue(),sliderGreen.getValue(),sliderBlue.getValue());   
   lblColor.setForeground(colorFrame); 
 }
 
 public static void main(String []str)
 {
  P7 p =new P7();
 }

}
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.