Search in sources :

Example 1 with ColorSelectionModel

use of javax.swing.colorchooser.ColorSelectionModel in project com.revolsys.open by revolsys.

the class ColorAlphaPanel method stateChanged.

@Override
public void stateChanged(final ChangeEvent e) {
    final Color color = getColorFromModel();
    final Color newColor = WebColors.newAlpha(color, this.alphaSlider.getValue());
    final ColorSelectionModel colorSelectionModel = getColorSelectionModel();
    colorSelectionModel.setSelectedColor(newColor);
}
Also used : Color(java.awt.Color) ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel)

Example 2 with ColorSelectionModel

use of javax.swing.colorchooser.ColorSelectionModel in project psiviewer by cmf.

the class AlphaChooserPanel method buildChooser.

/**
 * Builds a new chooser panel.
 */
protected void buildChooser() {
    setAlphaSlider(new CompoundSlider("Alpha", getColorSelectionModel()));
    add(getAlphaSlider());
    getColorSelectionModel().addChangeListener(new ChangeListener() {

        /**
         * When the user chooses a color, make sure that the current
         * alpha slider value is applied to it - otherwise it defaults
         * to alpha=255 (opaque).
         */
        public void stateChanged(ChangeEvent e) {
            ColorSelectionModel model = (ColorSelectionModel) e.getSource();
            Color selectedColor = model.getSelectedColor();
            if (selectedColor.getAlpha() != getAlphaSlider().getValue()) {
                Color colorWithAlphaApplied = new Color(selectedColor.getRed(), selectedColor.getGreen(), selectedColor.getBlue(), getAlphaSlider().getValue());
                model.setSelectedColor(colorWithAlphaApplied);
            }
        }
    });
}
Also used : ChangeEvent(javax.swing.event.ChangeEvent) ChangeListener(javax.swing.event.ChangeListener) ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel)

Example 3 with ColorSelectionModel

use of javax.swing.colorchooser.ColorSelectionModel in project opennars by opennars.

the class ColorPreviewPanel method stateChanged.

public void stateChanged(ChangeEvent e) {
    ColorSelectionModel csm = (ColorSelectionModel) e.getSource();
    lblCurrColor.setBackground(csm.getSelectedColor());
}
Also used : ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel)

Example 4 with ColorSelectionModel

use of javax.swing.colorchooser.ColorSelectionModel in project mafscaling by vimsh.

the class LogView method createWotChart.

protected void createWotChart() {
    wotMarker = new XYDomainMutilineAnnotation();
    wotMarker.setDefaultPaint(Color.WHITE);
    JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, null, PlotOrientation.VERTICAL, false, true, false);
    wotChartPanel = new ChartPanel(chart, true, true, true, true, true);
    wotChartPanel.setFocusable(true);
    wotChartPanel.setAutoscrolls(true);
    chart.setBackgroundPaint(chartColor);
    wotPlot = chart.getXYPlot();
    wotPlot.setRangePannable(true);
    wotPlot.setDomainPannable(true);
    wotPlot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    wotPlot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    wotPlot.setBackgroundPaint(chartBgColor);
    wotPlot.addAnnotation(wotMarker);
    wotPlot.setRangeAxis(null);
    NumberAxis xAxis = new NumberAxis();
    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setTickLabelPaint(Color.WHITE);
    xAxis.setLabelPaint(Color.LIGHT_GRAY);
    wotPlot.setDomainAxis(0, xAxis);
    wotPlot.setDataset(0, new XYSeriesCollection());
    wotPlot.mapDatasetToDomainAxis(0, 0);
    wotPlot.mapDatasetToRangeAxis(0, 0);
    LegendTitle legend = new LegendTitle(wotPlot);
    legend.setItemFont(new Font("Arial", 0, 10));
    legend.setPosition(RectangleEdge.TOP);
    legend.setItemPaint(Color.WHITE);
    chart.addLegend(legend);
    wotChartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            try {
                wotPlot.clearRangeMarkers();
                wotMarker.clearLabels(false);
                Rectangle2D dataArea = wotChartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
                Point2D p = wotChartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
                double x = wotPlot.getDomainAxis().java2DToValue(p.getX(), dataArea, wotPlot.getDomainAxisEdge());
                XYSeriesCollection dataset;
                XYSeries series;
                for (int i = 0; i < wotPlot.getDatasetCount(); ++i) {
                    dataset = (XYSeriesCollection) wotPlot.getDataset(i);
                    if (dataset != null && dataset.getSeriesCount() > 0) {
                        for (int j = 0; j < dataset.getSeriesCount(); ++j) {
                            series = dataset.getSeries(j);
                            int idx = 0;
                            double closest;
                            double pclosest = Math.abs(series.getX(0).doubleValue() - x);
                            for (int k = 1; k < series.getItemCount(); ++k) {
                                closest = Math.abs(series.getX(k).doubleValue() - x);
                                if (closest < pclosest) {
                                    idx = k;
                                    pclosest = closest;
                                }
                            }
                            double x0 = series.getX(0).doubleValue();
                            double x1 = series.getX(series.getItemCount() - 1).doubleValue();
                            double xerr = x0 * 0.01;
                            if (x >= (x0 - xerr) && x <= (x1 + xerr))
                                wotMarker.addLabel(series.getDescription() + ": ", wotPlot.getRenderer(i).getSeriesPaint(j), series.getY(idx).toString(), false);
                        }
                    }
                }
                if (wotMarker.count() > 0) {
                    boolean isLeft = (p.getX() < (dataArea.getMaxX() - dataArea.getMinX()) / 2) ? true : false;
                    if (isLeft) {
                        wotMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
                        wotMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
                    } else {
                        wotMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
                        wotMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
                    }
                    wotMarker.setValue(x);
                }
                wotChartPanel.repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.error(ex);
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            wotChartPanel.requestFocusInWindow();
            ChartEntity entity = event.getEntity();
            if (entity == null || !(entity instanceof LegendItemEntity))
                return;
            LegendItemEntity itemEntity = (LegendItemEntity) entity;
            int seriesIndex = ((XYDataset) itemEntity.getDataset()).indexOf(itemEntity.getSeriesKey());
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) wotChartPanel.getChart().getXYPlot().getRenderer();
            Paint p = renderer.getSeriesPaint(seriesIndex);
            colorChooser.setColor((Color) p);
            final ColorPreviewPanel preview = new ColorPreviewPanel(colorChooser);
            colorChooser.setPreviewPanel(preview);
            AbstractColorChooserPanel[] panels = colorChooser.getChooserPanels();
            for (AbstractColorChooserPanel accp : panels) {
                if (!accp.getDisplayName().equals("Swatches")) {
                    colorChooser.removeChooserPanel(accp);
                }
            }
            ColorSelectionModel model = colorChooser.getSelectionModel();
            model.addChangeListener(new ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent event) {
                    ColorSelectionModel model = (ColorSelectionModel) event.getSource();
                    preview.setColor(model.getSelectedColor());
                }
            });
            if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, colorChooser, "Select new color for " + itemEntity.getSeriesKey(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE))
                renderer.setSeriesPaint(seriesIndex, colorChooser.getColor());
        }
    });
    wotChartPanel.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (!wotChartPanel.hasFocus())
                return;
            int keyCode = e.getKeyCode();
            if (keyCode < KeyEvent.VK_LEFT || keyCode > KeyEvent.VK_DOWN)
                return;
            ValueAxis axis = null;
            if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT)
                axis = ((XYPlot) wotChartPanel.getChart().getXYPlot()).getDomainAxis();
            else
                axis = ((XYPlot) wotChartPanel.getChart().getXYPlot()).getRangeAxis();
            if (axis != null) {
                double delta = (axis.getUpperBound() - axis.getLowerBound()) / 100.0;
                if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_DOWN)
                    axis.setRange(axis.getLowerBound() - delta, axis.getUpperBound() - delta);
                else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_RIGHT)
                    axis.setRange(axis.getLowerBound() + delta, axis.getUpperBound() + delta);
            }
        }

        public void keyReleased(KeyEvent arg0) {
        }

        public void keyTyped(KeyEvent arg0) {
        }
    });
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) ChartPanel(org.jfree.chart.ChartPanel) NumberAxis(org.jfree.chart.axis.NumberAxis) LegendTitle(org.jfree.chart.title.LegendTitle) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) Font(java.awt.Font) KeyEvent(java.awt.event.KeyEvent) AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) Point2D(java.awt.geom.Point2D) ValueAxis(org.jfree.chart.axis.ValueAxis) ChangeListener(javax.swing.event.ChangeListener) ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) LegendItemEntity(org.jfree.chart.entity.LegendItemEntity) Paint(java.awt.Paint) JFreeChart(org.jfree.chart.JFreeChart) IOException(java.io.IOException) ChartMouseListener(org.jfree.chart.ChartMouseListener) ChangeEvent(javax.swing.event.ChangeEvent) KeyListener(java.awt.event.KeyListener) ChartEntity(org.jfree.chart.entity.ChartEntity) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 5 with ColorSelectionModel

use of javax.swing.colorchooser.ColorSelectionModel in project hid-serial by rayshobby.

the class ColorPreviewPanel method stateChanged.

public void stateChanged(ChangeEvent e) {
    ColorSelectionModel csm = (ColorSelectionModel) e.getSource();
    lblCurrColor.setBackground(csm.getSelectedColor());
}
Also used : ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel)

Aggregations

ColorSelectionModel (javax.swing.colorchooser.ColorSelectionModel)5 Color (java.awt.Color)2 ChangeEvent (javax.swing.event.ChangeEvent)2 ChangeListener (javax.swing.event.ChangeListener)2 Font (java.awt.Font)1 Paint (java.awt.Paint)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 Point2D (java.awt.geom.Point2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 IOException (java.io.IOException)1 AbstractColorChooserPanel (javax.swing.colorchooser.AbstractColorChooserPanel)1 ChartMouseEvent (org.jfree.chart.ChartMouseEvent)1 ChartMouseListener (org.jfree.chart.ChartMouseListener)1 ChartPanel (org.jfree.chart.ChartPanel)1 JFreeChart (org.jfree.chart.JFreeChart)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 ChartEntity (org.jfree.chart.entity.ChartEntity)1 LegendItemEntity (org.jfree.chart.entity.LegendItemEntity)1