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(int x, int y, int width, int height);

Source Link

Document

Sets the current clip to the rectangle specified by the given coordinates.

Usage

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content/*  w  w  w  .  ja  v a 2s  .c om*/
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}

From source file:MainClass.java

public void paint(Graphics g) {
    g.drawString(g.getClipBounds().toString(), 10, 30);

    g.clipRect(10, 40, getSize().width - 20, getSize().height - 80);

    g.fillOval(0, 0, getSize().width, getSize().height);

    String newClip = g.getClipBounds().toString();

    g.setClip(0, 0, getSize().width, getSize().height);

    g.drawString(newClip, 10, getSize().height - 10);
}

From source file:Main.java

public void paint(Graphics g) {
    g.drawString(g.getClipBounds().toString(), 10, 30);

    g.clipRect(10, 40, getSize().width - 20, getSize().height - 80);

    g.fillOval(0, 0, getSize().width, getSize().height);

    String newClip = g.getClipBounds().toString();

    g.setClip(0, 0, getSize().width, getSize().height);

    g.drawString(newClip, 10, getSize().height - 10);

}

From source file:MouseDragClip.java

public void paint(Graphics g) {
    int w = curX - startX, h = curY - startY;
    Dimension d = getSize();// ww w. jav  a2  s  . c  o  m
    if (!inDrag) { // probably first time through(?)
        g.drawImage(curImage, 0, 0, d.width, d.height, this);
        return;
    }
    System.err.println("paint:drawRect @[" + startX + "," + startY + "] size " + w + "x" + h);
    // Restore the old background, if previous selection
    if (oldStartX != -1) {
        g.setClip(oldStartX, oldStartY, oldWidth + 1, oldHeight + 1);
        g.drawImage(curImage, 0, 0, d.width, d.height, this);
        oldStartX = -1;
    }
    // Restore the background from previous motions of current drag
    if (oldX != -1) {
        g.setClip(startX, startY, w, h);
        g.drawImage(curImage, 0, 0, d.width + 1, d.height + 1, this);
    }
    // Draw the new rectangle
    g.setClip(0, 0, d.width, d.height);
    g.setColor(Color.red);
    g.drawRect(startX, startY, w, h);
    oldX = curX;
    oldY = curY;
}

From source file:org.kchine.r.server.impl.RGraphicsPanelRemote.java

public synchronized void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension d = getSize();// ww  w  .ja  va 2s  .  c o m

    if (!d.equals(_lastSize)) {
        _lastResizeTime = System.currentTimeMillis();
        _lastSize = d;
        return;
    } else {

    }

    if (forceAntiAliasing) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    int i = 0, j = _l.size();
    g.setFont(_gs.f);
    g.setClip(0, 0, d.width, d.height); // reset clipping rect
    g.setColor(Color.white);
    g.fillRect(0, 0, d.width, d.height);
    while (i < j) {
        GDObject o = (GDObject) _l.elementAt(i++);
        o.paint(this, _gs, g);
    }
}

From source file:JuliaSet2.java

public void print() {
    // Create some attributes objects. This is Java 1.3 stuff.
    // In Java 1.1, we'd use a java.util.Preferences object instead.
    JobAttributes jattrs = new JobAttributes();
    PageAttributes pattrs = new PageAttributes();

    // Set some example attributes: monochrome, landscape mode
    pattrs.setColor(PageAttributes.ColorType.MONOCHROME);
    pattrs.setOrientationRequested(PageAttributes.OrientationRequestedType.LANDSCAPE);
    // Print to file by default
    jattrs.setDestination(JobAttributes.DestinationType.FILE);
    jattrs.setFileName("juliaset.ps");

    // Look up the Frame that holds this component
    Component frame = this;
    while (!(frame instanceof Frame))
        frame = frame.getParent();//from   w w w .  jav  a 2s  .  com

    // Get a PrintJob object to print the Julia set with.
    // The getPrintJob() method displays a print dialog and allows the user
    // to override and modify the default JobAttributes and PageAttributes
    Toolkit toolkit = this.getToolkit();
    PrintJob job = toolkit.getPrintJob((Frame) frame, "JuliaSet1", jattrs, pattrs);

    // We get a null PrintJob if the user clicked cancel
    if (job == null)
        return;

    // Get a Graphics object from the PrintJob.
    // We print simply by drawing to this Graphics object.
    Graphics g = job.getGraphics();

    // Center the image on the page
    Dimension pagesize = job.getPageDimension(); // how big is page?
    Dimension panesize = this.getSize(); // how big is image?
    g.translate((pagesize.width - panesize.width) / 2, // center it
            (pagesize.height - panesize.height) / 2);

    // Draw a box around the Julia Set and label it
    g.drawRect(-1, -1, panesize.width + 2, panesize.height + 2);
    g.drawString("Julia Set for c={" + cx + "," + cy + "}", 0, -15);

    // Set a clipping region
    g.setClip(0, 0, panesize.width, panesize.height);

    // Now print the component by calling its paint method
    this.paint(g);

    // Finally tell the printer we're done with the page.
    // No output will be generated if we don't call dispose() here.
    g.dispose();
}

From source file:CopyAreaPerformance.java

protected void paintComponent(Graphics g) {
    long startTime = System.nanoTime();
    // prevVX is set to -10000 when first enabled
    if (useCopyArea && prevVX > -9999) {
        // Most of this code determines the proper areas to copy and clip
        int scrollX = viewX - prevVX;
        int scrollY = viewY - prevVY;
        int copyFromY, copyFromX;
        int clipFromY, clipFromX;
        if (scrollX == 0) {
            // vertical scroll
            if (scrollY < 0) {
                copyFromY = 0;/*from w  ww  . ja  v  a  2s  .com*/
                clipFromY = 0;
            } else {
                copyFromY = scrollY;
                clipFromY = getHeight() - scrollY;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(0, copyFromY, getWidth(), getHeight() - Math.abs(scrollY), 0, -scrollY);
            g.setClip(0, clipFromY, getWidth(), Math.abs(scrollY));
        } else {
            // horizontal scroll
            if (scrollX < 0) {
                copyFromX = 0;
                clipFromX = 0;
            } else {
                copyFromX = scrollX;
                clipFromX = getWidth() - scrollX;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(copyFromX, 0, getWidth() - Math.abs(scrollX), getHeight(), -scrollX, 0);
            g.setClip(clipFromX, 0, Math.abs(scrollX), getHeight());
        }
    }
    // Track previous view position for next scrolling operation
    prevVX = viewX;
    prevVY = viewY;
    // Get the clip in case we need it later
    Rectangle clipRect = g.getClip().getBounds();
    int clipL = (int) (clipRect.getX());
    int clipT = (int) (clipRect.getY());
    int clipR = (int) (clipRect.getMaxX());
    int clipB = (int) (clipRect.getMaxY());
    g.setColor(Color.WHITE);
    g.fillRect(clipL, clipT, (int) clipRect.getWidth(), (int) clipRect.getHeight());
    for (int column = 0; column < 256; ++column) {
        int x = column * (SMILEY_SIZE + PADDING) - viewX;
        if (useClip) {
            if (x > clipR || (x + (SMILEY_SIZE + PADDING)) < clipL) {
                // trivial reject; outside to the left or right
                continue;
            }
        }
        for (int row = 0; row < 256; ++row) {
            int y = row * (SMILEY_SIZE + PADDING) - viewY;
            if (useClip) {
                if (y > clipB || (y + (SMILEY_SIZE + PADDING)) < clipT) {
                    // trivial reject; outside to the top or bottom
                    continue;
                }
            }
            Color faceColor = new Color(column, row, 0);
            drawSmiley(g, faceColor, x, y);
        }
    }
    long stopTime = System.nanoTime();
    System.out.println("Painted in " + ((stopTime - startTime) / 1000000) + " ms");
}

From source file:org.gephi.ui.components.ReportSelection.java

/**
 *
 * @param graphics//from  w w w. j  a  va  2s .com
 * @param pageFormat
 * @param pageIndex
 * @return
 */
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {

    boolean last = false;
    try {

        View rootView = displayPane.getUI().getRootView(displayPane);

        double scaleX = pageFormat.getImageableWidth() / displayPane.getMinimumSize().getWidth();

        scaleX = Math.min(scaleX, 1.0);
        double scaleY = scaleX;

        int end = (int) (pageIndex * ((1.0f / scaleY) * (double) pageFormat.getImageableHeight()));
        Rectangle allocation = new Rectangle(0, -end, (int) pageFormat.getImageableWidth(),
                (int) pageFormat.getImageableHeight());
        ((Graphics2D) graphics).scale(scaleX, scaleY);

        graphics.setClip((int) (pageFormat.getImageableX() / scaleX),
                (int) (pageFormat.getImageableY() / scaleY), (int) (pageFormat.getImageableWidth() / scaleX),
                (int) (pageFormat.getImageableHeight() / scaleY));

        ((Graphics2D) graphics).translate(((Graphics2D) graphics).getClipBounds().getX(),
                ((Graphics2D) graphics).getClipBounds().getY());

        rootView.paint(graphics, allocation);

        last = end > displayPane.getUI().getPreferredSize(displayPane).getHeight();

        if ((last)) {
            return Printable.NO_SUCH_PAGE;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Printable.PAGE_EXISTS;
}

From source file:edu.ku.brc.services.mapping.LocalityMapper.java

/**
 * Grabs a new map from the web service and appropriately adorns it
 * with labels and markers./*  w  ww.  j  a  v a 2  s  . c om*/
 *
 * @return a map image as an icon
 * @throws HttpException a network error occurred while grabbing the map from the service
 * @throws IOException a network error occurred while grabbing the map from the service
 */
protected Icon grabNewMap() throws HttpException, IOException {
    recalculateBoundingBox();

    if (!cacheValid) {
        //            Image mapImage = getMapFromService("mapus.jpl.nasa.gov",
        //                    "/wms.cgi?request=GetMap&srs=EPSG:4326&format=image/png&styles=visual",
        //                    "global_mosaic",
        //                    mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);
        //
        //            Image overlayImage = getMapFromService("129.237.201.132",
        //                    "/cgi-bin/mapserv?map=/var/www/maps/specify.map&service=WMS&request=GetMap&srs=EPSG:4326&version=1.3.1&format=image/png&transparent=true",
        //                    "states,rivers",
        //                    mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);

        Image mapImage = getMapFromService("lifemapper.org", //$NON-NLS-1$
                "/ogc?map=specify.map&service=WMS&request=GetMap&srs=EPSG:4326&version=1.3.1&STYLES=&format=image/png&transparent=TRUE", //$NON-NLS-1$
                "global_mosaic,states,rivers", //$NON-NLS-1$
                mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);

        mapIcon = new ImageIcon(mapImage);
        //            overlayIcon = new ImageIcon(overlayImage);
        cacheValid = true;

        mapWidth = mapIcon.getIconWidth();
        mapHeight = mapIcon.getIconHeight();

        if (mapWidth < 0 || mapHeight < 0) {
            throw new IOException("Request for map failed.  Received map has negative width or height."); //$NON-NLS-1$
        }

        mapLatRange = mapMaxLat - mapMinLat;
        mapLongRange = mapMaxLong - mapMinLong;

        pixelPerLatRatio = mapHeight / mapLatRange;
        pixelPerLongRatio = mapWidth / mapLongRange;

        for (int i = 0; i < mapLocations.size(); ++i) {
            MapLocationIFace loc = mapLocations.get(i);
            Point iconLoc = determinePixelCoordsOfMapLocationIFace(loc);
            markerLocations.set(i, iconLoc);
        }

        cacheValid = true;
    }

    Icon icon = new Icon() {
        public void paintIcon(Component c, Graphics g, int x, int y) {
            // this helps keep the labels inside the map
            g.setClip(x, y, mapWidth, mapHeight);
            // log the x and y for the MouseMotionListener
            mostRecentPaintedX = x;
            mostRecentPaintedY = y;
            Point currentLocPoint = null;
            if (currentLoc != null) {
                currentLocPoint = determinePixelCoordsOfMapLocationIFace(currentLoc);
            }

            mapIcon.paintIcon(c, g, x, y);
            //                overlayIcon.paintIcon(c, g, x, y);

            Point lastLoc = null;
            for (int i = 0; i < mapLocations.size(); ++i) {
                Point markerLoc = markerLocations.get(i);
                String label = labels.get(i);
                boolean current = (currentLoc != null) && markerLoc.equals(currentLocPoint);

                if (markerLoc == null) {
                    log.error("A marker location is null"); //$NON-NLS-1$
                    continue;
                }
                if (!pointIsOnMapIcon(x + markerLoc.x, y + markerLoc.y)) {
                    log.error("A marker location is off the map"); //$NON-NLS-1$
                    continue;
                }
                if (showArrows && lastLoc != null) {
                    int x1 = x + lastLoc.x;
                    int y1 = y + lastLoc.y;
                    int x2 = x + markerLoc.x;
                    int y2 = y + markerLoc.y;
                    Color origColor = g.getColor();
                    if (current && !animationInProgress) {
                        g.setColor(getCurrentLocColor());
                    } else {
                        g.setColor(arrowColor);
                    }
                    GraphicsUtils.drawArrow(g, x1, y1, x2, y2, 2, 2);
                    g.setColor(origColor);
                }
                if (current) {
                    currentLocMarker.paintIcon(c, g, markerLoc.x + x, markerLoc.y + y);
                } else {
                    marker.paintIcon(c, g, markerLoc.x + x, markerLoc.y + y);
                }
                if (label != null) {
                    Color origColor = g.getColor();
                    FontMetrics fm = g.getFontMetrics();
                    int length = fm.stringWidth(label);
                    g.setColor(Color.WHITE);
                    g.fillRect(markerLoc.x + x - (length / 2), markerLoc.y + y - (fm.getHeight() / 2), length,
                            fm.getHeight());
                    g.setColor(labelColor);
                    GraphicsUtils.drawCenteredString(label, g, markerLoc.x + x, markerLoc.y + y);
                    g.setColor(origColor);
                }

                lastLoc = markerLoc;
            }
            if (showArrowAnimations && animationInProgress) {
                int startIndex = mapLocations.indexOf(animStartLoc);
                int endIndex = mapLocations.indexOf(animEndLoc);
                if (startIndex != -1 && endIndex != -1) {
                    Point startPoint = markerLocations.get(startIndex);
                    Point endPoint = markerLocations.get(endIndex);
                    Point arrowEnd = GraphicsUtils.getPointAlongLine(startPoint, endPoint, percent);
                    Color orig = g.getColor();
                    g.setColor(getCurrentLocColor());
                    GraphicsUtils.drawArrow(g, startPoint.x + x, startPoint.y + y, arrowEnd.x + x,
                            arrowEnd.y + y, 5, 3);
                    g.setColor(orig);
                }
            }
        }

        public int getIconWidth() {
            return mapWidth;
        }

        public int getIconHeight() {
            return mapHeight;
        }
    };

    return icon;
}

From source file:com.jcraft.weirdx.XPixmap.java

static void reqPutImage(Client c) throws IOException {
    int foo, n;//from  w ww .ja va  2s. c o m
    InputOutput io = c.client;
    byte format;
    short width, height, dstx, dsty;
    byte depth;
    byte lpad;
    XPixmap pixmap = null;
    format = (byte) c.data;
    n = c.length;
    foo = io.readInt();
    XDrawable d = c.lookupDrawable(foo);
    if (d == null) {
        c.errorValue = foo;
        c.errorReason = 9; // BadDrawable;
    }
    foo = io.readInt();
    GC gc = c.lookupGC(foo);
    if (gc == null && c.errorReason == 0) {
        c.errorValue = foo;
        c.errorReason = 13; // GC
    }
    width = (short) io.readShort();
    height = (short) io.readShort();
    dstx = (short) io.readShort();
    dsty = (short) io.readShort();
    lpad = (byte) io.readByte();
    depth = (byte) io.readByte();
    io.readPad(2);
    c.length -= 6;
    n -= 6;
    if (c.errorReason != 0) {
        return;
    }
    if (dsty < 0) {
        //      height+=dsty;
        dsty = 0;
    }
    if (dstx < 0) {
        //      width+=dstx;
        dstx = 0;
    }

    int ddstx = dstx;
    int ddsty = dsty;

    synchronized (XPixmap.class) {
        if (d instanceof XPixmap) {
            pixmap = (XPixmap) d;
            if (pixmap.imageDirty) {
                pixmap.image2data();
            }
        } else {
            if (!((XWindow) d).ddxwindow.isVisible()) {
                io.readPad(n * 4);
                return;
            }
            pixmap = null;
            XPixmap[] pixmaps = ((XWindow) d).screen.pixmaps;
            for (int i = 0; i < pixmaps.length; i++) {
                if (pixmaps[i].depth == d.depth) {
                    pixmap = pixmaps[i];
                    break;
                }
            }
            if (pixmap == null) {
            }
            ((Resizable) pixmap).setSize(width, height);
            ((Resizable) pixmap).setColormap(d.getColormap());
            pixmap.lpad = lpad;
            dstx = 0;
            dsty = 0;
        }

        byte[] data = null;

        data = pixmap.getData();

        int ww = 0;
        int j = 0;
        int i = 0;

        j = dsty;

        if (depth == 1 && (pixmap.depth == 1 || pixmap.depth == 8)) {
            int www = 0;
            if (d instanceof XWindow) {
                www = ((Resizable) pixmap).getRealWidth();
            } else {
                www = pixmap.width;
            }

            if (WeirdX.imageByteOrder == 1) {
                Pixmap1.putData(c, data, www, dstx, dsty, width, lpad);
            } else {
                Pixmap1.putData_LSB(c, data, www, dstx, dsty, width, lpad);
            }

            if (d.depth != 1) {
                byte f = (byte) gc.fgPixel;
                byte b = (byte) gc.bgPixel;
                for (i = dsty; i < height + dsty; i++) {
                    for (j = dstx; j < width + dstx; j++) {
                        if (data[i * www + j] == 0) {
                            data[i * www + j] = b;
                        } else {
                            data[i * www + j] = f;
                        }
                    }
                }

            }
        } else if (depth == 8) {
            if (format == 2) { // ZPixmap
                int restw = width;
                int restww = width;
                int resth = height;
                if (d.width < dstx + width) {
                    restw = (short) (d.width - dstx);
                }
                if (d.height < dsty + height) {
                    resth = (short) (d.height - dsty);
                }

                int www = 0;
                if (d instanceof XWindow) {
                    www = ((Resizable) pixmap).getRealWidth();
                } else {
                    www = pixmap.width;
                }
                j *= www;

                if (width < c.bbuffer.length) {
                    int paddedwidthi = ((width + 3) / 4);
                    int paddedwidth = paddedwidthi * 4;
                    int padding = paddedwidth - restw;
                    while (n != 0) {
                        if (resth > 0) {
                            io.readByte(data, j + dstx, restw);
                            if (padding != 0) {
                                io.readPad(padding);
                            }
                        } else {
                            io.readPad(paddedwidth);
                        }
                        n -= paddedwidthi;
                        j += www;
                        resth--;
                    }
                } else {
                    while (n != 0) {
                        restww = restw;
                        ww = width;
                        i = dstx;
                        while (0 < ww) {
                            io.readByte(c.bbuffer, 0, 4);
                            n--;
                            if (4 <= ww) {
                                if (resth > 0) {
                                    if (restww >= 4) {
                                        System.arraycopy(c.bbuffer, 0, data, j + i, 4);
                                        restww -= 4;
                                    } else if (restww > 0) {
                                        System.arraycopy(c.bbuffer, 0, data, j + i, restww);
                                        restww = 0;
                                    }
                                }
                                i += 4;
                                ww -= 4;
                            } else {
                                if (resth > 0) {
                                    if (restww >= ww) {
                                        System.arraycopy(c.bbuffer, 0, data, j + i, ww);
                                        restww -= ww;
                                    } else if (restww > 0) {
                                        System.arraycopy(c.bbuffer, 0, data, j + i, restww);
                                        restww = 0;
                                    }
                                }
                                i += ww;
                                break;
                            }
                        }
                        j += www;
                        resth--;
                    }
                }
            } else {
                int www = 0;
                if (d instanceof XWindow) {
                    www = ((Resizable) pixmap).getRealWidth();
                } else {
                    www = pixmap.width;
                }
                n *= 4;
                while (n != 0) {
                    ww = width;
                    i = dstx;
                    while (4 < ww) {
                        foo = io.readInt();
                        n -= 4;
                        data[j * www + i] = (byte) (foo & 0xff);
                        i++;
                        foo = foo >> 8;
                        data[j * www + i] = (byte) (foo & 0xff);
                        i++;
                        foo = foo >> 8;
                        data[j * www + i] = (byte) (foo & 0xff);
                        i++;
                        foo = foo >> 8;
                        data[j * www + i] = (byte) (foo & 0xff);
                        i++;
                        ww -= 4;
                    }
                    if (ww != 0) {
                        foo = io.readInt();
                        n -= 4;
                        while (0 < ww) {
                            data[j * www + i] = (byte) (foo & 0xff);
                            i++;
                            foo = foo >> 8;
                            ww--;
                        }
                    }
                    j++;
                }
            }
        } else if (pixmap.depth == 16) {
            ((Pixmap16) pixmap).putImage(c, gc, dstx, dsty, width, height, lpad, format, depth);
        } else {
            io.readPad(n * 4);
        }

        if (d instanceof XWindow) {
            Graphics g = ((XWindow) d).getGraphics();
            pixmap.mis.newPixels(dstx, dsty, width, height);
            Image dataImg = Toolkit.getDefaultToolkit().createImage(pixmap.mis);
            Image ii = dataImg;

            java.awt.Shape tmp = g.getClip();
            g.clipRect(ddstx, ddsty, width, height);
            g.drawImage(ii, ddstx, ddsty, Screen.screen[0].root.ddxwindow);
            if (tmp == null) {
                g.setClip(0, 0, d.width, d.height);
            } else {
                g.setClip(tmp);
            }
            ((XWindow) d).draw(ddstx, ddsty, width, height);
            dataImg.flush();
        } else {
            if (pixmap.time < pixmap.colormap.icmtime) {
                pixmap.mkMIS();
            }
            pixmap.mis.newPixels(dstx, dsty, width, height);
            Image dataImg = Toolkit.getDefaultToolkit().createImage(pixmap.mis);
            if (dstx != 0 || dsty != 0 || width != d.width || height != d.height) {
                java.awt.Shape tmp = pixmap.imgg.getClip();

                pixmap.imgg.clipRect(dstx, dsty, width, height);
                pixmap.imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow);
                if (tmp == null) {
                    pixmap.imgg.setClip(0, 0, d.width, d.height);
                } else {
                    pixmap.imgg.setClip(tmp);
                }
            } else {
                pixmap.imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow);
            }
            dataImg.flush();
            pixmap.reset();
            pixmap.time = System.currentTimeMillis();
        }
    }
}