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:uk.co.modularaudio.service.guicompfactory.impl.RealComponentBack.java

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

    final int imageWidth = backBufferedImage.getWidth();
    final int imageHeight = backBufferedImage.getHeight();
    final int width = getWidth();
    // Hack since I duffed up the heights due to border.
    final int height = getHeight() + 8;

    if (imageWidth != width || imageHeight != height) {
        final StringBuilder sb = new StringBuilder("Component ");
        sb.append(rc.getInstance().getDefinition().getId());
        sb.append(" has badly sized back image: (");
        sb.append(imageWidth);//from   ww  w  .  j av  a 2 s .  c om
        sb.append(", ");
        sb.append(imageHeight);
        sb.append(") - component size(");
        sb.append(width);
        sb.append(", ");
        sb.append(height);
        sb.append(")");
        final String msg = sb.toString();
        log.warn(msg);
    }

    g.drawImage(backBufferedImage, 0, 0, null);
}

From source file:BufferedAnimate.java

public void paint(Graphics g) {
    if ((oldSize == null) || (oldSize != getSize())) {
        oldSize = getSize();/*from  ww w.  jav  a2s.c  o  m*/
        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    }
    if (insets == null) {
        insets = getInsets();
    }
    // Calculate each time in case of resize
    int x = insets.left;
    int y = insets.top;
    int width = getWidth() - insets.left - insets.right;
    int height = getHeight() - insets.top - insets.bottom;
    int start = 0;
    int steps = colors.length;
    int stepSize = 360 / steps;
    synchronized (colors) {
        Graphics bufferG = buffer.getGraphics();
        bufferG.setColor(Color.WHITE);
        bufferG.fillRect(x, y, width, height);
        for (int i = 0; i < steps; i++) {
            bufferG.setColor(colors[i]);
            bufferG.fillArc(x, y, width, height, start, stepSize);
            start += stepSize;
        }
    }
    g.drawImage(buffer, 0, 0, this);
}

From source file:CustomAlphaTest.java

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

    if (m_Alpha != null) {
        drawGraph();//  w w w .j  a  v  a  2 s.c o m
        g.drawImage(m_Image, m_nInsetX, m_nInsetY, this);
    }
}

From source file:org.deegree.services.wps.provider.jrxml.contentprovider.map.MapContentProvider.java

private Object prepareMap(List<OrderedDatasource<?>> datasources, String type, int originalWidth,
        int originalHeight, int width, int height, Envelope bbox, int resolution) throws ProcessletException {
    if ("net.sf.jasperreports.engine.JRRenderable".equals(type)) {
        return new MapRenderable(datasources, bbox, resolution);
    } else {//from  w  ww.  ja v  a  2s.c  o m
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        for (OrderedDatasource<?> datasource : datasources) {
            BufferedImage image = datasource.getImage(width, height, bbox);
            if (image != null) {
                g.drawImage(image, 0, 0, null);
            }
        }
        g.dispose();
        return convertImageToReportFormat(type, bi);
    }
}

From source file:org.deegree.portal.standard.wms.control.DynLegendListener.java

/**
 * In case the legend can not be obtained from the OGCLayer, this method is called to create a dummy legend image
 * plus the legend title//from w  w w  . ja  va 2  s.  c  o  m
 * 
 * @param layerName
 * @return BufferedImage holding the created dummy legend
 */
private BufferedImage createMissingLegend(String layerName) {
    LOG.logDebug("URL is null. Drawing the image from a missingImage variable in init params");
    BufferedImage missingLegend = new BufferedImage(80, 15, BufferedImage.TYPE_INT_RGB);
    Graphics g = missingLegend.getGraphics();
    Rectangle2D rect = g.getFontMetrics().getStringBounds(layerName, g);
    g.dispose();
    missingLegend = new BufferedImage(rect.getBounds().width + 80, missingImg.getHeight() + 15,
            BufferedImage.TYPE_INT_ARGB);
    g = missingLegend.getGraphics();
    g.drawImage(missingImg, 0, 0, null);
    g.setColor(Color.RED);
    if (useLayerTitle) {
        g.drawString(layerName, missingImg.getWidth() + 5,
                missingImg.getHeight() / 2 + g.getFont().getSize() / 2);
    }
    g.dispose();

    return missingLegend;
}

From source file:Animator.java

/**
 * Paint the current frame.//from w  ww .  j  av  a2 s . c  o m
 */
public void paint(Graphics g) {
    if (error || !loaded) {
        if (startUpImage != null) {
            if (tracker.checkID(STARTUP_ID)) {
                g.drawImage(startUpImage, 0, 0, this);
            }
        } else {
            if (backgroundImage != null) {
                if (tracker.checkID(BACKGROUND_ID)) {
                    g.drawImage(backgroundImage, 0, 0, this);
                }
            } else {
                g.clearRect(0, 0, maxWidth, maxHeight);
            }
        }
    } else {
        if ((images != null) && (images.size() > 0)) {
            if (frameNum < images.size()) {
                if (backgroundImage == null) {
                    offScrGC.fillRect(0, 0, maxWidth, maxHeight);
                } else {
                    offScrGC.drawImage(backgroundImage, 0, 0, this);
                }

                Image image = (Image) images.elementAt(frameNum);
                Point pos = null;
                if (positions != null) {
                    pos = (Point) positions.get(frameNumKey);
                }
                if (pos != null) {
                    xPos = pos.x;
                    yPos = pos.y;
                }
                offScrGC.drawImage(image, xPos, yPos, this);
                g.drawImage(offScrImage, 0, 0, this);
            } else {
                // no more animation, but need to draw something
                dbg("No more animation; drawing last image.");
                if (backgroundImage == null) {
                    g.fillRect(0, 0, maxWidth, maxHeight);
                } else {
                    g.drawImage(backgroundImage, 0, 0, this);
                }
                g.drawImage((Image) images.lastElement(), 0, 0, this);
            }
        }
    }
}

From source file:lucee.runtime.img.Image.java

public static BufferedImage toBufferedImage(java.awt.Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//  w  w w  .  j a va  2s .com

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

From source file:ScaleTest_2008.java

/**
 * Paints the test image that will be downscaled and timed by the various
 * scaling methods. A different image is rendered into each of the four
 * quadrants of this image: RGB stripes, a picture, vector art, and 
 * a black and white grid.//from   ww  w . jav  a 2s.c om
 */
private void paintOriginalImage() {
    Graphics g = originalImage.getGraphics();
    // Erase to black
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, FULL_SIZE, FULL_SIZE);

    // RGB quadrant
    for (int i = 0; i < QUAD_SIZE; i += 3) {
        int x = i;
        g.setColor(Color.RED);
        g.drawLine(x, 0, x, QUAD_SIZE);
        x++;
        g.setColor(Color.GREEN);
        g.drawLine(x, 0, x, QUAD_SIZE);
        x++;
        g.setColor(Color.BLUE);
        g.drawLine(x, 0, x, QUAD_SIZE);
    }

    // Picture quadrant
    try {
        URL url = getClass().getResource("BBGrayscale.png");
        BufferedImage picture = ImageIO.read(url);
        // Center picture in quadrant area
        int xDiff = QUAD_SIZE - picture.getWidth();
        int yDiff = QUAD_SIZE - picture.getHeight();
        g.drawImage(picture, QUAD_SIZE + xDiff / 2, yDiff / 2, null);
    } catch (Exception e) {
        System.out.println("Problem reading image file: " + e);
    }

    // Vector drawing quadrant
    g.setColor(Color.WHITE);
    g.fillRect(0, QUAD_SIZE, QUAD_SIZE, QUAD_SIZE);
    g.setColor(Color.BLACK);
    g.drawOval(2, QUAD_SIZE + 2, QUAD_SIZE - 4, QUAD_SIZE - 4);
    g.drawArc(20, QUAD_SIZE + 20, (QUAD_SIZE - 40), QUAD_SIZE - 40, 190, 160);
    int eyeSize = 7;
    int eyePos = 30 - (eyeSize / 2);
    g.fillOval(eyePos, QUAD_SIZE + eyePos, eyeSize, eyeSize);
    g.fillOval(QUAD_SIZE - eyePos - eyeSize, QUAD_SIZE + eyePos, eyeSize, eyeSize);

    // B&W grid
    g.setColor(Color.WHITE);
    g.fillRect(QUAD_SIZE + 1, QUAD_SIZE + 1, QUAD_SIZE, QUAD_SIZE);
    g.setColor(Color.BLACK);
    for (int i = 0; i < QUAD_SIZE; i += 4) {
        int pos = QUAD_SIZE + i;
        g.drawLine(pos, QUAD_SIZE + 1, pos, FULL_SIZE);
        g.drawLine(QUAD_SIZE + 1, pos, FULL_SIZE, pos);
    }

    originalImagePainted = true;
}

From source file:org.deegree.portal.standard.wms.control.DynLegendListener.java

/**
 * Draws the given symbol to the given image
 * /*from www  .j av a 2  s . c  o  m*/
 * @param map
 *            Hashmap holding the properties of the legend
 * @param bi
 *            image of the legend
 * @param color
 *            color to fill the graphic
 * @return The drawn BufferedImage
 */
private BufferedImage drawSymbolsToBI(HashMap<String, Object> map, BufferedImage bi, Color color) {

    Graphics g = bi.getGraphics();
    g.setColor(color);
    g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

    String[] layers = (String[]) map.get("NAMES");
    String[] titles = (String[]) map.get("TITLES");
    BufferedImage[] legs = (BufferedImage[]) map.get("IMAGES");
    int h = topMargin;
    for (int i = layers.length - 1; i >= 0; i--) {
        g.drawImage(legs[i], leftMargin, h, null);
        g.setColor(Color.BLACK);
        // just draw title if the flag has been set in listener configuration,
        // the legend image is less than a defined value and a legend image
        // (not missing) has been accessed
        if (useLayerTitle && legs[i].getHeight() < maxNNLegendSize && !missing.contains(layers[i])) {
            g.drawString(titles[i], leftMargin + legs[i].getWidth() + 10,
                    h + (int) (legs[i].getHeight() / 1.2));
        }
        h += legs[i].getHeight() + 5;
        if (separator != null && i > 0) {
            g.drawImage(separator, leftMargin, h, null);
            h += separator.getHeight() + 5;
        }
    }
    g.dispose();
    return bi;
}

From source file:net.fenyo.gnetwatch.GUI.BasicComponent.java

/**
 * Repaints the component using the backing store.
 * @param g graphics context.//from  w  w  w.  j a va  2  s . co m
 * @return void.
 */
// AWT thread
public void paint(final Graphics g) {
    if (backing_store == null)
        return;
    backing_g.clearRect(0, 0, dimension.width, dimension.height);

    updateFPS();

    final long current_time = System.currentTimeMillis();
    if (current_time - last_paint_100ms >= 100) {
        last_paint_100ms = current_time;
        last_fps_100ms = fps;
    }

    synchronized (sync_value_per_vinterval) {
        final long now = paintAxis();
        paintChart(now);
    }

    paintFPS(last_fps_100ms);

    g.drawImage(backing_store, 0, 0, this);
}