Example usage for java.awt GradientPaint getColor1

List of usage examples for java.awt GradientPaint getColor1

Introduction

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

Prototype

public Color getColor1() 

Source Link

Document

Returns the color C1 anchored by the point P1.

Usage

From source file:Main.java

/**
 * Serialises a <code>Paint</code> object.
 *
 * @param paint  the paint object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*from  w w  w.j  a  v  a 2 s .c  om*/
public static void writePaint(final Paint paint, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (paint != null) {
        stream.writeBoolean(false);
        stream.writeObject(paint.getClass());
        if (paint instanceof Serializable) {
            stream.writeObject(paint);
        } else if (paint instanceof GradientPaint) {
            final GradientPaint gp = (GradientPaint) paint;
            stream.writeFloat((float) gp.getPoint1().getX());
            stream.writeFloat((float) gp.getPoint1().getY());
            stream.writeObject(gp.getColor1());
            stream.writeFloat((float) gp.getPoint2().getX());
            stream.writeFloat((float) gp.getPoint2().getY());
            stream.writeObject(gp.getColor2());
            stream.writeBoolean(gp.isCyclic());
        }
    } else {
        stream.writeBoolean(true);
    }

}

From source file:PaintUtils.java

/** Resizes a gradient to fill the width and height available. If the
 * gradient is left to right it will be resized to fill the entire width.
 * If the gradient is top to bottom it will be resized to fill the entire
 * height. If the gradient is on an angle it will be resized to go from
 * one corner to the other of the rectangle formed by (0,0 -> width,height).
 *
 * This method can resize java.awt.GradientPaint, java.awt.LinearGradientPaint,
 * and the LinearGradientPaint implementation from Apache's Batik project. Note,
 * this method does not require the MultipleGradientPaint.jar from Apache to
 * compile or to run. MultipleGradientPaint.jar *is* required if you want
 * to resize the LinearGradientPaint from that jar.
 *
 * Any paint passed into this method which is not a kind of gradient paint (like
 * a Color or TexturePaint) will be returned unmodified. It will not throw
 * an exception. If the gradient cannot be resized due to other errors the
 * original paint will be returned unmodified. It will not throw an
 * exception.//from  w  w  w  . ja v a  2 s.  c  o  m
 *
 */
public static Paint resizeGradient(Paint p, int width, int height) {
    if (p == null)
        return p;

    if (p instanceof GradientPaint) {
        GradientPaint gp = (GradientPaint) p;
        Point2D[] pts = new Point2D[2];
        pts[0] = gp.getPoint1();
        pts[1] = gp.getPoint2();
        pts = adjustPoints(pts, width, height);
        return new GradientPaint(pts[0], gp.getColor1(), pts[1], gp.getColor2(), gp.isCyclic());
    }

    if ("java.awt.LinearGradientPaint".equals(p.getClass().getName())
            || "org.apache.batik.ext.awt.LinearGradientPaint".equals(p.getClass().getName())) {
        return resizeLinearGradient(p, width, height);
    }
    return p;
}

From source file:Main.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    GradientPaint gp1 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);

    System.out.println(gp1.getColor1());
    g2d.setPaint(gp1);/*from   w w w .j a  v a2 s .  com*/
    g2d.fillRect(20, 20, 300, 40);

}

From source file:com.newatlanta.bluedragon.CustomBarRenderer.java

/**
 * Returns a legend item for a series.// www  . j av a2s  .  c o m
 * 
 * @param datasetIndex
 *          the dataset index (zero-based).
 * @param series
 *          the series index (zero-based).
 * 
 * @return The legend item.
 */
public LegendItem getLegendItem(int datasetIndex, int series) {

    CategoryPlot cp = getPlot();
    if (cp == null) {
        return null;
    }

    CategoryDataset dataset;
    dataset = cp.getDataset(datasetIndex);
    String label = getLegendItemLabelGenerator().generateLabel(dataset, series);
    String description = label;
    String toolTipText = null;
    if (getLegendItemToolTipGenerator() != null) {
        toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
    }
    String urlText = null;
    if (getLegendItemURLGenerator() != null) {
        urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
    }
    Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0);
    Paint paint = getSeriesPaint(series);
    Paint outlinePaint = getSeriesOutlinePaint(series);
    Stroke outlineStroke = getSeriesOutlineStroke(series);

    // This is the fix for bug #2695
    if (paint instanceof java.awt.GradientPaint) {
        // When the paintstyle is "shade" use the lighter
        // color while with "light" use the darker color
        // NOTE: if we take the lighter color (Color2) and make it darker
        // and it equals the darker color (Color1) then the paintstyle
        // is "shade".
        GradientPaint gp = ((GradientPaint) paint);
        if (cfCHART.getDarkerColor(gp.getColor2()).equals(gp.getColor1()))
            paint = gp.getColor2(); // the lighter color
        else
            paint = gp.getColor1(); // the darker color
    }

    return new LegendItem(label, description, toolTipText, urlText, true, shape, true, paint,
            isDrawBarOutline(), outlinePaint, outlineStroke, false, new Line2D.Float(), new BasicStroke(1.0f),
            Color.black);
}

From source file:it.cnr.istc.utils.gui.ReverseGradientXYBarPainter.java

/**
 * Paints a single bar instance./*  ww w .java2s .co m*/
 *
 * @param g2 the graphics target.
 * @param renderer the renderer.
 * @param row the row index.
 * @param column the column index.
 * @param bar the bar
 * @param base indicates which side of the rectangle is the base of the bar.
 */
@Override
public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar,
        RectangleEdge base) {

    Paint itemPaint = renderer.getItemPaint(row, column);

    Color c0, c1;
    if (itemPaint instanceof Color) {
        c0 = (Color) itemPaint;
        c1 = c0.brighter();
    } else if (itemPaint instanceof GradientPaint) {
        GradientPaint gp = (GradientPaint) itemPaint;
        c0 = gp.getColor1();
        c1 = gp.getColor2();
    } else {
        c0 = Color.blue;
        c1 = Color.blue.brighter();
    }

    // as a special case, if the bar colour has alpha == 0, we draw
    // nothing.
    if (c0.getAlpha() == 0) {
        return;
    }

    if (base == RectangleEdge.LEFT || base == RectangleEdge.RIGHT) {
        Rectangle2D[] regions = splitVerticalBar(bar, this.g1, this.g2, this.g3);
        GradientPaint gp = new GradientPaint((float) regions[0].getMinX(), 0.0f, c0,
                (float) regions[0].getMaxX(), 0.0f, Color.white);
        g2.setPaint(gp);
        g2.fill(regions[0]);

        gp = new GradientPaint((float) regions[1].getMinX(), 0.0f, Color.white, (float) regions[1].getMaxX(),
                0.0f, c0);
        g2.setPaint(gp);
        g2.fill(regions[1]);

        gp = new GradientPaint((float) regions[2].getMinX(), 0.0f, c0, (float) regions[2].getMaxX(), 0.0f, c1);
        g2.setPaint(gp);
        g2.fill(regions[2]);

        gp = new GradientPaint((float) regions[3].getMinX(), 0.0f, c1, (float) regions[3].getMaxX(), 0.0f, c0);
        g2.setPaint(gp);
        g2.fill(regions[3]);
    } else if (base == RectangleEdge.TOP || base == RectangleEdge.BOTTOM) {
        Rectangle2D[] regions = splitHorizontalBar(bar, this.g1, this.g2, this.g3);
        GradientPaint gp = new GradientPaint(0.0f, (float) regions[0].getMinY(), c0, 0.0f,
                (float) regions[0].getMaxX(), Color.white);
        g2.setPaint(gp);
        g2.fill(regions[0]);

        gp = new GradientPaint(0.0f, (float) regions[1].getMinY(), Color.white, 0.0f,
                (float) regions[1].getMaxY(), c0);
        g2.setPaint(gp);
        g2.fill(regions[1]);

        gp = new GradientPaint(0.0f, (float) regions[2].getMinY(), c0, 0.0f, (float) regions[2].getMaxY(), c1);
        g2.setPaint(gp);
        g2.fill(regions[2]);

        gp = new GradientPaint(0.0f, (float) regions[3].getMinY(), c1, 0.0f, (float) regions[3].getMaxY(), c0);
        g2.setPaint(gp);
        g2.fill(regions[3]);
    }

    // draw the outline...
    if (renderer.isDrawBarOutline()) {
        Stroke stroke = renderer.getItemOutlineStroke(row, column);
        Paint paint = renderer.getItemOutlinePaint(row, column);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(bar);
        }
    }

}

From source file:com.newatlanta.bluedragon.CustomClusteredXYBarRenderer.java

/**
 * Returns a default legend item for the specified series. Subclasses should
 * override this method to generate customised items.
 * /*w  ww .  j a v a  2 s  .  c om*/
 * @param datasetIndex
 *          the dataset index (zero-based).
 * @param series
 *          the series index (zero-based).
 * 
 * @return A legend item for the series.
 */
public LegendItem getLegendItem(int datasetIndex, int series) {
    LegendItem result = null;
    XYPlot xyplot = getPlot();
    if (xyplot != null) {
        XYDataset dataset = xyplot.getDataset(datasetIndex);
        if (dataset != null) {
            XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
            String label = lg.generateLabel(dataset, series);
            String description = label;
            String toolTipText = null;
            if (getLegendItemToolTipGenerator() != null) {
                toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
            }
            String urlText = null;
            if (getLegendItemURLGenerator() != null) {
                urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
            }
            Shape shape = getLegendBar();
            Paint paint = getSeriesPaint(series);
            Paint outlinePaint = getSeriesOutlinePaint(series);
            Stroke outlineStroke = getSeriesOutlineStroke(series);

            // This is the fix for bug #2695
            if (paint instanceof java.awt.GradientPaint) {
                // When the paintstyle is "shade" use the lighter
                // color while with "light" use the darker color
                // NOTE: if we take the lighter color (Color2) and make it darker
                // and it equals the darker color (Color1) then the paintstyle
                // is "shade".
                GradientPaint gp = ((GradientPaint) paint);
                if (cfCHART.getDarkerColor(gp.getColor2()).equals(gp.getColor1()))
                    paint = gp.getColor2(); // the lighter color
                else
                    paint = gp.getColor1(); // the darker color
            }

            result = new LegendItem(label, description, toolTipText, urlText, shape, paint, outlineStroke,
                    outlinePaint);
        }
    }
    return result;
}

From source file:org.jfree.eastwood.GXYPlot.java

/**
 * Draws the background for the plot.//  w w w.  j ava 2 s . co  m
 *
 * @param g2  the graphics device.
 * @param area  the area.
 */
public void drawBackground(Graphics2D g2, Rectangle2D area) {
    Paint p = getBackgroundPaint();
    if (p instanceof GradientPaint) {
        // do the transformation directly
        GradientPaint gp = (GradientPaint) p;
        double r = (this.f1 - this.f0) * area.getWidth();
        Point2D p0 = new Point2D.Double(area.getMinX() + this.f0 * area.getWidth(), area.getMaxY());
        Point2D p1 = new Point2D.Double(p0.getX() + r * Math.cos(this.angle),
                p0.getY() - r * Math.sin(this.angle));
        p = new GradientPaint(p0, gp.getColor1(), p1, gp.getColor2());
    }
    g2.setPaint(p);
    g2.fill(area);
}

From source file:net.sf.fspdfs.chartthemes.spring.EyeCandySixtiesChartTheme.java

/**
 *
 *//* ww  w  .ja v  a2s  .  c  o m*/
protected void configureChart(JFreeChart jfreeChart, JRChartPlot jrPlot) throws JRException {
    super.configureChart(jfreeChart, jrPlot);

    TextTitle title = jfreeChart.getTitle();
    if (title != null) {
        RectangleInsets padding = title.getPadding();
        double bottomPadding = Math.max(padding.getBottom(), 15d);
        title.setPadding(padding.getTop(), padding.getLeft(), bottomPadding, padding.getRight());
    }

    GradientPaint gp = (GradientPaint) getDefaultValue(defaultChartPropertiesMap,
            ChartThemesConstants.BACKGROUND_PAINT);

    jfreeChart.setBackgroundPaint(new GradientPaint(0f, 0f, gp.getColor1(), 0f, getChart().getHeight() * 0.7f,
            gp.getColor2(), false));
}

From source file:net.sf.jasperreports.chartthemes.spring.EyeCandySixtiesChartTheme.java

@Override
protected void configureChart(JFreeChart jfreeChart, JRChartPlot jrPlot) throws JRException {
    super.configureChart(jfreeChart, jrPlot);

    TextTitle title = jfreeChart.getTitle();
    if (title != null) {
        RectangleInsets padding = title.getPadding();
        double bottomPadding = Math.max(padding.getBottom(), 15d);
        title.setPadding(padding.getTop(), padding.getLeft(), bottomPadding, padding.getRight());
    }//from  w  w  w.  jav a  2 s. c om

    GradientPaint gp = (GradientPaint) getDefaultValue(defaultChartPropertiesMap,
            ChartThemesConstants.BACKGROUND_PAINT);

    jfreeChart.setBackgroundPaint(new GradientPaint(0f, 0f, gp.getColor1(), 0f, getChart().getHeight() * 0.7f,
            gp.getColor2(), false));
}

From source file:SWTGraphics2D.java

/**
 * Sets the paint for this graphics context.  For now, this graphics
 * context only supports instances of {@link Color} or
 * {@link GradientPaint} (in the latter case there is no real gradient
 * support, the paint used is the <code>Color</code> returned by
 * <code>getColor1()</code>).
 *
 * @param paint  the paint (<code>null</code> not permitted).
 *
 * @see #getPaint()/*  ww  w.  j a  va  2s  .c  o  m*/
 * @see #setColor(Color)
 */
public void setPaint(Paint paint) {
    if (paint instanceof Color) {
        setColor((Color) paint);
    } else if (paint instanceof GradientPaint) {
        GradientPaint gp = (GradientPaint) paint;
        setColor(gp.getColor1());
    } else {
        throw new RuntimeException("Can only handle 'Color' at present.");
    }
}