Example usage for java.awt Graphics clipRect

List of usage examples for java.awt Graphics clipRect

Introduction

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

Prototype

public abstract void clipRect(int x, int y, int width, int height);

Source Link

Document

Intersects the current clip with the specified rectangle.

Usage

From source file:Main.java

public void paint(Graphics g) {
    g.setColor(Color.red);
    g.drawRect(0, 0, 100, 100);
    g.clipRect(25, 25, 50, 50);
    g.drawLine(0, 100, 100, 0);
}

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:Main.java

public void paint(Graphics g) {
    g.setColor(Color.red);//from   www  .  j  av  a 2 s.  c o m
    Graphics clippedGraphics = g.create();
    clippedGraphics.drawRect(0, 0, 100, 100);
    clippedGraphics.clipRect(25, 25, 50, 50);
    clippedGraphics.drawLine(0, 0, 100, 100);
    clippedGraphics.dispose();
    clippedGraphics = null;
    g.drawLine(0, 100, 100, 0);
}

From source file:Main.java

public void paint(Graphics g) {
    g.setColor(Color.red);//from   w  w  w.ja  v a 2s.  com
    Graphics clippedGraphics = g.create();
    clippedGraphics.drawRect(0, 0, 100, 100);
    clippedGraphics.clipRect(25, 25, 50, 50);
    clippedGraphics.drawLine(0, 0, 100, 100);
    clippedGraphics.finalize();
    clippedGraphics = null;
    g.drawLine(0, 100, 100, 0);
}

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

static void reqPutImage(Client c) throws IOException {
    int foo, n;/*from   w  ww .j  a va 2  s  . com*/
    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.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;//from ww w. 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:com.jcraft.weirdx.XWindow.java

void makeBackgroundTile(int xx, int yy, int w, int h) {
    XWindow win = this;
    int i = (attr & backgroundState);
    if (i == ParentRelative) {
        win = parent;//from ww  w. j a va 2s . co  m
        if (win == null)
            return;
        i = (win.attr & backgroundState);
    }

    if (i == BackgroundPixmap && win.background.pixmap != null) {
        Image img = win.background.img;
        if (img == null)
            return;
        if (!win.ddxwindow.isVisible())
            return;
        Graphics g = null;
        java.awt.Shape tmp = null;
        if (xx != 0 || yy != 0 || w != width || h != height) {
            g = getGraphics();
            tmp = g.getClip();
            g.clipRect(xx, yy, w, h);
        }
        if (win == this) {
            ddxwindow.fillImage(img, win.background.pixmap.width, win.background.pixmap.height);
        } else {
            ddxwindow.fillImage(img, win.background.pixmap.width, win.background.pixmap.height,
                    origin.x - borderWidth, origin.y - borderWidth);
        }
        if (g != null) {
            if (tmp == null) {
                g.setClip(0, 0, width, height);
            } else {
                g.setClip(tmp);
            }
        }
    } else if (i == BackgroundPixel) {
        XColormap cmap = win.getColormap();
        ddxwindow.setBackground(cmap.getColor(win.background.pixel), xx, yy, w, h);
    }
}

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

static final void reqCopyPlane(Client c) throws IOException {
    int foo;//  w ww  .  ja  v  a 2s.co m
    XDrawable dsrc = null, ddst = null;
    InputOutput io = c.client;
    foo = io.readInt();
    dsrc = c.lookupDrawable(foo);
    if (dsrc == null) {
        c.errorValue = foo;
        c.errorReason = 9; // Drawable
    }

    int dest = io.readInt();
    ddst = c.lookupDrawable(dest);
    if (ddst == null && c.errorReason == 0) {
        c.errorValue = dest;
        c.errorReason = 9; // Drawable
    }
    foo = io.readInt();
    GC gc = c.lookupGC(foo);
    if (gc == null && c.errorReason == 0) {
        c.errorValue = foo;
        c.errorReason = 13; // BadGC;
    }
    int sx, sy, dx, dy, width, height;

    sx = (short) io.readShort();
    sy = (short) io.readShort();

    int destx, desty;

    destx = (short) io.readShort();
    dx = destx - sx;

    desty = (short) io.readShort();
    dy = desty - sy;

    width = io.readShort();
    height = io.readShort();

    int bplane = io.readInt();
    c.length -= 8;

    if (c.errorReason != 0) {
        return;
    }

    Graphics g = ddst.getGraphics();
    if (((dsrc instanceof XWindow) && !((XWindow) dsrc).ddxwindow.isVisible())) {
        g = null;
    }

    if (dsrc.width <= sx || dsrc.height <= sy || (sx + width) <= 0 || (sy + height) <= 0 || (destx + width) <= 0
            || (desty + height) <= 0) {
        g = null;
    }

    if (g != null) {
        if (dsrc instanceof XWindow) {
            if (ddst instanceof XWindow) {
                ((XWindow) dsrc).ddxwindow.copyArea(((XWindow) ddst), gc, sx, sy, width, height, destx, desty);
            }
        } else {
            Image img = null;
            if (ddst instanceof XPixmap) {
                ((XPixmap) dsrc).copyPlane((XPixmap) ddst, gc, sx, sy, destx, desty, width, height);
            } else {
                img = ((XPixmap) dsrc).getImage((XWindow) ddst, gc, sx, sy, width, height);
                XWindow wdst = (XWindow) ddst;

                if (sx == 0 && sy == 0 && width == dsrc.width && height == dsrc.height) {
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx, desty, width, height);
                } else {
                    java.awt.Shape tmp = g.getClip();
                    g.clipRect(destx, desty, width, height);
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx - sx, desty - sy, dsrc.width,
                            dsrc.height);
                    if (tmp == null) {
                        g.setClip(0, 0, wdst.width, wdst.height);
                    } else {
                        g.setClip(tmp);
                    }
                }
                wdst.draw(destx, desty, width, height);
                if (img != ((XPixmap) dsrc).getImage()) {
                    img.flush();
                }
            }
        }
    }
}

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

@SuppressWarnings("unused")
static void reqPolyFillRectangle(Client c, XDrawable d, GC gc) throws IOException {
    int n = c.length;
    //int foo;/*w  w w .j ava  2 s .c om*/

    Graphics graphics = d.getGraphics(gc, GC.GCFunction | GC.GCSubwindowMode);
    if (graphics == null) {
        c.client.readPad(n * 4);
        return;
    }

    if (gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap && (d instanceof XPixmap)
            && ((XPixmap) d).data != null) {
        XPixmap p = (XPixmap) d;
        byte[] data = p.data;

        XPixmap cpixmap = (XPixmap) (gc.clip_mask.getMask());

        short x, y;
        int ww, hh;

        byte f = (byte) gc.fgPixel;

        while (n != 0) {
            x = (short) c.client.readShort();
            y = (short) c.client.readShort();
            ww = c.client.readShort();
            hh = c.client.readShort();
            n -= 2;

            if (x < 0) {
                ww += x;
                x = 0;
            }
            if (y < 0) {
                hh += y;
                y = 0;
            }
            if (d.width <= x || d.height <= y)
                continue;
            if (ww <= 0 || hh <= 0)
                continue;
            //      if((x+ww)<=0 || (y+hh)<=0) continue;

            if (ww > cpixmap.width)
                ww = cpixmap.width;
            if (hh > cpixmap.height)
                hh = cpixmap.height;
            if (ww > d.width)
                ww = d.width;
            if (hh > d.height)
                hh = d.height;
            for (int i = 0; i < hh; i++) {
                for (int j = 0; j < ww; j++) {
                    if (p.data[(i + y) * p.width + x + j] == 0) {
                        p.data[(i + y) * p.width + x + j] = f;
                    }
                }
            }
        }
        return;
    }

    /*
        if(gc.clip_mask!=null &&
           (d instanceof Pixmap) &&
           gc.clip_mask.width==d.width &&
           gc.clip_mask.height==d.height &&
           ((Pixmap)d).data!=null){
            
          Pixmap p=(Pixmap)d;
            
    //      for(int i=0;i<p.height;i++){
    //   for(int j=0;j<p.width;j++){
    //     System.out.print(p.data[i*p.width+j]+" ");
    //   }
    //   System.out.println("");
    //      }
            
          byte f=(byte)gc.fgPixel;
          for(int i=0;i<gc.clip_mask.height;i++){
       for(int j=0;j<gc.clip_mask.width;j++){
         if(p.data[i*gc.clip_mask.width+j]==0){
           p.data[i*gc.clip_mask.width+j]=f;
         }
       }
          }
            
    //      for(int i=0;i<((Pixmap)d).height;i++){
    //   for(int j=0;j<((Pixmap)d).width;j++){
    //     System.out.print(((Pixmap)d).data[i*((Pixmap)d).width+j]+" ");
    //   }
    //   System.out.println("");
    //      }
            
          while(n!=0){
       c.client.readPad(8); n-=2;
          }
          return;
        }
    */

    if (gc.clip_mask != null && gc.clip_mask instanceof ClipRectangles) {
        java.awt.Rectangle rec = (Rectangle) (gc.clip_mask.getMask());
        if (rec == null) {
            while (n > 0) {
                c.client.readPad(8);
                n -= 2;
            }
            return;
        }
    }

    XPixmap p = null;
    if (((gc.attr & GC.fillStyle) == GC.FillOpaqueStippled) || ((gc.attr & GC.fillStyle) == GC.FillStippled)) {
        p = gc.stipple;
    }
    if (((gc.attr & GC.fillStyle) == GC.FillTiled)) {
        p = gc.tile.pixmap;
    }

    short x, y;
    int ww, hh;

    int sx = d.width;
    int sy = d.height;
    int lx = 0;
    int ly = 0;

    while (n != 0) {
        x = (short) c.client.readShort();
        y = (short) c.client.readShort();
        ww = c.client.readShort();
        hh = c.client.readShort();

        n -= 2;

        if (x < 0) {
            ww += x;
            x = 0;
        }
        if (y < 0) {
            hh += y;
            y = 0;
        }
        if (d.width <= x || d.height <= y)
            continue;
        if (ww <= 0 || hh <= 0)
            continue;

        if (x < sx)
            sx = x;
        ;
        if (lx < x + ww)
            lx = x + ww;
        if (y < sy)
            sy = y;
        if (ly < y + hh)
            ly = y + hh;

        if (((gc.attr & GC.fillStyle) == GC.FillOpaqueStippled) || ((gc.attr & GC.fillStyle) == GC.FillStippled)
                || ((gc.attr & GC.fillStyle) == GC.FillTiled)) {

            java.awt.Shape tmp = null;
            ww += x;
            hh += y;
            if ((p.width > ww - x) || (p.height > hh - y)) {
                tmp = graphics.getClip();
                graphics.clipRect(x, y, ww - x, hh - y);
            }
            graphics.drawImage(p.img, x, y, Screen.screen[0].root.ddxwindow);
            if (tmp != null)
                graphics.setClip(tmp);
            for (int i = x + p.width; i < ww; i += p.width) {
                int www = p.width;
                int hhh = p.height;
                if (i + www > ww)
                    www = ww - i;
                if (y + hhh >= hh)
                    hhh = hh - y;
                graphics.copyArea(x, y, www, hhh, i - x, 0);
            }
            for (int j = y + p.height; j < hh; j += p.height) {
                int hhh = p.height;
                if (j + hhh >= hh)
                    hhh = hh - j;
                graphics.copyArea(x, y, ww - x, hhh, 0, j - y);
            }
        } else {
            graphics.fillRect(x, y, ww, hh);
        }
    }

    if (sx < 0)
        sx = 0;
    if (sy < 0)
        sy = 0;
    if (d instanceof XWindow) {
        ((XWindow) d).draw(sx, sy, lx - sx + 2, ly - sy + 2);
    }

    if (gc.function == GC.GXxor || gc.function == GC.GXinvert) {
        graphics.setPaintMode();
    }

    if (gc.clip_mask != null && gc.clip_mask instanceof ClipRectangles) {
        d.restoreClip();
    }
}