Example usage for java.awt LinearGradientPaint LinearGradientPaint

List of usage examples for java.awt LinearGradientPaint LinearGradientPaint

Introduction

In this page you can find the example usage for java.awt LinearGradientPaint LinearGradientPaint.

Prototype

public LinearGradientPaint(Point2D start, Point2D end, float[] fractions, Color[] colors,
        CycleMethod cycleMethod) 

Source Link

Document

Constructs a LinearGradientPaint with a default SRGB color space.

Usage

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java

/**
 * Creates a continuous legend item for one item in dimensionSet, i.e. dimensionSet must be a
 * set containing exactly one value.//from  w w  w . j a  va2 s .  c  o  m
 * 
 * @param dateFormat
 *            format used to format minValue and maxValue as dates, or null if they should be
 *            displayed numerically instead of as dates
 * @throws ChartPlottimeException
 */
private LegendItem createContinuousLegendItem(PlotInstance plotInstance, Set<PlotDimension> dimensionSet,
        double minValue, double maxValue, DateFormat dateFormat) {
    PlotConfiguration plotConfiguration = plotInstance.getCurrentPlotConfigurationClone();
    PlotDimension dimension = dimensionSet.iterator().next();
    DefaultDimensionConfig dimensionConfig = (DefaultDimensionConfig) plotConfiguration
            .getDimensionConfig(dimension);
    DimensionConfigData dimensionConfigData = plotInstance.getPlotData()
            .getDimensionConfigData(dimensionConfig);
    // String label = dimensionConfig.getLabel();
    // if(label == null) {
    // label = I18N.getGUILabel("plotter.unnamed_value_label");
    // }
    String label = "";

    if (dimension == PlotDimension.COLOR) {
        ColorProvider colorProvider = dimensionConfigData.getColorProvider();
        if (!colorProvider.supportsNumericalValues()) {
            throw new RuntimeException(
                    "Color provider for continuous legend item does not support numerical values.");
        }

        // shape dimensions
        final int width = 50;
        final int height = 10;

        // create item paint

        // first disable logarithmic scale on color provider ( -> linear gradient in legend)
        // ContinuousColorProvider continuousColorProvider = null;
        // if (dimensionConfig.isLogarithmic() && colorProvider instanceof
        // ContinuousColorProvider) {
        // continuousColorProvider = (ContinuousColorProvider)colorProvider;
        // continuousColorProvider.setLogarithmic(false);
        // }

        // calculate gradient
        float fractions[] = new float[width];
        Color colors[] = new Color[width];
        for (int i = 0; i < width; ++i) {

            float fraction = i / (width - 1.0f);
            double fractionValue;
            if (colorProvider instanceof ContinuousColorProvider
                    && ((ContinuousColorProvider) colorProvider).isColorMinMaxValueDifferentFromOriginal(
                            ((ContinuousColorProvider) colorProvider).getMinValue(),
                            ((ContinuousColorProvider) colorProvider).getMaxValue())) {
                fractionValue = ((ContinuousColorProvider) colorProvider).getMinValue()
                        + fraction * (((ContinuousColorProvider) colorProvider).getMaxValue()
                                - ((ContinuousColorProvider) colorProvider).getMinValue());
            } else {
                fractionValue = minValue + fraction * (maxValue - minValue);
            }
            colors[i] = colorProvider.getColorForValue(fractionValue);
            fractions[i] = fraction;
        }
        LinearGradientPaint shapeFillPaint = new LinearGradientPaint(new Point(0, 0), new Point(width, 0),
                fractions, colors, CycleMethod.REPEAT);

        // reset color provider to logarithmic if necessary
        // if (continuousColorProvider != null && dimensionConfig.isLogarithmic()) {
        // continuousColorProvider.setLogarithmic(true);
        // }

        // create item shape
        Rectangle itemShape = new Rectangle(width, height);

        if (colorProvider instanceof ContinuousColorProvider) {
            return createFlankedShapeLegendItem(label, ((ContinuousColorProvider) colorProvider).getMinValue(),
                    ((ContinuousColorProvider) colorProvider).getMaxValue(), itemShape, shapeFillPaint, true,
                    dateFormat);
        } else {
            return createFlankedShapeLegendItem(label, minValue, maxValue, itemShape, shapeFillPaint, true,
                    dateFormat);
        }
    } else if (dimension == PlotDimension.SHAPE) {
        // shape provider probably never supports numerical values
        return null;
    } else if (dimension == PlotDimension.SIZE) {
        SizeProvider sizeProvider = dimensionConfigData.getSizeProvider();

        if (!sizeProvider.supportsNumericalValues()) {
            throw new RuntimeException(
                    "Size provider for continuous legend item does not support numerical values.");
        }

        double minScalingFactor = sizeProvider.getMinScalingFactor();
        double maxScalingFactor = sizeProvider.getMaxScalingFactor();
        ContinuousSizeProvider legendSizeProvider = new ContinuousSizeProvider(minScalingFactor,
                maxScalingFactor, MIN_LEGEND_ITEM_SCALING_FACTOR, MAX_LEGEND_ITEM_SCALING_FACTOR, false);

        int legendItemCount = 4;
        Area composedShape = new Area();
        Shape originalShape = UNDEFINED_SHAPE;
        if (dimensionSet.contains(PlotDimension.SIZE) && dimensionSet.size() == 1) {
            originalShape = UNDEFINED_SHAPE_AND_COLOR;
        }
        double maxHeight = originalShape.getBounds().getHeight() * MAX_LEGEND_ITEM_SCALING_FACTOR;
        for (int i = 0; i < legendItemCount; ++i) {
            double fraction = minScalingFactor
                    + ((double) i / legendItemCount * (maxScalingFactor - minScalingFactor));
            double legendScalingFactor = legendSizeProvider.getScalingFactorForValue(fraction);

            double composedWidth = composedShape.getBounds().getWidth();

            AffineTransform t = new AffineTransform();
            t.scale(legendScalingFactor, legendScalingFactor);
            Shape shape = t.createTransformedShape(originalShape);

            t = new AffineTransform();
            double shapeWidth = shape.getBounds().getWidth();
            double shapeHeight = shape.getBounds().getHeight();
            t.translate(composedWidth + shapeWidth * .1, (maxHeight - shapeHeight) / 2.0);
            t.translate(-shape.getBounds().getMinX(), -shape.getBounds().getMinY());
            shape = t.createTransformedShape(shape);
            composedShape.add(new Area(shape));
        }

        return createFlankedShapeLegendItem(label, minValue, maxValue, composedShape, UNDEFINED_COLOR_PAINT,
                false, dateFormat);

    } else {
        throw new RuntimeException("Unsupported dimension. Execution path should never reach this line.");
    }
}

From source file:com.rapidminer.gui.new_plotter.gui.ColorSchemeDialog.java

private void calculateGradientPreview() {

    Color startColor = (Color) gradientStartColorComboBox.getSelectedItem();
    Color endColor = (Color) gradientEndColorComboBox.getSelectedItem();

    if (startColor != null && endColor != null) {

        int width = preview.getWidth() - 1;

        ContinuousColorProvider colorProvider = new ContinuousColorProvider(1, width, startColor, endColor, 255,
                false);//  w w w  .  j a  v  a 2 s.  c  o  m

        // create paint
        float fractions[] = new float[width];
        Color colors[] = new Color[width];

        for (int i = 0; i < width; ++i) {

            float fraction = i / (width - 1.0f);
            double fractionValue = 1 + fraction * (width - 1);
            colors[i] = colorProvider.getColorForValue(fractionValue);
            fractions[i] = fraction;
        }

        Point leftPoint = new Point(0, 0);
        Point rightPoint = new Point(width, 0);

        LinearGradientPaint gradient = new LinearGradientPaint(leftPoint, rightPoint, fractions, colors,
                CycleMethod.REFLECT);
        preview.setGradientPaint(gradient);
        preview.repaint();
    }
}