/* * Geotoolkit - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2008 - 2009, Johann Sorel * (C) 2011 Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ package org.geotoolkit.gui.swing.util; import java.awt.Color; import java.awt.Component; import javax.swing.JOptionPane; import org.geotoolkit.gui.swing.resource.MessageBundle; /** * ColorChooser with alpha * * @author Johann Sorel (Geomatys) * @module */ public class JAlphaColorChooser extends javax.swing.JPanel { /** Creates new form JAlphaColorChooser */ public JAlphaColorChooser(Color c) { initComponents(); setColor(c); } public Color getColor(){ Color c = this.guiColor.getColor(); int alpha = guiOpacity.getValue(); c = new Color(c.getRed(), c.getGreen(), c.getBlue(),alpha); return c; } public void setColor(Color c){ if(c == null){ c = Color.WHITE; } this.guiColor.setColor(c); this.guiOpacity.setValue(c.getAlpha()); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { guiColor = new javax.swing.JColorChooser(); guiOpacity = new javax.swing.JSlider(); jSeparator1 = new javax.swing.JSeparator(); jLabel1 = new javax.swing.JLabel(); guiOpacity.setMaximum(255); guiOpacity.setValue(255); jLabel1.setText(MessageBundle.format("opacity")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(guiColor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(guiOpacity, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(guiColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(guiOpacity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JColorChooser guiColor; private javax.swing.JSlider guiOpacity; private javax.swing.JLabel jLabel1; private javax.swing.JSeparator jSeparator1; // End of variables declaration//GEN-END:variables public static Color showDialog(Component component, String title, Color initialColor) { final JAlphaColorChooser pane = new JAlphaColorChooser(initialColor != null? initialColor : Color.white); JOptionPane.showMessageDialog(component,pane,title,JOptionPane.PLAIN_MESSAGE); return pane.getColor(); } }