Selecting Color


The ColorChoose.java file allows end users to select the color of the text in the document to be printed. The user interface of the ColorChoose.java file appears when an end user clicks the Color menu item from the Color menu of the Printing application.

Listing 4-3 shows the ColorChoose.java file:

Listing 4-3: The ColorChoose.java File

start example
 /* Import javax.swing package classes */ import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JSlider; import javax.swing.JDialog; import javax.swing.BorderFactory; import java.awt.Dimension; /* Import java.awt package classes */ import java.awt.GridLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Color; /* Import javax.swing.event package classes */ import javax.swing.event.ChangeListener; import javax.swing.event.ChangeEvent; /* class ColorChoose - This class creates a Color dialog box that enables the  end user to change the color of the file text.       Fields:        panel - Contains all the components of the Color dialog       redLabel - Contains the content of Red label       greenLabel - Contains the content of Green label       blueLabel - Contains the content of Blue label       previewLabel - Contains the content of Preview label       redSlider - Enables the end user to select the red value       greenSlider - Enables the end user to select the green value       blueSlider - Enables the end user to select the blue value       labelText - Contains the content of preview text          ok - Creates an OK button          cancel - Creates a cancel button          r = 0 - Stores the Red value          g = 0 -   Stores the Green value             b = 0 - Stores the Blue value             Methods:       getOK() - This method returns the OK button object       getCancel() - This method returns the Cancel button object       stateChanged() - This method is invoked when an end user slide slider       color() - This method returns the color */ public class ColorChoose extends JDialog implements ChangeListener {    /* Declare the object of JPanel class */    JPanel panel;    /* Declare the objects of JLabel class */    JLabel redLabel;    JLabel greenLabel;    JLabel blueLabel;    JLabel previewLabel;    /* Declare the objects of JSlider class */    JSlider redSlider;    JSlider greenSlider;    JSlider blueSlider;    /* Declare the object of JTextField class */    JTextField labelText;    /* Declare the objects of JButton class */    JButton ok;    JButton cancel;    /* Declare the integer for storing the RGB values */    int r = 0;    int g = 0;    int b = 0;    /* Default constructor */    public ColorChoose()    {       /* Set the title of the Font dialog */       setTitle("Selecting the Color");       /* Set resizable button to FALSE */       setResizable(false);       /* Initialize the object of JPanel */       panel = new JPanel();       /* Set the Layout as GridLayout*/       panel.setLayout(new GridLayout(5,2,1,1));       /* Add the panel to Color dialog frame */       getContentPane().add(panel);       /* Initialize and add Red label to the panel */             redLabel = new JLabel("Red: ");       panel.add(redLabel);       /* Initialize and add Red slider to the panel */       redSlider = new JSlider(0, 255, 1);       panel.add(redSlider);       /* Set Border to the Red slider */       redSlider.setBorder(BorderFactory.createEtchedBorder());       /* Add state change listener to Red slider */       redSlider.addChangeListener(this);       /* Initialize and add Green label to the panel */             greenLabel = new JLabel("Green: ");       panel.add(greenLabel);       /* Initialize and add Green slider to the panel */       greenSlider = new JSlider(0, 255, 1);       panel.add(greenSlider);       /* Set Border to the Green slider */       greenSlider.setBorder(BorderFactory.createEtchedBorder());       /* Add state change listener to Green slider */       greenSlider.addChangeListener(this);       /* Initialize and add Blue label to the panel */             blueLabel = new JLabel("Blue: ");       panel.add(blueLabel);       /* Initialize and add Blue slider to the panel */       blueSlider = new JSlider(0, 255, 1);       panel.add(blueSlider);       /* Set Border to the Blue slider */       blueSlider.setBorder(BorderFactory.createEtchedBorder());       /* Add state change listener to Blue slider */       blueSlider.addChangeListener(this);       /* Initialize and add Preview label to the panel */             previewLabel = new JLabel("Preview: ");       panel.add(previewLabel);       /* Initialize and add Preview text field to the panel */             labelText = new JTextField(10);       panel.add(labelText);       /* Initialize and add OK button to the panel */             JPanel pan=new JPanel(new FlowLayout(FlowLayout.CENTER));       ok = new JButton("OK");       ok.setPreferredSize(new Dimension(80,23));       pan.add(ok);       /* Initialize and add Cancel button to the panel */             cancel = new JButton("Cancel");       cancel.setPreferredSize(new Dimension(80,23));       pan.add(cancel);       panel.add(new JLabel(" "));       panel.add(pan);    }    /* getOk() method - This method is invoked when end user click the OK button of the Color dialog box          parameter - NA          return value - ok    */    public JButton getOk()    {       return ok;    }    /*    getCancel() method - This method is invoked when end user click the Cancel button of the Color dialog box    parameter - NA    return value - cancel    */    public JButton getCancel()    {       return cancel;    }    /*    stateChanged() - This method is called when the user slides any slider of the Color dialog box.    Parameters:   ce - an ChangeEvent object containing details of the event.    Return Value: NA     */    public void stateChanged(ChangeEvent ce)     {       /* This section is executed, when end user slides the Red slider */       if(ce.getSource() == redSlider)       {          r = redSlider.getValue();             }       /* This section is executed, when end user slides the Green slider */       else if(ce.getSource() == greenSlider)       {          g = greenSlider.getValue();       }       /* This section is executed, when end user slides the Blue slider */       else if(ce.getSource() == blueSlider)       {          b = blueSlider.getValue();             }       /* Create the object of Color class */       Color c = new Color(r, g, b);       /* Set the background color of the preview text field */       labelText.setBackground(c);          }    /*    color() - This method is set color of the file text    Parameters:   NA    Return Value: color     */    public Color color()    {       Color color = new Color(redSlider.getValue(), greenSlider.getValue(), blueSlider.getValue());       return color;    }    } 
end example

Download this listing.

The ColorChoose.java file provides a user interface with three sliders and two buttons. In addition, the user interface of the ColorChoose.java file provides a text box that shows a preview of the selected color. The end user can use the three sliders on the user interface of ColorChoose.java to specify the RGB value of the selected color. The OK button on the user interface sets the color of the text in the document to be printed to the selected color. The Cancel button on the user interface cancels the current selection. Figure 4-6 shows the user interface of the ColorChoose.java file:

click to expand: this figure shows the selecting the color dialog box to change the color of selected text.
Figure 4-6: The Selecting the Color Dialog Box

Clicking any button on the user interface of ColorChoose.java calls the actionPerformed() method. This method acts as a listener for the events that the end user generates.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net