Example usage for java.awt Graphics2D fill

List of usage examples for java.awt Graphics2D fill

Introduction

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

Prototype

public abstract void fill(Shape s);

Source Link

Document

Fills the interior of a Shape using the settings of the Graphics2D context.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int w, h;/*from www .j a  va 2 s .c  om*/
    w = 150;
    h = 150;
    Ellipse2D.Double circle = new Ellipse2D.Double(12, 12, 12, 12);

    Document document = new Document(new Rectangle(w, h));

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("sun_tutorial.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Graphics2D g2 = cb.createGraphics(w, h);

    g2.setColor(Color.green);

    g2.fill(circle);

    g2.dispose();

    document.close();
}

From source file:org.jfree.graphics2d.demo.BufferedImageDemo.java

/**
 * Starting point for the demo.//from w  ww . jav  a  2  s  .c  o m
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    ImageIcon icon = new ImageIcon(BufferedImageDemo.class.getResource("jfree_chart_1.jpg"));
    g2.rotate(Math.PI / 12);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(Color.WHITE);
    g2.fill(new Rectangle(0, 0, 600, 400));
    g2.setPaint(Color.RED);
    g2.draw(new Rectangle(0, 0, 600, 400));
    g2.drawImage(icon.getImage(), 10, 20, null);
    ImageIO.write(image, "png", new File("image-test.png"));
}

From source file:G2DCircleIntersectPDF.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*w  ww.j a  v  a2 s . c  o  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("G2DCircleIntersectPDF.pdf"));
        document.open();
        DefaultFontMapper mapper = new DefaultFontMapper();
        FontFactory.registerDirectories();
        mapper.insertDirectory("c:\\windows\\fonts");

        int w = 150;
        int h = 150;
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2 = tp.createGraphics(w, h, mapper);
        tp.setWidth(w);
        tp.setHeight(h);
        double ew = w / 2;
        double eh = h / 2;
        Ellipse2D.Double circle, circle1;

        circle = new Ellipse2D.Double(ew - 16, eh - 29, 50.0, 50.0);

        g2.setColor(Color.green);
        g2.fill(circle);

        g2.setColor(Color.red);
        circle1 = new Ellipse2D.Double(ew, eh, 50.0, 50.0);
        g2.fill(circle1);

        Area area1 = new Area(circle);
        Area area2 = new Area(circle1);

        g2.setColor(Color.BLUE);
        area1.intersect(area2);
        g2.fill(area1);

        g2.dispose();
        cb.addTemplate(tp, 50, 400);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawOldLinearGradientPaintTest(Graphics2D g2) {
    // top left//  w  w  w .j  av  a 2 s .  co  m
    GradientPaint lgp = new GradientPaint(10, 30, Color.RED, 50, 30, Color.BLUE);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 10, 40, 40));

    // top right
    lgp = new GradientPaint(80, 10, Color.RED, 80, 50, Color.BLUE);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 10, 40, 40));

    // bottom left
    lgp = new GradientPaint(10, 100, Color.RED, 50, 60, Color.BLUE);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 60, 40, 40));

    // bottom right
    lgp = new GradientPaint(70, 70, Color.RED, 90, 90, Color.BLUE);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 60, 40, 40));

}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawLinearGradientPaintTest(Graphics2D g2) {
    // top left//from w  w  w  .  j  ava  2  s.com
    LinearGradientPaint lgp = new LinearGradientPaint(10, 30, 50, 30, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 10, 40, 40));

    // top right
    lgp = new LinearGradientPaint(80, 10, 80, 50, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 10, 40, 40));

    // bottom left
    lgp = new LinearGradientPaint(10, 100, 50, 60, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 60, 40, 40));

    // bottom right
    lgp = new LinearGradientPaint(70, 70, 90, 90, new float[] { 0.0f, 0.5f, 1.0f },
            new Color[] { Color.RED, Color.YELLOW, Color.BLUE }, CycleMethod.REPEAT);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 60, 40, 40));

}

From source file:Utils.java

public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f, new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL ? width : 0.0f,
            orientation == SwingConstants.VERTICAL ? height : 0.0f, new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);/*from   www . j  av  a  2  s.  co m*/
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();
    gradient.flush();

    return gradient;
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawRadialGradientPaintTest(Graphics2D g2) {
    RadialGradientPaint rgp = new RadialGradientPaint(50, 50, 40, 30, 30, new float[] { 0f, 0.75f, 1f },
            new Color[] { Color.RED, Color.GREEN, Color.BLUE }, MultipleGradientPaint.CycleMethod.NO_CYCLE);

    g2.setPaint(rgp);/* www .j a v  a 2  s .  c  om*/
    Ellipse2D circle = new Ellipse2D.Double(10, 10, 80, 80);
    g2.fill(circle);
}

From source file:Main.java

/**
 * Fills a gradient using the start color and end color specified.
 *
 * @param g//from ww  w  . j a  v  a2 s. c  o  m
 *          the gradient
 * @param s
 *          the shape to fill
 * @param start
 *          the start color
 * @param end
 *          the end color
 * @param vertical
 *          true for a vertical gradient; false for horizontal
 */
public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) {
    Rectangle r = s.getBounds();
    Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end);
    Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setPaint(gp);
    g.fill(s);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o);
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

private static void drawMask(Graphics2D g2, Shape mask) {
    g2.fill(mask);
}

From source file:Main.java

/**
 * Draws a single arrow head//  w  w w. ja  v a 2s .  co  m
 * 
 * @param aG
 *          the canvas to draw on;
 * @param aXpos
 *          the X position of the arrow head;
 * @param aYpos
 *          the (center) Y position of the arrow head;
 * @param aFactor
 *          +1 to have a left-facing arrow head, -1 to have a right-facing
 *          arrow head;
 * @param aArrowWidth
 *          the total width of the arrow head;
 * @param aArrowHeight
 *          the total height of the arrow head.
 */
public static final void drawArrowHead(final Graphics2D aG, final int aXpos, final int aYpos, final int aFactor,
        final int aArrowWidth, final int aArrowHeight) {
    final double halfHeight = aArrowHeight / 2.0;
    final int x1 = aXpos + (aFactor * aArrowWidth);
    final int y1 = (int) Math.ceil(aYpos - halfHeight);
    final int y2 = (int) Math.floor(aYpos + halfHeight);

    final Polygon arrowHead = new Polygon();
    arrowHead.addPoint(aXpos, aYpos);
    arrowHead.addPoint(x1, y1);
    arrowHead.addPoint(x1, y2);

    aG.fill(arrowHead);
}