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:TransformDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect = new Rectangle(5, 5, 200, 200);

    int w = getSize().width;
    int h = getSize().height;

    AffineTransform saveXform = g2.getTransform();
    AffineTransform toCenterAt = new AffineTransform();
    toCenterAt.translate(w / 2 - (rect.width / 2), h / 2 - (rect.height / 2));
    g2.transform(toCenterAt);/*from  ww w  .  j  a  v a 2s.c  o  m*/

    g2.fill(rect);

    g2.transform(saveXform);
}

From source file:XMLWriteTest.java

public void paintComponent(Graphics g) {
    if (rects.size() == 0)
        newDrawing();/*from w  w  w.ja v a2s .c  om*/
    Graphics2D g2 = (Graphics2D) g;

    // draw all rectangles
    for (int i = 0; i < rects.size(); i++) {
        g2.setPaint(colors.get(i));
        g2.fill(rects.get(i));
    }
}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

private void drawArrow(Graphics2D g, double speed) {
    int startX = -(arrowWidth / 2);
    int startY = -(arrowHeight / 2);
    // draw main arrow line
    g.fill(new Rectangle2D.Double(startX, startY, arrowWidth, arrowHeight));

    // drawing speed feathers (representing 25 ms each)
    int featherOffset = 0;
    int featherHeight = arrowHeight / featherPadding;
    int flagFeathers = (int) speed / 25;
    int[] flagFeatherX = { 1, -featherWidth, 1 };
    for (int i = 0; i < flagFeathers; i++) {
        if (i > 0)
            featherOffset += 1; // more space if multiple flags
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        int flagSize = (int) (featherHeight * 1.5f);
        int[] flagFeatherY = { yValue, (yValue - flagSize / 2), yValue - flagSize };
        g.fill(new Polygon(flagFeatherX, flagFeatherY, 3));
        featherOffset += 1;/*www  .  j  ava 2  s.c  om*/
    }
    // Add space between flag-feather and next
    if (flagFeathers > 0)
        featherOffset++;
    double remainingSpeed = speed - (flagFeathers * 25);

    // draw full feathers (representing 5 ms each)
    int fullFeathers = (int) remainingSpeed / 5;
    g.setStroke(new BasicStroke(arrowWidth));
    for (int i = 0; i < fullFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, -featherWidth };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }
    remainingSpeed = remainingSpeed - (fullFeathers * 5);

    // draw half feathers (representing 2.5 ms each)
    int halfFeathers = (int) (remainingSpeed / 2.5);
    // never draw half-feathers at the start
    if (featherOffset == 0)
        featherOffset = 1;
    for (int i = 0; i < halfFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, (-featherWidth / 2) };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }

    g.setStroke(new BasicStroke(1));
}

From source file:AreaIntersect.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    double halfWdith = getSize().width / 2;
    double halfHeight = getSize().height / 2;

    g2.setColor(Color.green);//from  w w w . ja  v a2  s.  c  o m

    leaf.setFrame(halfWdith - 16, halfHeight - 29, 15.0, 15.0);
    leaf1 = new Area(leaf);
    leaf.setFrame(halfWdith - 14, halfHeight - 47, 30.0, 30.0);
    leaf2 = new Area(leaf);
    leaf1.intersect(leaf2);
    g2.fill(leaf1);

}

From source file:MainClass.java

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

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);//from  www  .j av a 2  s .c o m
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));

}

From source file:ColorFadingAnimation.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(50, 50, 50));
    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(rh);//from  w  w w. j ava2 s . c o m
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha_rectangle));
    g2d.fill(rect);
}

From source file:MainClass.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    // pageIndex 0 corresponds to page number 1.
    if (pageIndex >= 1)
        return Printable.NO_SUCH_PAGE;

    PrinterGraphics p = (PrinterGraphics) g;

    System.out.println(p.getPrinterJob().getCopies());
    System.out.println(p.getPrinterJob().getJobName());

    Graphics2D g2 = (Graphics2D) g;

    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();

    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();

    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);

    g2.setColor(Color.red);//from  www .  j  a  v a  2  s. co  m
    g2.draw(r);

    Shape s = new Ellipse2D.Double(xo + 4, yo + 4, 32, 32);

    g2.fill(s);

    return Printable.PAGE_EXISTS;
}

From source file:net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder.java

public JasperConcatenatedReportBuilder toPng(OutputStream outputStream, float zoom) throws DRException {
    Validate.notNull(outputStream, "outputStream must not be null");
    Validate.isTrue(zoom > 0, "zoom must be > 0");

    int maxWidth = 0;
    int maxHeight = 0;

    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pages = jasperPrint.getPages().size();
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        maxWidth += pageWidth * pages + (pages - 1) + 2;
        int height = (int) (jasperPrint.getPageHeight() * zoom) + 2;
        if (height > maxHeight) {
            maxHeight = height;/*  w ww  .  java2  s  .  c  om*/
        }
    }

    Image pageImage = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) pageImage.getGraphics();
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.fill(new Rectangle2D.Float(1, 1, maxWidth - 1, maxHeight - 1));

    int offset = 1;
    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        for (int i = 0; i < jasperPrint.getPages().size(); i++) {
            try {
                JRGraphics2DExporter exporter = new JRGraphics2DExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics());
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_X, offset);
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_Y, 1);
                exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(i));
                exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
                exporter.exportReport();
                offset += pageWidth + 1;
            } catch (JRException e) {
                throw new DRException(e);
            }
        }
    }
    try {
        ImageIO.write((RenderedImage) pageImage, "png", outputStream);
    } catch (IOException e) {
        throw new DRException(e);
    }
    return this;
}

From source file:Main.java

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

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);/* w  w w.  ja  v a  2 s.  co m*/
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);

    p1.setLocation(150, 200);
    p2.setLocation(250, 200);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false);
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2);
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Acyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);
}

From source file:MainClass.java

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

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);/*from w w w .  jav  a 2s.c  o m*/
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);

    p1.setLocation(150, 200);
    p2.setLocation(250, 200);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false);
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2);
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Acyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);
}