Example usage for java.awt Graphics getClip

List of usage examples for java.awt Graphics getClip

Introduction

In this page you can find the example usage for java.awt Graphics getClip.

Prototype

public abstract Shape getClip();

Source Link

Document

Gets the current clipping area.

Usage

From source file:Main.java

public void paint(Graphics g) {
    g.drawString(g.getClip() + "", 10, 30);
}

From source file:Myopia.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    if (w == 0 || h == 0) {
        return;//w  ww  .  j a  va2  s.c  o m
    }

    // Only create the offscreen image if the one we have
    // is the wrong size.
    if (mOffscreenImage == null || mOffscreenImage.getWidth() != w || mOffscreenImage.getHeight() != h) {
        mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D ig2 = mOffscreenImage.createGraphics();
    ig2.setClip(g.getClip());
    super.paint(ig2, c);
    ig2.dispose();

    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}

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

static void reqPutImage(Client c) throws IOException {
    int foo, n;/*from   w w w .  ja  v  a 2 s .  c om*/
    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();
        }
    }
}

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

public void paintComponent(Graphics g) {
    if (window.clss == InputOnly) {
        super.paintComponent(g);
        return;/*from  w  w  w. j a va2s.c o  m*/
    }

    if (offi == null)
        return;

    g.drawImage(offi, bw, bw, this);

    Rectangle r = g.getClip().getBounds();
    if (bw > 0 && (r.x <= bw || r.y <= bw || (width + bw) <= (r.width + r.x)
            || (height + bw) <= (r.height + r.y))) {
        if (window.isBorderPixel()) {
            g.setColor(window.getColormap().getColor(window.border.pixel));
        } else {
            g.setColor(Color.black);
        }
        for (int i = bw - 1; 0 <= i; i--) {
            g.drawRect(i, i, width + 2 * bw - i * 2 - 1, height + 2 * bw - i * 2 - 1);
        }
    }

    if (window.screen.windowmode == WeirdX.InBrowser || window == window.screen.root) {
        if (LogoImage.logoimage != null && window.x + r.x <= LogoImage.logoimagewidth
                && window.y + r.y <= LogoImage.logoimageheight) {
            g.drawImage(LogoImage.logoimage, 0, 0, LogoImage.logoimagewidth - (window.x - bw),
                    LogoImage.logoimageheight - (window.y - bw), window.x - bw, window.y - bw,
                    LogoImage.logoimagewidth, LogoImage.logoimageheight, this);
        } else {
        }
    }
}

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

public void paint(Graphics g) {
    try { // ?????
        super.paint(g);
    } catch (Exception e) {
        return;/*from  w  w w . j  a va2  s .c om*/
    }
    Client client = window.client;
    if (client == null || client == serverClient)
        return;
    Rectangle r = g.getClip().getBounds();
    if (exposed.width == 0) {
        exposed.setBounds(r);
    } else if (expcontains(r.x, r.y, r.width, r.height)) {
        return;
    } else {
        expunion(r);
    }

    event.mkExpose(window.id, r.x, r.y, r.width, r.height, 0);
    try {
        window.sendEvent(event, 1, null);
    } catch (Exception ee) {
    }
}

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

public void paintComponent(Graphics g) {
    /*    if(window.clss==InputOnly){
          super.paintComponents(g);//from www  .  j  ava 2  s.co m
          return;
        }*/

    if (offi == null)
        return;

    g.drawImage(offi, bw, bw, this);

    if (window == null)
        return;

    Rectangle r = g.getClip().getBounds();
    if (bw > 0 && (r.x <= bw || r.y <= bw || (width + bw) <= (r.width + r.x)
            || (height + bw) <= (r.height + r.y))) {
        if (window.isBorderPixel()) {
            g.setColor(window.getColormap().getColor(window.border.pixel));
        } else {
            g.setColor(Color.black);
        }
        for (int i = bw - 1; 0 <= i; i--) {
            g.drawRect(i, i, width + 2 * bw - i * 2 - 1, height + 2 * bw - i * 2 - 1);
        }
    }

    if (window.screen.windowmode == WeirdX.InBrowser || window == window.screen.root) {
        if (LogoImage.logoimage != null && window.x + r.x <= LogoImage.logoimagewidth
                && window.y + r.y <= LogoImage.logoimageheight) {
            g.drawImage(LogoImage.logoimage, 0, 0, LogoImage.logoimagewidth - (window.x - bw),
                    LogoImage.logoimageheight - (window.y - bw), window.x - bw, window.y - bw,
                    LogoImage.logoimagewidth, LogoImage.logoimageheight, this);
        } else {
        }
    }
}

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

public void paint(Graphics g) {
    paintComponent(g);/*from w ww  .  j  ava2s.co  m*/
    try { // ?????
        super.paint(g);
    } catch (Exception e) {
        return;
    }
    if (window == null)
        return;
    Client client = window.client;
    if (client == null || client == serverClient)
        return;
    Rectangle r = g.getClip().getBounds();
    if (exposed.width == 0) {
        exposed.setBounds(r);
    } else if (expcontains(r.x, r.y, r.width, r.height)) {
        return;
    } else {
        expunion(r);
    }

    event.mkExpose(window.id, r.x, r.y, r.width, r.height, 0);
    try {
        window.sendEvent(event, 1, null);
    } catch (Exception ee) {
    }
}

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

public void copyArea(XWindow dst, GC gc, int srcx, int srcy, int width, int height, int destx, int desty) {
    Graphics g = dst.getGraphics();
    if (g == null)
        return;//  w  ww  .j  a  v a2 s.  c  o  m

    if (window == dst) {
        copyArea(srcx, srcy, width, height, destx - srcx, desty - srcy);
        dst.draw(destx, desty, width, height);
        return;
    }

    Image img = window.getImage(gc, srcx, srcy, width, height);
    if (srcx == 0 && srcy == 0 && width == window.width && height == window.height) {
        dst.ddxwindow.drawImage(gc.clip_mask, img, destx, desty, width, height);
    } else {
        java.awt.Shape tmp = g.getClip();
        g.clipRect(destx, desty, width, height);
        dst.ddxwindow.drawImage(gc.clip_mask, img, destx - srcx, desty - srcy, window.width, window.height);
        if (tmp == null) {
            g.setClip(0, 0, dst.width, dst.height);
        } else {
            g.setClip(tmp);
        }
    }
    dst.draw(destx, desty, width, height);
    if (img != window.getImage()) {
        img.flush();
    }
}

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  w w .  j av a  2  s .c o m
                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:com.jcraft.weirdx.XPixmap.java

@SuppressWarnings("unused")
void copyArea(XPixmap dst, GC gc, int sx, int sy, int dx, int dy, int w, int h) {
    if ((width - sx) < w)
        w = width - sx;//  ww  w.j  a  v a 2s .  c  o m
    if ((dst.width - dx) < w)
        w = dst.width - dx;

    if ((height - sy) < h)
        h = height - sy;
    if ((dst.height - dy) < h)
        h = dst.height - dy;

    int s = sy * width + sx;
    int d = dy * dst.width + dx;

    Graphics g = dst.getGraphics();
    Image ii = getImage(gc, sx, sy, w, h);

    if (sx == 0 && sy == 0 && w == width && h == height) {
        g.drawImage(ii, dx, dy, width, height, Screen.screen[0].root.ddxwindow);
    } else {
        java.awt.Shape tmp = g.getClip();
        g.setClip(dx, dy, w, h);
        g.drawImage(ii, dx - sx, dy - sy, width, height, Screen.screen[0].root.ddxwindow);

        if (tmp == null) {
            g.setClip(0, 0, dst.width, dst.height);
        } else {
            g.setClip(tmp);
        }
    }
    dst.draw(dx, dy, w, h);

    if (ii != getImage()) {
        ii.flush();
    }
}