Example usage for java.awt Graphics setClip

List of usage examples for java.awt Graphics setClip

Introduction

In this page you can find the example usage for java.awt Graphics 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:Java2DUtils.java

public static void resetClip(Graphics g) {
    g.setClip((Shape) clipBoundsStack.pop());
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * //from   w  ww  .j  av  a  2  s.c om
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private static void paintComponents(Container c, Graphics g) {
    if (!c.isShowing())
        return;/*w  ww  . j ava 2s .c  o  m*/

    final int ncomponents = c.getComponentCount();
    final Rectangle clip = g.getClipBounds();

    int i = ncomponents - 1;
    while (i >= 0) {
        final Component component[] = c.getComponents();
        final Component comp = component[i];
        if (comp == null || !comp.isVisible())
            continue;
        final Rectangle bounds = comp.getBounds();
        Rectangle cr;
        if (clip == null)
            cr = new Rectangle(bounds);
        else
            cr = bounds.intersection(clip);
        if (cr.isEmpty())
            continue;

        final Graphics cg = g.create();
        cg.setClip(cr);
        cg.translate(bounds.x, bounds.y);
        try {
            comp.paint(cg);
        } catch (Throwable e) {
            //
        }

        cg.dispose();
        i--;
    }
}

From source file:Java2DUtils.java

@SuppressWarnings("unchecked")
public static void setClip(Graphics g, Rectangle clipBounds) {
    Rectangle currentClipBounds;/*w w w . jav a 2s  . c o m*/

    clipBounds = new Rectangle(clipBounds);
    clipBounds.width += 1;
    clipBounds.height += 1;

    currentClipBounds = g.getClipBounds();
    if (currentClipBounds != null) {
        clipBounds = clipBounds.intersection(g.getClipBounds());
    }

    clipBoundsStack.push(currentClipBounds);
    g.setClip(clipBounds);
}

From source file:Main.java

public void paint(Graphics 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);
    g.setClip(e);

    g.setColor(Color.red);//  ww  w  .j  ava 2s.  c  o  m
    g.fillRect(0, 0, w, h);

}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

@Override
public void mouseDragged(MouseEvent e) {
    super.mouseDragged(e);

    ChartPanel chartPanel = getChartPanel(e);
    JFreeChart selectedChart = chartPanel.getChart();
    ChartDescriptor cd = ChartUtils.getChartDescriptor(selectedChart);
    int[] indices = cd.getSourceIndices();

    XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();

    //Create double buffer
    Image buffer = chartPanel.createImage(chartPanel.getWidth(), chartPanel.getHeight());
    Graphics bufferGraphics = buffer.getGraphics();
    chartPanel.paint(bufferGraphics);/*from w  w w  . j  a  v a2s  . com*/

    if (lastX == 0 && lastY == 0) {
        lastX = e.getX();
        lastY = e.getY();
    }

    drawRect = new Rectangle();
    int x1 = Math.min(Math.min(e.getX(), lastX), startX);
    int y1 = Math.min(Math.min(e.getY(), lastY), startY);
    int x2 = Math.max(Math.max(e.getX(), lastX), startX);
    int y2 = Math.max(Math.max(e.getY(), lastY), startY);

    drawRect.x = x1;
    drawRect.y = y1;
    drawRect.width = x2 - drawRect.x;
    drawRect.height = y2 - drawRect.y;

    //Create a clipping rectangle
    Rectangle clipRect = new Rectangle(drawRect.x - 100, drawRect.y - 100, drawRect.width + 200,
            drawRect.height + 200);

    //Check for selected points
    for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) {
        for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) {
            Number xK = plot.getDataset().getX(i, j);
            Number yK = plot.getDataset().getY(i, j);
            Point2D datasetPoint2D = new Point2D.Double(domainValueTo2D(chartPanel, plot, xK.doubleValue()),
                    rangeValueTo2D(chartPanel, plot, yK.doubleValue()));

            if (drawRect.contains(datasetPoint2D)) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointAdded = mouseDragSelection.addPoint(cp);
                if (pointAdded) {
                    ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(j, i);
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            } else if (!mouseDragSelection.isEmpty()) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointRemoved = mouseDragSelection.removePoint(cp);
                if (pointRemoved) {
                    ((ScatterPlotRenderer) plot.getRenderer()).removeMarkedPoint(new Point(j, i));
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            }
        }
    }

    Iterator<PlotPointData> iterator = currentSelection.iterator();
    while (iterator.hasNext()) {
        PlotPointData next = iterator.next();
        Point dataPoint = next.getDataPoint();
        ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(dataPoint);
    }

    lastX = e.getX();
    lastY = e.getY();

    Graphics graphics = chartPanel.getGraphics();
    graphics.setClip(clipRect);

    //Draw selection rectangle
    bufferGraphics.drawRect(drawRect.x, drawRect.y, drawRect.width, drawRect.height);

    graphics.drawImage(buffer, 0, 0, chartPanel.getWidth(), chartPanel.getHeight(), null);
}

From source file:Main.java

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;

    int bottomLineY = height - thickness - shadowPad;

    RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(0 + strokePad, 0 + strokePad,
            width - thickness - shadowPad, bottomLineY, radius, radius);

    Area area = new Area(bubble);

    g2.setRenderingHints(hints);//from w ww.  j av  a 2 s . co m

    g2.setColor(color);
    g2.setStroke(stroke);
    g2.draw(area);

    Area shadowArea = new Area(new Rectangle(0, 0, width, height));
    shadowArea.subtract(area);
    g.setClip(shadowArea);
    Color shadow = new Color(color.getRed(), color.getGreen(), color.getBlue(), 128);
    g2.setColor(shadow);
    g2.translate(shadowPad, shadowPad);
    g2.draw(area);
}

From source file:web.diva.server.model.SomClustering.SomClustImgGenerator.java

private void drawScale(Graphics scale, Point st, int width, int height) {
    Rectangle r = new Rectangle(st.x, st.y, width, height);
    if (width < 50 || height < 25) {
        return;//from   w w w. j  a  v a 2  s.  c  o m
    }
    ScaleAndAxis sc = new ScaleAndAxis();
    sc.setColorFactory(colors);
    scale.translate(st.x, st.y);
    Rectangle bac = scale.getClipBounds();
    sc.setLocation(r.x, r.y);
    sc.setSize(r.width, r.height);
    sc.paintComponent(scale);
    scale.setClip(bac);
    scale.translate(-st.x, -st.y);
}

From source file:tufts.vue.RichTextBox.java

public void Xpaint(Graphics g) {
    super.paint(g);
    g.setColor(Color.gray);//from www . j a  va2  s.  c  o  m
    g.setClip(null);
    g.drawRect(0, 0, getWidth(), getHeight());

}

From source file:tufts.vue.RichTextBox.java

public void paintComponent(Graphics g) {
    if (TestDebug || DEBUG.TEXT)
        out("paintComponent @ " + getX() + "," + getY() + " parent=" + getParent());

    final MapViewer viewer = (MapViewer) javax.swing.SwingUtilities.getAncestorOfClass(MapViewer.class, this);
    Graphics2D g2d = (Graphics2D) g;
    if (viewer != null) {
        g2d.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, viewer.AA_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    }//from www.j ava2s. co m
    // turn on anti-aliasing -- the cursor repaint loop doesn't
    // set anti-aliasing, so text goes jiggy around cursor/in selection if we don't do this
    ////  g.clipRect(0, 0,getWidth(), getAdjustedHeight());

    super.paintComponent(g2d);

    //super.paintComponent(g);
    if (true) {
        // draw a border (we don't want to add one because that changes the preferred size at a bad time)
        //  g.setColor(Color.gray);
        g.setClip(null);
        final int xpad = 1;
        final int ypad = 1;
        g.drawRect(-xpad, -ypad, (int) ((getWidth()) + xpad * 2 - 1), (int) ((getHeight()) + ypad * 2 - 1));
    }
}