issues 30 & 43 add color wheel and recent colors
ADD
builder/src/main/java/builder/views/ColorWheel.java
+package builder.views;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+
+import javax.swing.JPanel;
+
+
+public class ColorWheel extends JPanel {
+ private static final long serialVersionUID = 1L;
+
+ protected void paintComponent(Graphics g)
+ {
+ super.paintComponent(g);
+
+ float hueDegree = 1 / 360.0f;
+
+ for (int i = 0; i < 360; i++)
+ {
+ Color color = Color.getHSBColor(i * hueDegree, 1.0f, 1.0f);
+ g.setColor( color );
+ g.fillArc( 30, 15, 250, 250, i, 1);
+ }
+ }
+
+ @Override
+ public Dimension getPreferredSize()
+ {
+ return new Dimension(250, 250);
+ }
+}

MODIFY
builder/src/main/java/builder/views/GUIsliceColorChooser.java
builder/src/main/java/builder/views/GUIsliceColorChooser.java
*/
package builder.views;
-import java.awt.BorderLayout;
+import java.awt.AWTException;
+//import java.awt.BorderLayout;
import java.awt.Color;
+import java.awt.Cursor;
import java.awt.Font;
+import java.awt.Point;
+import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JFrame;
@@ -40,6 +46,7 @@ import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
+import builder.Builder;
import builder.common.ColorFactory;
import builder.common.ColorIcon;
import builder.common.ColorItem;
@@ -51,6 +58,7 @@ import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
+import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JList;
@@ -110,7 +118,7 @@ public class GUIsliceColorChooser extends JDialog {
private JTabbedPane tabbedPane;
/** The rgb panel. */
- private JPanel rgbPanel;
+ private JPanel rgbTab;
/** The color panel. */
private JPanel colorPanel;
@@ -127,6 +135,9 @@ public class GUIsliceColorChooser extends JDialog {
/** The current color. */
private Color currentColor;
+ /** recent color panel */
+ private SwatchPanel recentColorsPanel;
+
/** The red value. */
private int redValue;
@@ -154,23 +165,31 @@ public class GUIsliceColorChooser extends JDialog {
/** The spinner B. */
private JSpinner spinnerB;
- /** The is spinner. */
+ /** The data from spinner. */
private boolean isSpinner = false;
- /** The is list. */
+ /** The data from list. */
private boolean isList = false;
+ /** The data from eye dropper. */
+ private boolean isEyeDropper = false;
+
+ Robot robot;
+
/** The cf. */
ColorFactory cf;
/** The b save dialog. */
boolean bSaveDialog;
- /** The b init UI. */
+ /** The flag to indicate UI initialized. */
boolean bInitUI = false;
+ /** our cross hair cursor */
+ Cursor crossHairCursor;
+
/**
- * Instantiates a new GU islice color chooser.
+ * Instantiates a new GUIslice color chooser.
*
* @param owner
* the owner
@@ -178,6 +197,11 @@ public class GUIsliceColorChooser extends JDialog {
public GUIsliceColorChooser(JFrame owner) {
super(owner, "GUIslice Color Chooser", true);
cf = ColorFactory.getInstance();
+ try {
+ this.robot = new Robot();
+ } catch (AWTException e) {
+ e.printStackTrace();
+ }
}
/**
@@ -210,22 +234,23 @@ public class GUIsliceColorChooser extends JDialog {
*/
private void initUI() {
setBounds(100, 100, 550, 450);
+ crossHairCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
redValue = currentColor.getRed();
greenValue = currentColor.getGreen();
blueValue = currentColor.getBlue();
contentPane = (JPanel) getContentPane();
+
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(2, 0));
+// contentPane.setLayout(new BorderLayout(2, 0));
setContentPane(contentPane);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
- contentPane.add(tabbedPane, BorderLayout.CENTER);
- JScrollPane scrollPane = new JScrollPane();
- tabbedPane.addTab("GUIslice Colors", null, scrollPane, null);
- scrollPane.setPreferredSize(new Dimension(250, 300));
+ JScrollPane guisliceTab = new JScrollPane();
+ tabbedPane.addTab("GUIslice Colors", null, guisliceTab, null);
+ guisliceTab.setPreferredSize(new Dimension(250, 300));
// create the model and add elements
listModel = new DefaultListModel<>();
itemList = cf.getColorList();
@@ -236,10 +261,10 @@ public class GUIsliceColorChooser extends JDialog {
listSelectionModel = colorList.getSelectionModel();
colorList.setCellRenderer(new GUIsliceColorRenderer());
- scrollPane.setViewportView(colorList);
+ guisliceTab.setViewportView(colorList);
- rgbPanel = new JPanel();
- tabbedPane.addTab("RGB Colors", null, rgbPanel, null);
+ rgbTab = new JPanel();
+ tabbedPane.addTab("RGB Colors", null, rgbTab, null);
JLabel lblRed = new JLabel("Red:");
lblRed.setFont(new Font("Tahoma", Font.PLAIN, 14));
@@ -274,44 +299,32 @@ public class GUIsliceColorChooser extends JDialog {
sliderB.setPaintLabels(true);
sliderB.addChangeListener(new SliderListener());
- GroupLayout gl_rgbPanel = new GroupLayout(rgbPanel);
- gl_rgbPanel.setHorizontalGroup(
- gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_rgbPanel.createSequentialGroup()
- .addGap(57)
- .addGroup(gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addComponent(lblRed)
- .addComponent(lblGreen)
- .addComponent(lblBlue))
+ GroupLayout gl_rgbTab = new GroupLayout(rgbTab);
+ gl_rgbTab.setHorizontalGroup(gl_rgbTab.createParallelGroup(Alignment.LEADING).addGroup(gl_rgbTab
+ .createSequentialGroup().addGap(57)
+ .addGroup(gl_rgbTab
+ .createParallelGroup(Alignment.LEADING).addComponent(lblRed).addComponent(lblGreen).addComponent(lblBlue))
.addGap(30)
- .addGroup(gl_rgbPanel.createParallelGroup(Alignment.TRAILING)
+ .addGroup(gl_rgbTab.createParallelGroup(Alignment.TRAILING)
.addComponent(sliderG, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(sliderB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(sliderR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(54, Short.MAX_VALUE))
- );
- gl_rgbPanel.setVerticalGroup(
- gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_rgbPanel.createSequentialGroup()
- .addGap(57)
- .addGroup(gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addComponent(lblRed)
+ .addContainerGap(54, Short.MAX_VALUE)));
+ gl_rgbTab.setVerticalGroup(gl_rgbTab.createParallelGroup(Alignment.LEADING).addGroup(gl_rgbTab
+ .createSequentialGroup().addGap(57)
+ .addGroup(gl_rgbTab.createParallelGroup(Alignment.LEADING).addComponent(lblRed)
.addComponent(sliderR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(53)
- .addGroup(gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addComponent(lblGreen)
+ .addGroup(gl_rgbTab.createParallelGroup(Alignment.LEADING).addComponent(lblGreen)
.addComponent(sliderG, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(61)
- .addGroup(gl_rgbPanel.createParallelGroup(Alignment.LEADING)
- .addComponent(lblBlue)
- .addComponent(sliderB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(74, Short.MAX_VALUE))
- );
- rgbPanel.setLayout(gl_rgbPanel);
+ .addGroup(gl_rgbTab.createParallelGroup(Alignment.LEADING).addComponent(lblBlue).addComponent(sliderB,
+ GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
+ .addContainerGap(74, Short.MAX_VALUE)));
+ rgbTab.setLayout(gl_rgbTab);
colorPanel = new JPanel();
colorPanel.setPreferredSize(new Dimension(125, 350));
- contentPane.add(colorPanel, BorderLayout.EAST);
JLabel lblspinnerR = new JLabel("Red:");
spinnerR = new JSpinner();
@@ -338,13 +351,19 @@ public class GUIsliceColorChooser extends JDialog {
JLabel lblTxtPreview = new JLabel("Preview");
lblTxtPreview.setFont(new Font("Tahoma", Font.BOLD, 12));
+ JLabel lblRecentColors = new JLabel("Recent Colors");
+
+ recentColorsPanel = new SwatchPanel();
+ RecentSwatchListener recentSwatchListener = new RecentSwatchListener();
+ recentColorsPanel.addMouseListener(recentSwatchListener);
+
GroupLayout gl_colorPanel = new GroupLayout(colorPanel);
gl_colorPanel.setHorizontalGroup(
gl_colorPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_colorPanel.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_colorPanel.createParallelGroup(Alignment.TRAILING)
+ .addGroup(gl_colorPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_colorPanel.createSequentialGroup()
+ .addContainerGap()
.addGroup(gl_colorPanel.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_colorPanel.createSequentialGroup()
.addComponent(lblspinnerB)
@@ -357,23 +376,35 @@ public class GUIsliceColorChooser extends JDialog {
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_colorPanel.createParallelGroup(Alignment.LEADING)
.addComponent(spinnerG, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(spinnerR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
- .addContainerGap(28, Short.MAX_VALUE))
+ .addComponent(spinnerR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
+ .addGap(1))))
.addGroup(gl_colorPanel.createSequentialGroup()
- .addGap(12)
- .addGroup(gl_colorPanel.createParallelGroup(Alignment.TRAILING)
- .addComponent(lblTxtPreview)
- .addComponent(lblPreview, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(39))))
+ .addGap(38)
+ .addComponent(lblPreview, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
+ .addGap(30))
+ .addGroup(gl_colorPanel.createSequentialGroup()
+ .addGap(21)
+ .addComponent(lblRecentColors)
+ .addGap(37))
+ .addGroup(gl_colorPanel.createSequentialGroup()
+ .addGap(30)
+ .addGroup(gl_colorPanel.createParallelGroup(Alignment.LEADING)
+ .addComponent(recentColorsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
+ .addComponent(lblTxtPreview, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
+ .addContainerGap())
);
gl_colorPanel.setVerticalGroup(
gl_colorPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_colorPanel.createSequentialGroup()
- .addContainerGap(103, Short.MAX_VALUE)
+ .addContainerGap()
+ .addComponent(lblRecentColors)
+ .addGap(18)
+ .addComponent(recentColorsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
.addComponent(lblTxtPreview)
- .addPreferredGap(ComponentPlacement.UNRELATED)
+ .addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblPreview, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(38)
+ .addGap(13)
.addGroup(gl_colorPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblspinnerR)
.addComponent(spinnerR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
@@ -391,11 +422,11 @@ public class GUIsliceColorChooser extends JDialog {
JPanel commandPanel = new JPanel();
commandPanel.setPreferredSize(new Dimension(450, 50));
- contentPane.add(commandPanel, BorderLayout.SOUTH);
btnOK = new JButton("OK");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
+ recentColorsPanel.setMostRecentColor(currentColor);
bSaveDialog = true;
Closed_Option = JOptionPane.OK_OPTION;
dispose();
@@ -414,25 +445,62 @@ public class GUIsliceColorChooser extends JDialog {
btnCANCEL.addActionListener(actionListener);
GroupLayout gl_commandPanel = new GroupLayout(commandPanel);
- gl_commandPanel.setHorizontalGroup(
- gl_commandPanel.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_commandPanel.createSequentialGroup()
- .addContainerGap(230, Short.MAX_VALUE)
- .addComponent(btnCANCEL)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(btnOK)
- .addContainerGap())
- );
- gl_commandPanel.setVerticalGroup(
- gl_commandPanel.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_commandPanel.createSequentialGroup()
- .addContainerGap(16, Short.MAX_VALUE)
- .addGroup(gl_commandPanel.createParallelGroup(Alignment.BASELINE)
- .addComponent(btnOK)
- .addComponent(btnCANCEL))
- .addContainerGap())
- );
+ gl_commandPanel
+ .setHorizontalGroup(gl_commandPanel.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING,
+ gl_commandPanel.createSequentialGroup().addContainerGap(230, Short.MAX_VALUE).addComponent(btnCANCEL)
+ .addPreferredGap(ComponentPlacement.RELATED).addComponent(btnOK).addContainerGap()));
+ gl_commandPanel.setVerticalGroup(gl_commandPanel.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING,
+ gl_commandPanel.createSequentialGroup().addContainerGap(16, Short.MAX_VALUE)
+ .addGroup(
+ gl_commandPanel.createParallelGroup(Alignment.BASELINE).addComponent(btnOK).addComponent(btnCANCEL))
+ .addContainerGap()));
commandPanel.setLayout(gl_commandPanel);
+ GroupLayout gl_contentPane = new GroupLayout(contentPane);
+ gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
+ .addGroup(gl_contentPane.createSequentialGroup()
+ .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 397, GroupLayout.PREFERRED_SIZE).addGap(2)
+ .addComponent(colorPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
+ .addComponent(commandPanel, GroupLayout.PREFERRED_SIZE, 524, GroupLayout.PREFERRED_SIZE));
+ gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
+ .addGroup(gl_contentPane.createSequentialGroup()
+ .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
+ .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 351, GroupLayout.PREFERRED_SIZE)
+ .addComponent(colorPanel, GroupLayout.PREFERRED_SIZE, 351, GroupLayout.PREFERRED_SIZE))
+ .addComponent(commandPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
+ GroupLayout.PREFERRED_SIZE)));
+
+ JPanel wheelTab = new JPanel();
+ tabbedPane.addTab("Color Wheel", null, wheelTab, null);
+
+ JButton btnEyeDropper = new JButton("");
+ btnEyeDropper.setIcon(new ImageIcon(Builder.class.getResource("/resources/icons/eyedropper.png")));
+ btnEyeDropper.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent evt) {
+ isEyeDropper = true;
+ setCursor(crossHairCursor);
+ }
+ });
+
+ JPanel wheelPanel = new ColorWheel();
+ wheelPanel.addMouseListener(new MouseHandler());
+
+ JLabel lblUseEyeDropper = new JLabel("Use Eye Dropper to Choose your Color");
+ GroupLayout gl_wheelTab = new GroupLayout(wheelTab);
+ gl_wheelTab.setHorizontalGroup(gl_wheelTab.createParallelGroup(Alignment.LEADING)
+ .addGroup(gl_wheelTab.createSequentialGroup().addContainerGap()
+ .addComponent(wheelPanel, GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
+ .addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnEyeDropper).addGap(18))
+ .addGroup(gl_wheelTab.createSequentialGroup().addGap(67)
+ .addComponent(lblUseEyeDropper, GroupLayout.PREFERRED_SIZE, 218, GroupLayout.PREFERRED_SIZE)
+ .addContainerGap(107, Short.MAX_VALUE)));
+ gl_wheelTab.setVerticalGroup(gl_wheelTab.createParallelGroup(Alignment.LEADING)
+ .addGroup(gl_wheelTab.createSequentialGroup().addGap(4).addComponent(lblUseEyeDropper)
+ .addPreferredGap(ComponentPlacement.UNRELATED)
+ .addGroup(gl_wheelTab.createParallelGroup(Alignment.LEADING)
+ .addComponent(wheelPanel, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE).addComponent(btnEyeDropper))
+ .addContainerGap()));
+ wheelTab.setLayout(gl_wheelTab);
+ contentPane.setLayout(gl_contentPane);
// create the model and add elements
listModel = new DefaultListModel<>();
@@ -442,18 +510,44 @@ public class GUIsliceColorChooser extends JDialog {
}
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.addListSelectionListener(new ColorSelectionHandler());
+ }
- pack();
- setResizable(false);
+ public void setSelectedColor(Color c) {
+ currentColor = c;
+ isList = true;
+ if (redValue != currentColor.getRed()) {
+ redValue = currentColor.getRed();
+ spinnerR.setValue(Integer.valueOf(redValue));
+ sliderR.setValue(Integer.valueOf(redValue));
+ }
+ if (greenValue != currentColor.getGreen()) {
+ greenValue = currentColor.getGreen();
+ spinnerG.setValue(Integer.valueOf(greenValue));
+ sliderG.setValue(Integer.valueOf(greenValue));
+ }
+ if (blueValue != currentColor.getBlue()) {
+ blueValue = currentColor.getBlue();
+ spinnerB.setValue(Integer.valueOf(blueValue));
+ sliderB.setValue(Integer.valueOf(blueValue));
+ }
+ previewIcon.setColor(currentColor);
+ ColorItem item = cf.findColorItem(currentColor);
+ colorIndex = item.getIndex();
+ colorList.setSelectedIndex(colorIndex);
+ colorList.ensureIndexIsVisible(colorIndex);
+ colorPanel.repaint();
}
- /**
- * Gets the option.
- *
- * @return the option
- */
- public int getOption() {
- return Closed_Option;
+ class RecentSwatchListener extends MouseAdapter {
+ public void mousePressed(MouseEvent e) {
+ if (isEnabled()) {
+ Color color = recentColorsPanel.getColorForLocation(e.getX(), e.getY());
+ recentColorsPanel.setSelectedColorFromLocation(e.getX(), e.getY());
+ // ignore if setting to default color
+ if (color.getRGB() != -1118482)
+ setSelectedColor(color);
+ }
+ }
}
/**
@@ -489,22 +583,7 @@ public class GUIsliceColorChooser extends JDialog {
repaint();
}
- /**
- * The listener interface for receiving spinner events. The class that is
- * interested in processing a spinner event implements this interface, and the
- * object created with that class is registered with a component using the
- * component's <code>addSpinnerListener<code> method. When the spinner event
- * occurs, that object's appropriate method is invoked.
- *
- * @see SpinnerEvent
- */
class SpinnerListener implements ChangeListener {
-
- /**
- * stateChanged
- *
- * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
- */
@Override
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner) e.getSource();
@@ -527,27 +606,12 @@ public class GUIsliceColorChooser extends JDialog {
colorList.setSelectedIndex(colorIndex);
colorList.ensureIndexIsVisible(colorIndex);
isSpinner = false;
- repaint();
+ colorPanel.repaint();
+ }
}
}
- };
- /**
- * The listener interface for receiving slider events. The class that is
- * interested in processing a slider event implements this interface, and the
- * object created with that class is registered with a component using the
- * component's <code>addSliderListener<code> method. When the slider event
- * occurs, that object's appropriate method is invoked.
- *
- * @see SliderEvent
- */
class SliderListener implements ChangeListener {
-
- /**
- * stateChanged
- *
- * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
- */
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
if (isList == false && isSpinner == false) {
@@ -573,16 +637,7 @@ public class GUIsliceColorChooser extends JDialog {
}
}
- /**
- * The Class ColorSelectionHandler.
- */
class ColorSelectionHandler implements ListSelectionListener {
-
- /**
- * valueChanged
- *
- * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
- */
public void valueChanged(ListSelectionEvent e) {
ColorItem item;
int idx = colorList.getSelectedIndex();
@@ -614,5 +669,51 @@ public class GUIsliceColorChooser extends JDialog {
}
}
}
+ /**
+ * The Class MouseHandler.
+ */
+ private class MouseHandler extends MouseAdapter {
+
+ /**
+ * mouseClicked.
+ *
+ * @param e
+ * the e
+ * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
+ */
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ if (isEyeDropper) {
+ Point p = e.getLocationOnScreen();
+ Color c = robot.getPixelColor(p.x, p.y);
+ setSelectedColor(c);
+ setCursor(Cursor.getDefaultCursor());
+ isEyeDropper = false;
+ }
+ } // end mouseClicked
+
+ /**
+ * mouseReleased.
+ *
+ * @param e
+ * the e
+ * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
+ */
+ @Override
+ public void mouseReleased(MouseEvent e) {
+ } // end mouseReleased
+
+ /**
+ * mousePressed.
+ *
+ * @param e
+ * the e
+ * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
+ */
+ @Override
+ public void mousePressed(MouseEvent e) {
+ } // end mousePressed
+ } // end MouseHandler
+
}

ADD
builder/src/main/java/builder/views/SwatchPanel.java
+/**
+ *
+ * The MIT License
+ *
+ * Copyright 2019 Paul Conti
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package builder.views;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.MouseEvent;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Base64;
+import java.util.LinkedList;
+
+import javax.swing.JPanel;
+import javax.swing.UIManager;
+
+import builder.prefs.GeneralEditor;
+
+/**
+ * The Class SwatchPanel manages recent color selection
+ * They are stored inside the users preferences General Model.
+ *
+ * @author Paul Conti
+ */
+public class SwatchPanel extends JPanel {
+
+ /** The Constant serialVersionUID. */
+ private static final long serialVersionUID = 1L;
+
+ /** The Constant CUSTOM_COLORS_FILE. */
+ public static final String CUSTOM_COLORS_FILE = "custom_colors.list";
+
+ /** size of color square in our recent colors panel */
+ public static final int NSQUARE = 15;
+
+ /** number of columns in our recent colors panel */
+ public static final int NCOLUMNS = 4;
+
+ /** number of rows in our recent colors panel */
+ public static final int NROWS = 4;
+
+ /** The general editor. */
+ private GeneralEditor generalEditor;
+
+ /** lru cache of recent colors */
+ LinkedList<Color> lruList = new LinkedList<Color>();
+
+ /** The swatch size. */
+ protected Dimension swatchSize;
+
+ /** The num swatches. */
+ protected Dimension numSwatches;
+
+ /** The gap. */
+ protected Dimension gap;
+
+ /** The selected row. */
+ private int selRow;
+
+ /** The selected col. */
+ private int selCol;
+
+ /** The num colors. */
+ private int numColors;
+
+ /**
+ * Instantiates a new swatch panel.
+ */
+ public SwatchPanel() {
+ initValues();
+ initColors();
+ setToolTipText(""); // register for events
+ setOpaque(true);
+ setBackground(Color.white);
+ setFocusable(true);
+ setInheritsPopupMenu(true);
+
+ addFocusListener(new FocusAdapter() {
+ public void focusGained(FocusEvent e) {
+ repaint();
+ }
+
+ public void focusLost(FocusEvent e) {
+ repaint();
+ }
+ });
+ }
+
+ /**
+ * Gets the selected color.
+ *
+ * @return the selected color
+ */
+ public Color getSelectedColor() {
+ return getColorForCell(selCol, selRow);
+ }
+
+ /**
+ * Initializes the values.
+ */
+ protected void initValues() {
+ swatchSize = new Dimension(NSQUARE, NSQUARE);
+ numSwatches = new Dimension(NCOLUMNS, NROWS);
+ numColors = NCOLUMNS * NROWS;
+ gap = new Dimension(1, 1);
+ generalEditor = GeneralEditor.getInstance();
+ }
+
+ /**
+ * paintComponent
+ *
+ * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
+ */
+ public void paintComponent(Graphics g) {
+ g.setColor(getBackground());
+ g.fillRect(0, 0, getWidth(), getHeight());
+ for (int row = 0; row < numSwatches.height; row++) {
+ int y = row * (swatchSize.height + gap.height);
+ for (int column = 0; column < numSwatches.width; column++) {
+ Color c = getColorForCell(column, row);
+ g.setColor(c);
+ int x;
+ if (!this.getComponentOrientation().isLeftToRight()) {
+ x = (numSwatches.width - column - 1) * (swatchSize.width + gap.width);
+ } else {
+ x = column * (swatchSize.width + gap.width);
+ }
+ g.fillRect(x, y, swatchSize.width, swatchSize.height);
+ g.setColor(Color.black);
+ g.drawLine(x + swatchSize.width - 1, y, x + swatchSize.width - 1, y + swatchSize.height - 1);
+ g.drawLine(x, y + swatchSize.height - 1, x + swatchSize.width - 1, y + swatchSize.height - 1);
+
+ if (selRow == row && selCol == column && this.isFocusOwner()) {
+ Color c2 = new Color(c.getRed() < 125 ? 255 : 0, c.getGreen() < 125 ? 255 : 0, c.getBlue() < 125 ? 255 : 0);
+ g.setColor(c2);
+
+ g.drawLine(x, y, x + swatchSize.width - 1, y);
+ g.drawLine(x, y, x, y + swatchSize.height - 1);
+ g.drawLine(x + swatchSize.width - 1, y, x + swatchSize.width - 1, y + swatchSize.height - 1);
+ g.drawLine(x, y + swatchSize.height - 1, x + swatchSize.width - 1, y + swatchSize.height - 1);
+ g.drawLine(x, y, x + swatchSize.width - 1, y + swatchSize.height - 1);
+ g.drawLine(x, y + swatchSize.height - 1, x + swatchSize.width - 1, y);
+ }
+ }
+ }
+ }
+
+ /**
+ * getPreferredSize
+ *
+ * @see javax.swing.JComponent#getPreferredSize()
+ */
+ public Dimension getPreferredSize() {
+ int x = numSwatches.width * (swatchSize.width + gap.width) - 1;
+ int y = numSwatches.height * (swatchSize.height + gap.height) - 1;
+ return new Dimension(x, y);
+ }
+
+ /**
+ * Initializes the colors.
+ */
+ @SuppressWarnings("unchecked")
+ protected void initColors() {
+ Color defaultColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor", getLocale());
+ String recentColors = generalEditor.getRecentColors();
+ // do we have any user preferences for colors?
+ if (recentColors.length() > 0) {
+ ObjectInputStream in;
+ try {
+ byte[] data = Base64.getDecoder().decode(recentColors);
+ in = new ObjectInputStream(new ByteArrayInputStream(data));
+ lruList = (LinkedList<Color>) in.readObject();
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ } else {
+ // first time we initialize lru with default colors
+ for (int i = 0; i < numColors; i++) {
+ lruList.addFirst(defaultColor);
+ }
+ }
+ }
+
+ /**
+ * getToolTipText
+ *
+ * @see javax.swing.JComponent#getToolTipText(java.awt.event.MouseEvent)
+ */
+ public String getToolTipText(MouseEvent e) {
+ Color color = getColorForLocation(e.getX(), e.getY());
+ return color.getRed() + ", " + color.getGreen() + ", " + color.getBlue();
+ }
+
+ /**
+ * Sets the selected color from location.
+ *
+ * @param x
+ * the x
+ * @param y
+ * the y
+ */
+ public void setSelectedColorFromLocation(int x, int y) {
+ if (!this.getComponentOrientation().isLeftToRight()) {
+ selCol = numSwatches.width - x / (swatchSize.width + gap.width) - 1;
+ } else {
+ selCol = x / (swatchSize.width + gap.width);
+ }
+ selRow = y / (swatchSize.height + gap.height);
+ repaint();
+ }
+
+ /**
+ * Gets the color for location.
+ *
+ * @param x
+ * the x
+ * @param y
+ * the y
+ * @return the color for location
+ */
+ public Color getColorForLocation(int x, int y) {
+ int column;
+ if (!this.getComponentOrientation().isLeftToRight()) {
+ column = numSwatches.width - x / (swatchSize.width + gap.width) - 1;
+ } else {
+ column = x / (swatchSize.width + gap.width);
+ }
+ int row = y / (swatchSize.height + gap.height);
+ return getColorForCell(column, row);
+ }
+
+ /**
+ * Gets the color for cell.
+ *
+ * @param column
+ * the column
+ * @param row
+ * the row
+ * @return the color for cell
+ */
+ private Color getColorForCell(int column, int row) {
+ // convert a 2D array index into a 1D index
+ int idx = row * NCOLUMNS + column;
+ return lruList.get(idx);
+ }
+
+ /**
+ * Sets the most recent color.
+ *
+ * @param c
+ * the new most recent color
+ */
+ public void setMostRecentColor(Color c) {
+ // update our lru but first check and see if the color is already present
+ // if so, remove it then add to front of list
+ if (!lruList.remove(c)) {
+ lruList.removeLast();
+ }
+ lruList.addFirst(c);
+ repaint();
+ // now push the recent colors panel to user preferences
+ ObjectOutputStream out;
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ out = new ObjectOutputStream(baos);
+ out.writeObject(lruList);
+ out.close();
+ String recentColors = Base64.getEncoder().encodeToString(baos.toByteArray());
+ generalEditor.setRecentColors(recentColors);
+ generalEditor.savePreferences();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}

issues 30 & 43 add color wheel and recent colors