Example usage for java.awt Graphics drawImage

List of usage examples for java.awt Graphics drawImage

Introduction

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

Prototype

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as is currently available.

Usage

From source file:Main.java

public static void drawChar(char c, int x, int y, Graphics g) {
    BufferedImage img = null;// w ww .ja v  a  2  s  .  c om
    switch (c) {
    case ' ':
        img = GUI_GLYPH_CHARS[0];
        break;
    case '\u0263':
        img = GUI_GLYPH_CHARS[1];
        break;
    case '!':
        img = GUI_GLYPH_CHARS[33];
        break;
    case '"':
        img = GUI_GLYPH_CHARS[34];
        break;
    case '#':
        img = GUI_GLYPH_CHARS[35];
        break;
    case '$':
        img = GUI_GLYPH_CHARS[36];
        break;
    case '%':
        img = GUI_GLYPH_CHARS[37];
        break;
    case '&':
        img = GUI_GLYPH_CHARS[38];
        break;
    case '\'':
        img = GUI_GLYPH_CHARS[39];
        break;
    case '(':
        img = GUI_GLYPH_CHARS[40];
        break;
    case ')':
        img = GUI_GLYPH_CHARS[41];
        break;
    case '*':
        img = GUI_GLYPH_CHARS[42];
        break;
    case '+':
        img = GUI_GLYPH_CHARS[43];
        break;
    case ',':
        img = GUI_GLYPH_CHARS[44];
        break;
    case '-':
        img = GUI_GLYPH_CHARS[45];
        break;
    case '.':
        img = GUI_GLYPH_CHARS[46];
        break;
    case '/':
        img = GUI_GLYPH_CHARS[47];
        break;
    case '0':
        img = GUI_GLYPH_CHARS[48];
        break;
    case '1':
        img = GUI_GLYPH_CHARS[49];
        break;
    case '2':
        img = GUI_GLYPH_CHARS[50];
        break;
    case '3':
        img = GUI_GLYPH_CHARS[51];
        break;
    case '4':
        img = GUI_GLYPH_CHARS[52];
        break;
    case '5':
        img = GUI_GLYPH_CHARS[53];
        break;
    case '6':
        img = GUI_GLYPH_CHARS[54];
        break;
    case '7':
        img = GUI_GLYPH_CHARS[55];
        break;
    case '8':
        img = GUI_GLYPH_CHARS[56];
        break;
    case '9':
        img = GUI_GLYPH_CHARS[57];
        break;
    case ':':
        img = GUI_GLYPH_CHARS[58];
        break;
    }
    g.drawImage(img, x, y, null);
}

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 .ja  va2 s  .  c om
    }

    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.DDXWindowImp.java

public void paintComponent(Graphics g) {
    /*    if(window.clss==InputOnly){
          super.paintComponents(g);//from www . j  av a2s .  c om
          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.XPixmap.java

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

From source file:org.jimcat.gui.borders.RoundedShadowBorder.java

/**
 * /*from  w  w w .  j  a  v a 2  s . c om*/
 * paint this type of border
 * 
 * @see org.jimcat.gui.borders.RoundedBorder#paintBorder(java.awt.Component,
 *      java.awt.Graphics, int, int, int, int)
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Color borderColor = c.getBackground();

    // check if old image is still useable
    if (imageBuffer == null || width != lastWidth || height != lastHeight
            || !ObjectUtils.equals(borderColor, lastColor)) {
        // update image buffer
        imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        lastHeight = height;
        lastWidth = width;
        lastColor = borderColor;

        int shadow_width = SHADOW_WIDTH * 2;

        int xb = Math.max(x + SHADOW_WIDTH - SHADOW_DISPLACEMENT.x, x);
        int yb = Math.max(y + SHADOW_WIDTH - SHADOW_DISPLACEMENT.y, y);

        int xs = Math.max(x, x + SHADOW_DISPLACEMENT.x);
        int ys = Math.max(y, y + SHADOW_DISPLACEMENT.y);

        BufferedImage border = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics imgG = border.createGraphics();
        super.paintBorder(c, imgG, xb, yb, width - shadow_width - Math.abs(SHADOW_DISPLACEMENT.x),
                height - shadow_width - Math.abs(SHADOW_DISPLACEMENT.y));
        imgG.dispose();

        BufferedImage shadow = shadowRenderer.createShadow(border);

        Graphics2D g2 = (Graphics2D) imageBuffer.getGraphics();
        g2.drawImage(shadow, xs, ys, null);
        g2.drawImage(border, xb, yb, null);
        g2.dispose();
    }
    g.drawImage(imageBuffer, x, y, null);
}

From source file:edu.ku.brc.af.core.NavBox.java

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

    Dimension dim = getSize();//ww w.j av  a 2s  .com

    FontMetrics fm = g.getFontMetrics();
    int strW = fm.stringWidth(name);

    int x = (dim.width - strW) / 2;
    Insets ins = getBorder().getBorderInsets(this);
    int y = 2 + fm.getAscent();

    int lineW = dim.width - ins.left - ins.right;
    x = ins.left;
    int txtY = y;
    y += 3;

    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(x, y, x + lineW, y);
    y++;
    minHeight = y;

    x++;
    g.setColor(Color.LIGHT_GRAY.darker());
    g.drawLine(x, y, x + lineW, y);

    ((Graphics2D) g).setRenderingHints(UIHelper.createTextRenderingHints());
    g.setColor(Color.BLUE.darker());
    g.drawString(name, x, txtY);

    if (collapsableIconOpen != null) {
        if (iconRect == null) {
            iconRect = getBounds();
            iconRect.x = iconRect.width - collapsableIconOpen.getIconWidth();
            iconRect.y = 0;
        }
        g.drawImage(icon.getImage(), iconRect.x, iconRect.y, null);
    }
}

From source file:net.geoprism.dashboard.DashboardMap.java

/**
 * Builds a combined image layer of all the layers in a saved map.
 * /*from  w  w w  .  jav  a  2 s.c om*/
 * @mapWidth
 * @mapHeight
 * @orderedLayers
 * @mapBounds - expects json object {"bottom":"VALUE", "top":"VALUE", "left":"VALUE", "right":"VALUE"}
 */
public BufferedImage getLayersExportCanvas(int mapWidth, int mapHeight, DashboardLayer[] orderedLayers,
        String mapBounds) {
    String bottom;
    String top;
    String right;
    String left;
    String processingFormat = "png"; // needed to allow transparency on each overlay before combining to a single
                                     // map/format
    Graphics mapBaseGraphic = null;
    BufferedImage base = null;

    try {
        base = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB);
        mapBaseGraphic = base.getGraphics();
        mapBaseGraphic.drawImage(base, 0, 0, null);

        // Get bounds of the map
        try {
            JSONObject mapBoundsObj = new JSONObject(mapBounds);
            bottom = mapBoundsObj.getString("bottom");
            top = mapBoundsObj.getString("top");
            right = mapBoundsObj.getString("right");
            left = mapBoundsObj.getString("left");
        } catch (JSONException e) {
            String error = "Could not parse map bounds.";
            throw new ProgrammingErrorException(error, e);
        }

        // Generates map overlays and combines them into a single map image
        for (DashboardLayer layer : orderedLayers) {
            // if (layer instanceof DashboardThematicLayer)
            // {
            Graphics2D newOverlayBaseGraphic = null;
            Graphics2D mapLayerGraphic2d = null;

            String layersString = GeoserverProperties.getWorkspace() + ":" + layer.getViewName();

            StringBuffer requestURL = new StringBuffer();
            requestURL.append(GeoserverProperties.getLocalPath() + "/wms?");
            requestURL.append("LAYERS=" + layersString);
            requestURL.append("&");
            requestURL.append("STYLES="); // there are no geoserver styles being added. sld's are used instead
            requestURL.append("&");
            requestURL.append("SRS=EPSG%3A4326");
            requestURL.append("&");
            requestURL.append("TRANSPARENT=true");
            requestURL.append("&");
            requestURL.append("ISBASELAYER=false"); // in the browser the baselayer prop is set for the 1st layer in the
                                                    // map.
            requestURL.append("&");
            requestURL.append(
                    "SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage");
            requestURL.append("&");
            requestURL.append("FORMAT=image%2F" + processingFormat);
            requestURL.append("&");
            requestURL.append("BBOX=" + left + "," + bottom + "," + right + "," + top);
            requestURL.append("&");
            requestURL.append("WIDTH=" + Integer.toString(mapWidth));
            requestURL.append("&");
            requestURL.append("HEIGHT=" + Integer.toString(mapHeight));

            try {
                BufferedImage layerImg = this.getImageFromGeoserver(requestURL.toString());
                BufferedImage newOverlayBase = new BufferedImage(mapWidth, mapHeight,
                        BufferedImage.TYPE_INT_ARGB);

                newOverlayBaseGraphic = newOverlayBase.createGraphics();

                // Add transparency to the layerGraphic
                // This is set in JavaScript in the app so we are replicating browser side transparency settings that are
                // applied to the whole layer
                AlphaComposite thisLayerComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                        this.getLayerOpacity(layer));
                mapLayerGraphic2d = layerImg.createGraphics();
                newOverlayBaseGraphic.setComposite(thisLayerComposite);

                // Add the current layerGraphic to the base image
                newOverlayBaseGraphic.drawImage(layerImg, 0, 0, null);
                mapBaseGraphic.drawImage(newOverlayBase, 0, 0, null);

            } finally {
                if (newOverlayBaseGraphic != null) {
                    newOverlayBaseGraphic.dispose();
                }

                if (mapLayerGraphic2d != null) {
                    mapLayerGraphic2d.dispose();
                }
            }
            // }
        }
    } finally {
        mapBaseGraphic.dispose();
    }

    return base;
}

From source file:org.apache.cayenne.swing.components.textpane.JCayenneTextPane.java

public void paint(Graphics g) {

    super.paint(g);

    int start = getStartPositionInDocument();
    int end = getEndPositionInDocument();
    // end pos in doc

    // translate offsets to lines
    Document doc = pane.getDocument();
    int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
    int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;

    int fontHeight = g.getFontMetrics(pane.getFont()).getHeight();
    int fontDesc = g.getFontMetrics(pane.getFont()).getDescent();
    int starting_y = -1;

    try {//from  w  ww . j a  va  2 s.  co m
        if (pane.modelToView(start) == null) {
            starting_y = -1;
        } else {
            starting_y = pane.modelToView(start).y - scrollPane.getViewport().getViewPosition().y + fontHeight
                    - fontDesc;
        }
    } catch (Exception e1) {
        logObj.warn("Error: ", e1);
    }

    for (int line = startline, y = starting_y; line <= endline; y += fontHeight, line++) {
        Color color = g.getColor();

        if (line - 1 == doc.getDefaultRootElement().getElementIndex(pane.getCaretPosition())) {
            g.setColor(new Color(224, 224, 255));
            g.fillRect(0, y - fontHeight + 3, 30, fontHeight + 1);
        }

        if (imageError) {
            Image img = ModelerUtil.buildIcon("error.gif").getImage();
            g.drawImage(img, 0, endYPositionToolTip, this);
        }

        g.setColor(color);
    }

}

From source file:org.yccheok.jstock.gui.Utils.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from  ww  w.java  2 s .co  m

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}