Example usage for java.awt Graphics2D clip

List of usage examples for java.awt Graphics2D clip

Introduction

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

Prototype

public abstract void clip(Shape s);

Source Link

Document

Intersects the current Clip with the interior of the specified Shape and sets the Clip to the resulting intersection.

Usage

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

private static void drawClipTest(Graphics2D g2) {
    g2.translate(10, 20);// w w  w. j a  va 2 s  .  c  o  m
    g2.setColor(Color.RED);
    g2.fillRect(10, 10, 100, 100);
    g2.clip(new Rectangle(0, 0, 60, 60));
    g2.setPaint(Color.BLUE);
    g2.fillRect(10, 10, 100, 100);
    g2.setClip(null);
    g2.setPaint(Color.GREEN);
    g2.fillRect(60, 60, 50, 50);
}

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

public static void drawShape(Graphics2D g2, Rectangle2D imageArea, Shape shape, Color color, JFreeChart chart) {
    g2.clip(imageArea);
    g2.setPaint(color);/*from w  w w . j  a  v  a 2s .  c  o m*/
    Shape newShape = translateChartShape(shape, imageArea, chart);
    if (shape == null) {
        return;
    }
    g2.draw(newShape);
}

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

private static void drawDomainMask(Graphics2D g2, Rectangle2D imageArea, RangeMask mask, RangeMask selectedMask,
        JFreeChart chart, double fontSizeRate, Color fillColor) {
    g2.clip(imageArea);
    g2.setPaint(fillColor);//from   w  w w. ja v a2  s .  c  om
    Rectangle2D translatedRectangle = getDomainMaskFrame(mask, imageArea, chart);
    if (translatedRectangle == null || translatedRectangle.isEmpty()) {
        return;
    }
    drawMask(g2, translatedRectangle);
    if (mask == selectedMask) {
        drawMaskBoarder(g2, translatedRectangle);
    }
    drawMaskName(g2, mask, translatedRectangle, fontSizeRate);
}

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

/**
 * Draws mask rectangle (if present)./*from   w ww.j  a  v a2  s.  c o  m*/
 * The drawing is performed in XOR mode, therefore
 * when this method is called twice in a row,
 * the second call will completely restore the state
 * of the canvas.
 *
 * @param g2 the graphics device.
 * @param xor  use XOR for drawing?
 */
private static void drawMaskRectangle(Graphics2D g2, Rectangle2D imageArea, Abstract2DMask mask,
        Abstract2DMask selectedMask, JFreeChart chart, double fontSizeRate, Color fillColor) {
    g2.clip(imageArea);
    g2.setPaint(fillColor);
    Abstract2DMask translatedMask = translateChartRectangle(mask, imageArea, chart);
    if (translatedMask == null || translatedMask.getRectangleFrame().isEmpty()) {
        return;
    }
    drawMask(g2, translatedMask.getShape());
    if (mask == selectedMask) {
        drawMaskBoarder(g2, translatedMask);
    }
    drawMaskName(g2, translatedMask, imageArea, fontSizeRate);
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Ellipse2D e = new Ellipse2D.Float(10, 10, 200, 200);
    g2d.clip(e);
    g2d.fillRect(40, 60, 500, 500);/*from   w ww  .j ava2s. c o m*/
}

From source file:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response./*from  w  ww . j ava  2  s . c o  m*/
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.MultiAxesCrosshairOverlay.java

@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    Shape savedClip = g2.getClip();
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    g2.clip(dataArea);
    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();
    RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
    Iterator iterator = this.getDomainCrosshairs().iterator();
    while (iterator.hasNext()) {
        Crosshair ch = (Crosshair) iterator.next();
        if (ch.isVisible()) {
            double x = ch.getValue();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                drawVerticalCrosshair(g2, dataArea, xx, ch);
            } else {
                drawHorizontalCrosshair(g2, dataArea, xx, ch);
            }//from www  .  j a v  a2s .  c om
        }
    }

    int rangeAxisIdx = 0;
    for (ArrayList<Crosshair> crosshairsForRange : rangeCrosshairs) {
        ValueAxis yAxis = plot.getRangeAxis(rangeAxisIdx);
        RectangleEdge yAxisEdge = plot.getRangeAxisEdge(rangeAxisIdx);
        iterator = crosshairsForRange.iterator();
        while (iterator.hasNext()) {
            Crosshair ch = (Crosshair) iterator.next();
            if (ch.isVisible()) {
                double y = ch.getValue();
                double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
                if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                    drawHorizontalCrosshair(g2, dataArea, yy, ch);
                } else {
                    drawVerticalCrosshair(g2, dataArea, yy, ch);
                }
            }
        }
        g2.setClip(savedClip);
        ++rangeAxisIdx;
    }
}

From source file:ClipBetweenRectangleEllipse2D.java

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

    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width;
    int h = getSize().height;
    Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
    g2.setClip(e);/*from  w w w.j  a  v  a  2 s  . c  o  m*/

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

    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:net.sf.maltcms.chromaui.charts.overlay.Peak1DHeatmapOverlay.java

/**
 *
 * @param g2/*from   ww  w.  j ava2s  . c  o  m*/
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    if (isVisible()) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        g2.clip(dataArea);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
        ValueAxis yAxis = plot.getRangeAxis();
        RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
        Color c = g2.getColor();
        Color fillColor = peakAnnotations.getColor();
        if (fillColor == null || fillColor.equals(Color.WHITE)
                || fillColor.equals(new Color(255, 255, 255, 0))) {
            Logger.getLogger(getClass().getName())
                    .info("Peak annotation color was null or white, using color from treatment group!");
            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
        }
        g2.setColor(ChartCustomizer.withAlpha(fillColor, 0.5f));
        for (IPeakAnnotationDescriptor descr : peakAnnotations.getMembers()) {
            double x = descr.getApexTime();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            double width = xAxis.valueToJava2D(1, dataArea, xAxisEdge);
            double mzRange = (descr.getMassValues()[descr.getMassValues().length - 1]
                    - descr.getMassValues()[0]);
            double y = mzRange / 2.0d;
            double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
            double height = yAxis.valueToJava2D(mzRange, dataArea, yAxisEdge);
            AffineTransform at = AffineTransform.getTranslateInstance(xx, yy);
            at.concatenate(AffineTransform.getTranslateInstance(-x, -y));
            Rectangle2D.Double r = new Rectangle2D.Double(xx - (width / 2.0d), yy, width, height);
            g2.fill(at.createTransformedShape(r));
        }
        g2.setColor(c);
        g2.setClip(savedClip);
    }
}

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.  j a  v a2s .c om
    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);
}