Example usage for java.awt Graphics2D setClip

List of usage examples for java.awt Graphics2D setClip

Introduction

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

Prototype

public abstract void setClip(Shape clip);

Source Link

Document

Sets the current clipping area to an arbitrary clip shape.

Usage

From source file:JTable2Pdf.java

private void print() {
    Document document = new Document(PageSize.A4.rotate());
    try {// w  w  w . j  a  v a 2s  .com
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        Graphics2D g2 = cb.createGraphicsShapes(500, 500);

        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);

        table.print(g2);
        g2.setClip(oldClip);

        g2.dispose();
        cb.restoreState();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:ClipArea.java

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

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

    if (clip) {// ww w. jav  a 2s. c  o m
        Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
        g2.setClip(e);

        g2.setColor(Color.yellow);
        g2.fillRect(0, 0, w, h);
    }

    if (clipFurther) {
        Rectangle r = new Rectangle(w / 2, h / 2, w / 2, h / 2);
        g2.clip(r);

        g2.setColor(Color.green);
        g2.fillRect(0, 0, w, h);
    }
}

From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

@Override
public BufferedImage makeIcon(final int width, final int height, final Path2D shape, final boolean allowAlpha) {
    try {/*ww  w.j  a va 2s  .co m*/
        final BufferedImage img = this.image.rasterize(width, height, BufferedImage.TYPE_INT_ARGB);
        if (shape == null) {
            return img;
        } else {
            final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            final Graphics2D g = result.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                    RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g.setClip(makeTransformedPathForSize(width, height, shape));
            g.drawImage(img, 0, 0, null);
            g.dispose();
            return result;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.ProfilesComparisonReportChartCanvas.java

protected void paintComponent(Graphics g) {

    // prepare graphic object
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // set clipping area
    if (is_printing) {
        g2d.translate(-draw_area.x, -draw_area.y);
        g2d.setClip(draw_area);
    }/*ww  w. ja v  a2 s. c  om*/

    //paint canvas background
    if (!is_printing) {
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }

    // paint white background on drawing area    
    g2d.setColor(Color.white);
    g2d.fillRect(draw_area.x, draw_area.y, draw_area.width, draw_area.height);
    if (!is_printing) {
        g2d.setColor(Color.black);
        g2d.draw(draw_area);
    }

    // paint
    paintChart(g2d);

    // dispose graphic object
    g2d.dispose();

    revalidate();
}

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);//from   w w w  .  jav  a 2  s  .  c o  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:FontPaint.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*from   www.jav a2  s  .co  m*/
    int width = getSize().width;
    int height = getSize().height;
    Graphics2D g2 = (Graphics2D) g;

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

    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, 60);
    String s = new String("Java Source and Support.");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle outlineBounds = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(width / 2 - (outlineBounds.width / 2), height / 2 + (outlineBounds.height / 2));
    g2.transform(transform);
    g2.setColor(Color.blue);
    g2.draw(outline);
    g2.setClip(outline);
}

From source file:Starry.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*from  ww w .  j  a v  a  2 s. co  m*/
    w = getSize().width;
    h = getSize().height;
    Graphics2D g2;
    g2 = (Graphics2D) g;

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

    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, w / 10);
    String s = new String("The Starry Night");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle r = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(w / 2 - (r.width / 2), h / 2 + (r.height / 2));
    g2.transform(transform);
    g2.setColor(Color.blue);
    g2.draw(outline);
    g2.setClip(outline);
    g2.drawImage(img, r.x, r.y, r.width, r.height, this);

}

From source file:com.projity.pm.graphic.network.NetworkRenderer.java

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

    Rectangle clipBounds = g2.getClipBounds();
    Rectangle svgClip = clipBounds;
    if (clipBounds == null) {
        clipBounds = getGraphInfo().getDrawingBounds();
        //start at O,O because it's already translated
        if (visibleBounds == null)
            clipBounds = new Rectangle(0, 0, clipBounds.width, clipBounds.height);
        else {/*from w w  w.j  av  a  2s .  c om*/
            clipBounds = visibleBounds;
            g2.setClip(clipBounds);
        }
    }
    //Modif for offline graphics

    GraphicDependency dependency;
    for (Iterator i = getDependenciesIterator(); i.hasNext();) {
        dependency = (GraphicDependency) i.next();
        paintLink(g2, dependency);
    }

    GraphicNode node;
    Rectangle bounds;
    for (ListIterator i = graphInfo.getCache().getIterator(); i.hasNext();) {
        node = (GraphicNode) i.next();
        bounds = getBounds(node);
        if (bounds == null)
            continue;
        if (clipBounds.intersects(bounds))
            paintNode(g2, node);
    }

    if (visibleBounds != null)
        g2.setClip(svgClip);
}

From source file:net.sf.maltcms.chromaui.charts.events.XYAnnotationAdder.java

/**
 *
 * @param g2//www  . ja  v a 2s  .c  om
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    if (visible) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        g2.clip(dataArea);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        ValueAxis yAxis = plot.getRangeAxis();
        paint(g2, plot, dataArea, xAxis, yAxis, plot.getDomainAxisEdge(), plot.getRangeAxisEdge(), chartPanel);
        g2.setClip(savedClip);
    }
}

From source file:org.jfree.experimental.chart.plot.dial.DialPlot.java

/**
 * Draws the plot.  This method is usually called by the {@link JFreeChart}
 * instance that manages the plot./*  ww w .  java2  s  . co m*/
 * 
 * @param g2  the graphics target.
 * @param area  the area in which the plot should be drawn.
 * @param anchor  the anchor point (typically the last point that the 
 *     mouse clicked on, <code>null</code> is permitted).
 * @param parentState  the state for the parent plot (if any).
 * @param info  used to collect plot rendering info (<code>null</code> 
 *     permitted).
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    // first, expand the viewing area into a drawing frame
    Rectangle2D frame = viewToFrame(area);

    // draw the background if there is one...
    if (this.background != null && this.background.isVisible()) {
        if (this.background.isClippedToWindow()) {
            Shape savedClip = g2.getClip();
            g2.setClip(this.dialFrame.getWindow(frame));
            this.background.draw(g2, this, frame, area);
            g2.setClip(savedClip);
        } else {
            this.background.draw(g2, this, frame, area);
        }
    }

    Iterator iterator = this.layers.iterator();
    while (iterator.hasNext()) {
        DialLayer current = (DialLayer) iterator.next();
        if (current.isVisible()) {
            if (current.isClippedToWindow()) {
                Shape savedClip = g2.getClip();
                g2.setClip(this.dialFrame.getWindow(frame));
                current.draw(g2, this, frame, area);
                g2.setClip(savedClip);
            } else {
                current.draw(g2, this, frame, area);
            }
        }
    }

    // draw the cap if there is one...
    if (this.cap != null && this.cap.isVisible()) {
        if (this.cap.isClippedToWindow()) {
            Shape savedClip = g2.getClip();
            g2.setClip(this.dialFrame.getWindow(frame));
            this.cap.draw(g2, this, frame, area);
            g2.setClip(savedClip);
        } else {
            this.cap.draw(g2, this, frame, area);
        }
    }

    if (this.dialFrame.isVisible()) {
        this.dialFrame.draw(g2, this, frame, area);
    }

}