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:edu.ku.brc.af.ui.forms.validation.ValFormattedTextFieldSingle.java

@Override
public void paint(Graphics g) {
    super.paint(g);

    String text = getText();//w  ww .ja  va2  s .c o m

    //System.err.println("isViewOnly "+isViewOnly+"  isEnabled() "+isEnabled()+ "  ["+text+"]  "+(text.length() < bgStr.length()));
    if (!isViewOnly && needsUpdating && isEnabled() && text != null && text.length() < bgStr.length()) {
        FontMetrics fm = g.getFontMetrics();
        int w = fm.stringWidth(text);
        pnt = new Point(inner.left + w, inner.top + fm.getAscent());

        Rectangle r = g.getClipBounds();
        Dimension s = getSize();
        Insets i2 = getBorder().getBorderInsets(this);
        int x = i2.left - 1;
        int y = i2.top - 1;
        //int ww = s.width - i2.right + 1;
        int hh = s.height - i2.bottom + 1;

        String str = bgStr.substring(text.length(), bgStr.length());
        int bgW = fm.stringWidth(str);

        g.setClip(x + w, y, Math.min(x + bgW, g.getClipBounds().width - x), hh);

        g.setColor(textColor);
        g.drawString(str, pnt.x, pnt.y);

        g.setClip(r.x, r.y, r.width, r.height); // reset clip
    }

    //System.out.println(hashCode() + " isNew: " +isNew+"  valState: "+valState+"    isEnabled: "+isEnabled());
    // 3/2/09 - rods - removing !isNew from the condition
    //if (!isNew && valState == UIValidatable.ErrorType.Error && isEnabled())
    if (valState == UIValidatable.ErrorType.Error && isEnabled()) {
        UIHelper.drawRoundedRect((Graphics2D) g, isNew ? new Color(249, 249, 0) : valTextColor.getColor(),
                getSize(), 1);
    } else if (valState == UIValidatable.ErrorType.Incomplete && isEnabled()) {
        UIHelper.drawRoundedRect((Graphics2D) g, new Color(249, 249, 0), getSize(), 1);
    }
}

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;/*  ww w . j a va  2s  .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.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;/*from w w w . j av a2s. co  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();
    }
}

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

public final Graphics getGraphics(GC gc, int mask) {
    if (!isVisible()) {
        return null;
    }/*from  w w w  .j a v a 2  s.com*/
    if (offg == null)
        allocImage();
    Graphics graphics = offg;
    if ((mask & GC.GCSubwindowMode) != 0 && (gc.attr & GC.IncludeInferiors) != 0) {
        graphics = getGraphics();
        window.currentGC = null;
    } else {
        if (gc == window.currentGC && gc.time == window.gctime && (mask & ~window.gmask) == 0) {
            //System.out.println("DDXWindow skip");
            return graphics;
        }
        window.gctime = gc.time;
        window.currentGC = gc;
        window.gmask = mask;
    }

    if (gc.clip_mask != null && gc.clip_mask instanceof ClipRectangles) {
        java.awt.Rectangle rec = (Rectangle) (gc.clip_mask.getMask());
        if (rec != null) {
            graphics = offg;
        }

        if (rec == null
                || (rec.x == 0 && rec.y == 0 && rec.width == window.width && rec.height == window.height)) {
            //   return graphics;
        } else {
            graphics.setClip(rec.x, rec.y, rec.width, rec.height);
        }
    }

    if ((mask & GC.GCFunction) != 0) {
        Color color = window.getColormap().getColor(gc.fgPixel);
        if (gc.function == GC.GXxor) {
            window.gmask &= ~GC.GCFunction;
            graphics.setXORMode(new Color((color.getRGB() ^ graphics.getColor().getRGB()) & 0xffffff));
        } else if (gc.function == GC.GXinvert) {
            window.gmask &= ~GC.GCFunction;
            graphics.setXORMode(window.screen.defaultColormap.getColor(window.background.pixel));
        } else {
            graphics.setColor(color);
        }
    }

    if ((mask & GC.GCFont) != 0) {
        XFont font = gc.font;
        graphics.setFont(font.getFont());
    }

    if ((mask & GC.GCLineWidth) != 0 || (mask & GC.GCLineStyle) != 0 || (mask & GC.GCCapStyle) != 0
            || (mask & GC.GCJoinStyle) != 0) {
    }
    return graphics;
}

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

Graphics getGraphics(GC gc, int mask) {
    Graphics graphics = imgg;
    if (gc == currentGC && gc.time == gctime && (mask & ~gmask) == 0) {
        return graphics;
    }//from ww w. j a  v  a  2s .  c o  m
    gctime = gc.time;
    currentGC = gc;
    gmask = mask;
    if (gc.clip_mask != null && gc.clip_mask instanceof ClipRectangles) {
        java.awt.Rectangle rec = (Rectangle) (gc.clip_mask.getMask());
        if (rec == null) {
            return graphics;
        }
        graphics.setClip(rec.x, rec.y, rec.width, rec.height);
    }
    if ((mask & GC.GCFunction) != 0) {
        Color color = getColormap().getColor(gc.fgPixel);
        if (gc.function == GC.GXxor) {
            gmask &= ~GC.GCFunction;
            graphics.setXORMode(new Color((color.getRGB() ^ graphics.getColor().getRGB()) & 0xffffff));
        } else if (gc.function == GC.GXinvert) {
            //System.out.println("Pimxpa: GXinvert");
        } else {
            graphics.setColor(color);
        }
    }
    if ((mask & GC.GCFont) != 0) {
        XFont font = gc.font;
        graphics.setFont(font.getFont());
    }
    if ((mask & GC.GCLineWidth) != 0 || (mask & GC.GCLineStyle) != 0 || (mask & GC.GCCapStyle) != 0
            || (mask & GC.GCJoinStyle) != 0) {
    }
    return graphics;
}

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

static final void reqCopyPlane(Client c) throws IOException {
    int foo;/*from w w w  .  j  av a2  s  . c o 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.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.  jav a 2  s.c om*/
        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 reqCopyArea(Client c) throws IOException {
    int foo;/*from   w  w w. ja v  a2s  .c om*/
    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; // BadDrawable;
    }

    int dest = io.readInt();
    ddst = c.lookupDrawable(dest);
    if (ddst == null && c.errorReason == 0) {
        c.errorValue = dest;
        c.errorReason = 9; // BadDrawable;
    }

    foo = io.readInt();

    GC gc = c.lookupGC(foo);

    if (gc == null && c.errorReason == 0) {
        c.errorValue = foo;
        c.errorReason = 13; // BadGC;
    }

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

    int destx, desty;
    destx = (short) io.readShort();
    desty = (short) io.readShort();

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

    c.length -= 7;

    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;
    }

    /*
    LOG.info("copyArea: "+dsrc+" sx="+sx+",sy="+sy+
                          ", w="+width+", h="+height+" "+
                           ddst+" destx="+destx+",desty="+desty);
    */

    if (g != null) {
        if (dsrc instanceof XWindow) {

            if (sx < 0) {
                sx = 0;
            }
            if (sy < 0) {
                sy = 0;
            }
            if (destx < 0) {
                destx = 0;
            }
            if (desty < 0) {
                desty = 0;
            }

            if (ddst instanceof XWindow) {
                ((XWindow) dsrc).ddxwindow.copyArea(((XWindow) ddst), gc, sx, sy, width, height, destx, desty);
            }
            //      else{
            //     ((Window)dsrc).ddxwindow.
            //       copyArea((Pixmap)ddst, gc,
            //           sx, sy, destx, desty, width, height);
            //   }
        } else {

            if (ddst instanceof XPixmap) {
                if (sx < 0) {
                    sx = 0;
                }
                if (sy < 0) {
                    sy = 0;
                }
                if (destx < 0) {
                    destx = 0;
                }
                if (desty < 0) {
                    desty = 0;
                }

                ((XPixmap) dsrc).copyArea((XPixmap) ddst, gc, sx, sy, destx, desty, width, height);
            } else {
                Image 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();
                    if (destx >= 0 && desty >= 0) {
                        g.clipRect(destx, desty, width, height);
                    } else {
                        g.clipRect((destx < 0 ? 0 : destx), (desty < 0 ? 0 : desty),
                                (destx < 0 ? width + destx : width), (desty < 0 ? height + desty : 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();
                }
            }
        }
    }

    if ((gc.attr & GC.graphicsExposures) != 0) {
        c.cevent.mkNoExposure(dest, 0, 62);
        c.sendEvent(c.cevent, 1, 0, Event.NoEventMask, null);
    }
}