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:org.jfree.experimental.chart.plot.dial.DialCap.java

/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been 
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted). 
 *//* ww w  . j  a  v  a  2 s.co  m*/
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    g2.setPaint(this.fillPaint);

    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius, this.radius);
    Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f.getWidth(), f.getHeight());
    g2.fill(e);
    g2.setPaint(this.outlinePaint);
    g2.setStroke(this.outlineStroke);
    g2.draw(e);

}

From source file:ShowOff.java

protected void drawBackground(Graphics2D g2) {
    int side = 45;
    int width = getSize().width;
    int height = getSize().height;
    Color[] colors = { Color.yellow, Color.cyan, Color.orange, Color.pink, Color.magenta, Color.lightGray };
    for (int y = 0; y < height; y += side) {
        for (int x = 0; x < width; x += side) {
            Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side);
            int index = (x + y) / side % colors.length;
            g2.setPaint(colors[index]);/*  w  w w. j  ava 2  s. c o m*/
            g2.fill(ellipse);
        }
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int cx = getSize().width / 2;
    int cy = getSize().height / 2;

    g2.translate(cx, cy);//w w  w . ja  v  a  2  s.co  m
    g2.rotate(theta * Math.PI / 180);

    Shape oldClip = g2.getClip();
    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);
    g2.clip(e);

    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);
    g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
    g2.fill(c);

    g2.setPaint(Color.yellow);
    g2.fillOval(cx / 4, 0, cx, cy);

    g2.setClip(oldClip);

    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
    g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
    g2.setComposite(ac);

    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 3 / 4, cy * 3 / 4, 20, 20);
    g2.setStroke(new BasicStroke(4));
    g2.setPaint(Color.magenta);
    g2.fill(r);
    g2.setPaint(Color.green);
    g2.draw(r);

    g2.drawImage(image, -cx / 2, -cy / 2, this);
}

From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java

/**
 * Render the pie chart with the given height
 * /*from w w w.  ja v a 2 s  .  co m*/
 * @param height
 *          The height of the resulting image
 * @return The pie chart rendered as an image
 */
public BufferedImage render(int height) {
    BufferedImage image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.scale(zoom, zoom);
    // fill background to white
    g2.setColor(Color.WHITE);
    g2.fill(new Rectangle(totalWidth, totalHeight));
    // prepare render hints
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    // draw shadow image
    g2.drawImage(pieShadow.getImage(), 0, 0, pieShadow.getImageObserver());

    double start = 0;
    List<Arc2D> pies = new ArrayList<>();
    // pie segmente erzeugen und fuellen
    if (total == 0) {
        g2.setColor(BRIGHT_GRAY);
        g2.fillOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
        g2.setColor(Color.WHITE);
        g2.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
    } else {
        for (Segment s : segments) {
            double portionDegrees = s.getPortion() / total;
            Arc2D pie = paintPieSegment(g2, start, portionDegrees, s.getColor());
            if (withSubSegments) {
                double smallRadius = radius * s.getSubSegmentRatio();
                paintPieSegment(g2, start, portionDegrees, smallRadius, s.getColor().darker());
            }
            start += portionDegrees;
            // portion degree jetzt noch als String (z.B. "17.3%" oder "20%" zusammenbauen)
            String p = String.format(Locale.ENGLISH, "%.1f", Math.rint(portionDegrees * 1000) / 10.0);
            p = removeSuffix(p, ".0"); // evtl. ".0" bei z.B. "25.0" abschneiden (-> "25")
            s.setPercent(p + "%");
            pies.add(pie);
        }
        // weissen Rahmen um die pie segmente zeichen
        g2.setColor(Color.WHITE);
        for (Arc2D pie : pies) {
            g2.draw(pie);
        }
    }
    // Legende zeichnen
    renderLegend(g2);
    // "xx%" Label direkt auf die pie segmente zeichen
    g2.setColor(Color.WHITE);
    float fontSize = 32f;
    g2.setFont(NORMALFONT.deriveFont(fontSize).deriveFont(Font.BOLD));
    start = 0;
    for (Segment s : segments) {
        if (s.getPortion() < 1E-6) {
            continue; // ignore segments with portions that are extremely small
        }
        double portionDegrees = s.getPortion() / total;
        double angle = start + portionDegrees / 2; // genau in der Mitte des Segments
        double xOffsetForCenteredTxt = 8 * s.getPercent().length(); // assume roughly 8px per char
        int x = (int) (centerX + 0.6 * radius * Math.sin(2 * Math.PI * angle) - xOffsetForCenteredTxt);
        int y = (int) (centerY - 0.6 * radius * Math.cos(2 * Math.PI * angle) + fontSize / 2);
        g2.drawString(s.getPercent(), x, y);
        start += portionDegrees;
    }
    return image;
}

From source file:Pear.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Dimension d = getSize();/*from w  ww  . j av a2 s . c om*/
    int w = d.width;
    int h = d.height;
    double ew = w / 2;
    double eh = h / 2;

    g2.setColor(Color.green);

    // Creates the first leaf by filling the intersection of two Area objects
    // created from an ellipse.
    leaf.setFrame(ew - 16, eh - 29, 15.0, 15.0);
    leaf1 = new Area(leaf);
    leaf.setFrame(ew - 14, eh - 47, 30.0, 30.0);
    leaf2 = new Area(leaf);
    leaf1.intersect(leaf2);
    g2.fill(leaf1);

    // Creates the second leaf.
    leaf.setFrame(ew + 1, eh - 29, 15.0, 15.0);
    leaf1 = new Area(leaf);
    leaf2.intersect(leaf1);
    g2.fill(leaf2);

    g2.setColor(Color.black);

    // Creates the stem by filling the Area resulting from the subtraction of
    // two Area objects created from an ellipse.
    stem.setFrame(ew, eh - 42, 40.0, 40.0);
    st1 = new Area(stem);
    stem.setFrame(ew + 3, eh - 47, 50.0, 50.0);
    st2 = new Area(stem);
    st1.subtract(st2);
    g2.fill(st1);

    g2.setColor(Color.yellow);

    // Creates the pear itself by filling the Area resulting from the union of
    // two Area objects created by two different ellipses.
    circle.setFrame(ew - 25, eh, 50.0, 50.0);
    oval.setFrame(ew - 19, eh - 20, 40.0, 70.0);
    circ = new Area(circle);
    ov = new Area(oval);
    circ.add(ov);
    g2.fill(circ);
}

From source file:GradientPane.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
    float width = 300;
    float height = 50;
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false); // Acyclic
                                                                                       // gradient
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2); // Gradient color fill
    g2D.fill(rect1); // Fill the rectangle
    g2D.setPaint(Color.BLACK); // Outline in black
    g2D.draw(rect1); // Fill the rectangle
    g2D.draw(new Line2D.Float(p1, p2));
}

From source file:FilledGeneralPath.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int x = 5;/*from   w  w  w  .  ja  v  a 2s .c  om*/
    int y = 7;

    // fill and stroke GeneralPath
    int xPoints[] = { x, 200, x, 200 };
    int yPoints[] = { y, 200, 200, y };
    GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xPoints.length);
    filledPolygon.moveTo(xPoints[0], yPoints[0]);
    for (int index = 1; index < xPoints.length; index++) {
        filledPolygon.lineTo(xPoints[index], yPoints[index]);
    }
    filledPolygon.closePath();
    g2.setPaint(Color.red);
    g2.fill(filledPolygon);
    g2.setPaint(Color.black);
    g2.draw(filledPolygon);
    g2.drawString("Filled and Stroked GeneralPath", x, 250);
}

From source file:ShapeMover.java

public void update(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Dimension dim = getSize();/*from   www .j  a  va2s.  co m*/
    int w = (int) dim.getWidth();
    int h = (int) dim.getHeight();
    g2.setStroke(new BasicStroke(8.0f));

    if (isFirstTime) {
        area = new Rectangle(dim);
        rect.setLocation(w / 2 - 50, h / 2 - 25);
        isFirstTime = false;
    }

    // Clears the rectangle that was previously drawn.
    g2.setPaint(Color.white);
    g2.fillRect(0, 0, w, h);

    g2.setColor(Color.red);
    g2.draw(rect);
    g2.setColor(Color.black);
    g2.fill(rect);
}

From source file:TextureWithBufferedImage.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Rectangle2D rec1, rec2, rec3, rec4, rec5;
    rec1 = new Rectangle2D.Float(25, 25, 75, 150);
    rec2 = new Rectangle2D.Float(125, 25, 10, 75);
    rec3 = new Rectangle2D.Float(75, 125, 125, 75);
    rec4 = new Rectangle2D.Float(25, 15, 12, 75);
    rec5 = new Rectangle2D.Float(15, 50, 15, 15);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1);
    g2D.setComposite(ac);// www .j ava2 s.  c  om

    g2D.setStroke(new BasicStroke(5.0f));
    g2D.draw(rec1);
    GradientPaint gp = new GradientPaint(125f, 25f, Color.yellow, 225f, 100f, Color.blue);
    g2D.setPaint(gp);
    g2D.fill(rec2);
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.magenta);
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.black);
    big.drawLine(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    TexturePaint tp = new TexturePaint(bi, r);

    g2D.setPaint(tp);
    g2D.fill(rec3);
    g2D.setColor(Color.green);
    g2D.fill(rec4);
    g2D.setColor(Color.red);
    g2D.fill(rec5);
}

From source file:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

private void drawItemShape(Graphics2D g2, int row, int column, Shape shape) {
    if (getItemShapeFilled(row, column)) {
        g2.setPaint(getUseFillPaint() ? getItemFillPaint(row, column) : getItemPaint(row, column));

        g2.fill(shape);
    }/*  www.j a v  a2 s .  co m*/

    if (getDrawOutlines()) {
        g2.setPaint(getUseOutlinePaint() ? getItemOutlinePaint(row, column) : getItemPaint(row, column));

        g2.setStroke(getItemOutlineStroke(row, column));

        g2.draw(shape);
    }
}