Example usage for java.awt Graphics copyArea

List of usage examples for java.awt Graphics copyArea

Introduction

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

Prototype

public abstract void copyArea(int x, int y, int width, int height, int dx, int dy);

Source Link

Document

Copies an area of the component by a distance specified by dx and dy .

Usage

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  .  ja v  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.Draw.java

@SuppressWarnings("unused")
static void reqPolyFillRectangle(Client c, XDrawable d, GC gc) throws IOException {
    int n = c.length;
    //int foo;//from w  w  w . ja v  a2s . co m

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