Example usage for java.awt GradientPaint getPoint1

List of usage examples for java.awt GradientPaint getPoint1

Introduction

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

Prototype

public Point2D getPoint1() 

Source Link

Document

Returns a copy of the point P1 that anchors the first color.

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.
 *//* ww w  . j a va  2  s .c  o m*/
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.// ww  w  . j  av a 2 s.  co  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.getPoint1());
    g2d.setPaint(gp1);//from  w w  w. j  a va2 s.  c  o m
    g2d.fillRect(20, 20, 300, 40);

}

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

public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // check the value we are plotting...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;//  w w  w. j a  v a2s  .co m
    }

    double value = dataValue.doubleValue();

    Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(), dataArea.getY() + getYOffset(),
            dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset());

    PlotOrientation orientation = plot.getOrientation();

    double barW0 = calculateBarW0(plot, orientation, adjusted, domainAxis, state, row, column);
    double[] barL0L1 = calculateBarL0L1(value);
    if (barL0L1 == null) {
        return; // the bar is not visible
    }

    RectangleEdge edge = plot.getRangeAxisEdge();
    double transL0 = rangeAxis.valueToJava2D(barL0L1[0], adjusted, edge);
    double transL1 = rangeAxis.valueToJava2D(barL0L1[1], adjusted, edge);
    double barL0 = Math.min(transL0, transL1);
    double barLength = Math.abs(transL1 - transL0);

    // draw the bar...
    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
    }
    Paint itemPaint = getItemPaint(row, column);
    if (itemPaint instanceof GradientPaint) {
        itemPaint = getGradientPaintTransformer().transform((GradientPaint) itemPaint, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);

    double x0 = bar.getMinX();
    double x1 = x0 + getXOffset();
    double x2 = bar.getMaxX();
    double x3 = x2 + getXOffset();

    double y0 = bar.getMinY() - getYOffset();
    double y1 = bar.getMinY();
    double y2 = bar.getMaxY() - getYOffset();
    double y3 = bar.getMaxY();

    GeneralPath bar3dRight = null;
    GeneralPath bar3dTop = null;
    if (barLength > 0.0) {
        bar3dRight = new GeneralPath();
        bar3dRight.moveTo((float) x2, (float) y3);
        bar3dRight.lineTo((float) x2, (float) y1);
        bar3dRight.lineTo((float) x3, (float) y0);
        bar3dRight.lineTo((float) x3, (float) y2);
        bar3dRight.closePath();

        if (itemPaint instanceof Color) {
            g2.setPaint(((Color) itemPaint).darker());
        } else if (itemPaint instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) itemPaint;
            g2.setPaint(new StandardGradientPaintTransformer().transform(new GradientPaint(gp.getPoint1(),
                    gp.getColor1().darker(), gp.getPoint2(), gp.getColor2().darker(), gp.isCyclic()),
                    bar3dRight));
        }
        g2.fill(bar3dRight);
    }

    bar3dTop = new GeneralPath();
    bar3dTop.moveTo((float) x0, (float) y1);
    bar3dTop.lineTo((float) x1, (float) y0);
    bar3dTop.lineTo((float) x3, (float) y0);
    bar3dTop.lineTo((float) x2, (float) y1);
    bar3dTop.closePath();
    g2.fill(bar3dTop);

    if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
        g2.setStroke(getItemOutlineStroke(row, column));
        g2.setPaint(getItemOutlinePaint(row, column));
        g2.draw(bar);
        if (bar3dRight != null) {
            g2.draw(bar3dRight);
        }
        if (bar3dTop != null) {
            g2.draw(bar3dTop);
        }
    }

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
    }

    // add an item entity, if this information is being collected
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        GeneralPath barOutline = new GeneralPath();
        barOutline.moveTo((float) x0, (float) y3);
        barOutline.lineTo((float) x0, (float) y1);
        barOutline.lineTo((float) x1, (float) y0);
        barOutline.lineTo((float) x3, (float) y0);
        barOutline.lineTo((float) x3, (float) y2);
        barOutline.lineTo((float) x2, (float) y3);
        barOutline.closePath();
        addItemEntity(entities, dataset, row, column, barOutline);
    }
}

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

@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // check the value we are plotting...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;/*from   w  ww  . j ava  2  s.c  om*/
    }

    double value = dataValue.doubleValue();

    Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(), dataArea.getY() + getYOffset(),
            dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset());

    PlotOrientation orientation = plot.getOrientation();

    double barW0 = calculateBarW0(plot, orientation, adjusted, domainAxis, state, row, column);
    double[] barL0L1 = calculateBarL0L1(value);
    if (barL0L1 == null) {
        return; // the bar is not visible
    }

    RectangleEdge edge = plot.getRangeAxisEdge();
    double transL0 = rangeAxis.valueToJava2D(barL0L1[0], adjusted, edge);
    double transL1 = rangeAxis.valueToJava2D(barL0L1[1], adjusted, edge);
    double barL0 = Math.min(transL0, transL1);
    double barLength = Math.abs(transL1 - transL0);

    // draw the bar...
    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
    }
    Paint itemPaint = getItemPaint(row, column);
    if (itemPaint instanceof GradientPaint) {
        itemPaint = getGradientPaintTransformer().transform((GradientPaint) itemPaint, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);

    double x0 = bar.getMinX();
    double x1 = x0 + getXOffset();
    double x2 = bar.getMaxX();
    double x3 = x2 + getXOffset();

    double y0 = bar.getMinY() - getYOffset();
    double y1 = bar.getMinY();
    double y2 = bar.getMaxY() - getYOffset();
    double y3 = bar.getMaxY();

    GeneralPath bar3dRight = null;
    GeneralPath bar3dTop = null;
    if (barLength > 0.0) {
        bar3dRight = new GeneralPath();
        bar3dRight.moveTo((float) x2, (float) y3);
        bar3dRight.lineTo((float) x2, (float) y1);
        bar3dRight.lineTo((float) x3, (float) y0);
        bar3dRight.lineTo((float) x3, (float) y2);
        bar3dRight.closePath();

        if (itemPaint instanceof Color) {
            g2.setPaint(((Color) itemPaint).darker());
        } else if (itemPaint instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) itemPaint;
            g2.setPaint(new StandardGradientPaintTransformer().transform(new GradientPaint(gp.getPoint1(),
                    gp.getColor1().darker(), gp.getPoint2(), gp.getColor2().darker(), gp.isCyclic()),
                    bar3dRight));
        }
        g2.fill(bar3dRight);
    }

    bar3dTop = new GeneralPath();
    bar3dTop.moveTo((float) x0, (float) y1);
    bar3dTop.lineTo((float) x1, (float) y0);
    bar3dTop.lineTo((float) x3, (float) y0);
    bar3dTop.lineTo((float) x2, (float) y1);
    bar3dTop.closePath();
    g2.fill(bar3dTop);

    if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
        g2.setStroke(getItemOutlineStroke(row, column));
        g2.setPaint(getItemOutlinePaint(row, column));
        g2.draw(bar);
        if (bar3dRight != null) {
            g2.draw(bar3dRight);
        }
        if (bar3dTop != null) {
            g2.draw(bar3dTop);
        }
    }

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
    }

    // add an item entity, if this information is being collected
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        GeneralPath barOutline = new GeneralPath();
        barOutline.moveTo((float) x0, (float) y3);
        barOutline.lineTo((float) x0, (float) y1);
        barOutline.lineTo((float) x1, (float) y0);
        barOutline.lineTo((float) x3, (float) y0);
        barOutline.lineTo((float) x3, (float) y2);
        barOutline.lineTo((float) x2, (float) y3);
        barOutline.closePath();
        addItemEntity(entities, dataset, row, column, barOutline);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) {
    if (paint instanceof Color) {
        final Color color = (Color) paint;
        final int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setFillOpacity(alpha / 255.00f);
                    fillGState[alpha] = gs;
                }/*from   ww  w  . jav a 2  s . co  m*/
                cb.setGState(gs);
            }
            cb.setColorFill(color);
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setStrokeOpacity(alpha / 255.0f);
                    strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
            }
            cb.setColorStroke(color);
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2);
        final PdfShadingPattern pat = new PdfShadingPattern(shading);
        if (fill) {
            cb.setShadingFill(pat);
        } else {
            cb.setShadingStroke(pat);
        }
    } else if (paint instanceof TexturePaint) {
        try {
            final TexturePaint tp = (TexturePaint) paint;
            final BufferedImage img = tp.getImage();
            final Rectangle2D rect = tp.getAnchorRect();
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
            final AffineTransform inverse = this.normalizeMatrix();
            inverse.translate(rect.getX(), rect.getY());
            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
            final double[] mx = new double[6];
            inverse.getMatrix(mx);
            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                    (float) mx[5]);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }

        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    } else {
        try {
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            final BufferedImage img = new BufferedImage((int) width, (int) height, type);
            final Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            final AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            if (invert) {
                final AffineTransform tx = new AffineTransform();
                tx.scale(1, -1);
                tx.translate(-xoffset, -yoffset);
                g.drawImage(img, tx, null);
            }
            g.dispose();
            // g = null;
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(width, height);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }
        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    }
}