Class ColorChooserPanel

  • All Implemented Interfaces:
    Colorizer, Initializable, ImageObserver, MenuContainer, Serializable, Accessible

    public class ColorChooserPanel
    extends JPanel
    implements Initializable, Colorizer

    A panel that allows the selection of one color. This is an redesign of the JColorChooser. This isn't because the JColorChooser is flawed; in fact, we re-use much of the design of the JColorChooser. We just felt that only one color system needed to be present, and that there was no way to visually detect what color had been selected. We fixed this by only showing one color system at a time, and by removing a select few parts of the JColorChooser. The parts we removed from the chooser were the label and text field that allowed you to manually enter a specific value. We felt this busied the screen, and was unnecessary. The main purpose of this GUI is to get a Color, and we do so via the getColor method. However, we also allow the addition of ColorUpdaters, which will automatically update the color of the given Colorizer when the underlying JColorChoosers color has been updated. Note: For some reason, you must add this component as the main component of the UI. This is not a huge deal, but it will not display unless it is positioned at BorderLayout.CENTER or is a main component of a GridBagLayout. When you display the panel, it will look like this:

    Since:
    JTalker 0.1.5
    Version:
    0.0.1
    Author:
    C. William Oswald
    See Also:
    JColorChooser, Serialized Form
    • Field Detail

      • colorChooser

        protected JColorChooser colorChooser
        The JColorChooser used to get the Color. Note that this panel never actually displays the underlying JColorChooser; we only use its components within this panel.
    • Constructor Detail

      • ColorChooserPanel

        public ColorChooserPanel​(int colorSystem)
        Constructs a ColorChooserPanel with the color system. Note that this does not complete the construction; the init method must be called.
        Parameters:
        colorSystem - The coloring system to use when selecting a color.
        See Also:
        GUIConstants.COLOR_SYSTEM
      • ColorChooserPanel

        public ColorChooserPanel()
        Constructs a ColorChooerPanel with the default color system. This is currently set to 3. See that class for the possible values and what they mean.
    • Method Detail

      • init

        public void init()
        Adds all of the graphical and utility components to this panel. This will add all of the graphical components, as well as the ability for the color up top to change when the underlying JColorChooser changes its color. A big thanks to stack overflow for this question; I have since lost the link to the code (we have heavily modified it since). We are very proud of the fact that this class just redraws a JColorChooser the way we want to; however, in order to do so, we had to make sure the systems we used had a compatible way to do so. This is why the zeroeth version of the panels returned by JColorChoosers panels method (which allows this hack to work) is not included in the given color systems; it is just too different from the other panels. Due to the fact that each panels components are drawn in the same manner, we are allowed to extract the data from those AbstractColorChooserPanel using the Container.getComponents() method. This will also set the width of the slider to 25. That was the only GUI element which needed a size set; everything else is already defined by the JColorChooser.
        Specified by:
        init in interface Initializable
      • addColorUpdater

        public void addColorUpdater​(Colorizer colorizer)
        Adds a ColorUpdater to this ColorChooserPanel. This is just a way to avoid ugly code like this:

        ccp.addChangeListener(ccp.new ColorUpdater(ssp::setColor));

        Parameters:
        colorizer - The colorizer make a ColorUpdater out of.
      • getColor

        public Color getColor()
        Returns the chosen color.
        Returns:
        The chosen color.