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 dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2,
        int sy2, ImageObserver observer);

Source Link

Document

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.

Usage

From source file:Main.java

/**
 * Draw the image with 9 grids scaling//from  www.  ja  va  2s.co m
 * 
 * @param g
 * @param img
 * @param dx1
 * @param dy1
 * @param dx2
 * @param dy2
 * @param insets
 * @param paintCenter
 */
public static void drawImageWith9Grids(Graphics g, Image img, int dx1, int dy1, int dx2, int dy2, Insets insets,
        boolean paintCenter) {

    final int imgWidth = img.getWidth(null);
    final int imgHeight = img.getHeight(null);

    // top-left cornor
    g.drawImage(img, dx1, dy1, dx1 + insets.left, dy1 + insets.top, 0, 0, insets.left, insets.top, null);
    // top-right cornor
    g.drawImage(img, dx2 - insets.right, dy1, dx2, dy1 + insets.top, imgWidth - insets.right, 0, imgWidth,
            insets.top, null);
    // bottom-left cornor
    g.drawImage(img, dx1, dy2 - insets.bottom, dx1 + insets.left, dy2, 0, imgHeight - insets.bottom,
            insets.left, imgHeight, null);
    // bottom-right cornor
    g.drawImage(img, dx2 - insets.right, dy2 - insets.bottom, dx2, dy2, imgWidth - insets.right,
            imgHeight - insets.bottom, imgWidth, imgHeight, null);
    // top border
    g.drawImage(img, dx1 + insets.left, dy1, dx2 - insets.right, dy1 + insets.top, insets.left, 0,
            imgWidth - insets.right, insets.top, null);
    // bottom border
    g.drawImage(img, dx1 + insets.left, dy2 - insets.bottom, dx2 - insets.right, dy2, insets.left,
            imgHeight - insets.bottom, imgWidth - insets.right, imgHeight, null);
    // left border
    g.drawImage(img, dx1, dy1 + insets.top, dx1 + insets.left, dy2 - insets.bottom, 0, insets.top, insets.left,
            imgHeight - insets.bottom, null);
    // right border
    g.drawImage(img, dx2 - insets.right, dy1 + insets.top, dx2, dy2 - insets.bottom, imgWidth - insets.right,
            insets.top, imgWidth, imgHeight - insets.bottom, null);
    // center
    if (paintCenter) {
        g.drawImage(img, dx1 + insets.left, dy1 + insets.top, dx2 - insets.right, dy2 - insets.bottom,
                insets.left, insets.top, imgWidth - insets.right, imgHeight - insets.bottom, null);
    }
}

From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java

/**
 * Utility method for scaling thumbnails. Scales byte[] image by a scale factor.
 * Optionally crops images to square./*  www . java2  s  . c o  m*/
 * @param src RenderedImage containing the input image
 * @param scale Scale amount
 * @param square Boolean flag to crop to a square image
 * @return RenderedImage containing the transformed image
 * @throws IOException
 */
public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException {
    int sx = 0, sy = 0;
    int swidth = image.getWidth();
    int sheight = image.getHeight();

    if (square) {
        if (image.getHeight() > image.getWidth()) {
            sy = (int) ((image.getHeight() - image.getWidth()) / 2.0);
            sheight = swidth;
        } else if (image.getHeight() < image.getWidth()) {
            sx = (int) ((image.getWidth() - image.getHeight()) / 2.0);
            swidth = sheight;
        }
    }
    int width = (int) (swidth * scale);
    int height = (int) (sheight * scale);

    BufferedImage scaled = new BufferedImage(image.getColorModel(),
            image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(),
            null);

    Graphics g = scaled.getGraphics();
    g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null);
    g.dispose();

    return scaled;
}

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;//from   ww w.  j a va  2  s. co  m
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}

From source file:ImageLabel.java

public void paint(Graphics g) {

    /*//from   w w w  .j  a  va  2  s  .c om
     * Draw the image stretched to exactly cover the size of the drawing area.
     */
    Dimension size = getSize();
    g.drawImage(img, 0, 0, size.width, size.height, 0, 0, img.getWidth(null), img.getHeight(null), null);

    /*
     * Fill a rounded rectangle centered in the drawing area. Calculate the size
     * of the rectangle from the size of the text
     */
    g.setFont(font);
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, frc);

    int wText = (int) bounds.getWidth();
    int hText = (int) bounds.getHeight();

    int rX = (size.width - wText) / 2;
    int rY = (size.height - hText) / 2;
    g.setColor(Color.yellow);
    g.fillRoundRect(rX, rY, wText, hText, hText / 2, hText / 2);

    /*
     * Draw text positioned in the rectangle. Since the rectangle is sized based
     * on the bounds of the String we can position it using those bounds.
     */
    int xText = rX - (int) bounds.getX();
    int yText = rY - (int) bounds.getY();
    g.setColor(Color.black);
    g.setFont(font);
    g.drawString(text, xText, yText);
}

From source file:JumbledImage.java

public void paint(Graphics g) {

    int dx, dy;//from   w w w.j a v a  2s .com
    for (int x = 0; x < numlocs; x++) {
        int sx = x * cw;
        for (int y = 0; y < numlocs; y++) {
            int sy = y * ch;
            int cell = cells[x * numlocs + y];
            dx = (cell / numlocs) * cw;
            dy = (cell % numlocs) * ch;
            g.drawImage(bi, dx, dy, dx + cw, dy + ch, sx, sy, sx + cw, sy + ch, null);
        }
    }
}

From source file:SampleAWTRenderer.java

/**
 * Processes the data and renders it to a component
 *//*from ww w  .jav  a 2  s .com*/
public synchronized int process(Buffer buffer) {
    if (component == null)
        return BUFFER_PROCESSED_FAILED;

    Format inf = buffer.getFormat();
    if (inf == null)
        return BUFFER_PROCESSED_FAILED;

    if (inf != inputFormat || !buffer.getFormat().equals(inputFormat)) {
        if (setInputFormat(inf) != null)
            return BUFFER_PROCESSED_FAILED;
    }

    Object data = buffer.getData();
    if (!(data instanceof int[]))
        return BUFFER_PROCESSED_FAILED;

    if (lastBuffer != buffer) {
        lastBuffer = buffer;
        newImage(buffer);
    }

    sourceImage.newPixels(0, 0, inWidth, inHeight);

    Graphics g = component.getGraphics();
    if (g != null) {
        if (reqBounds == null) {
            bounds = component.getBounds();
            bounds.x = 0;
            bounds.y = 0;
        } else
            bounds = reqBounds;
        g.drawImage(destImage, bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, inWidth, inHeight,
                component);
    }

    return BUFFER_PROCESSED_OK;
}

From source file:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Create Playlist buttons.//w ww. j  a va  2s. com
 * @param sx
 * @param sy
 * @return
 */
private ActiveJButton createPLButton(int sx, int sy) {
    Image normal = new BufferedImage(22, 18, BufferedImage.TYPE_INT_RGB);
    Image clicked = new BufferedImage(22, 18, BufferedImage.TYPE_INT_RGB);
    Graphics g = normal.getGraphics();
    g.drawImage(imPlaylist, 0, 0, 22, 18, sx, sy, sx + 22, sy + 18, null);
    sx += 23;
    g = clicked.getGraphics();
    g.drawImage(imPlaylist, 0, 0, 22, 18, sx, sy, sx + 22, sy + 18, null);
    ActiveJButton comp = new ActiveJButton();
    comp.setIcon(new ImageIcon(normal));
    comp.setPressedIcon(new ImageIcon(clicked));
    comp.setRolloverIcon(new ImageIcon(clicked));
    comp.setRolloverEnabled(true);
    return comp;
}

From source file:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Set presets button./*w w w  .ja v  a  2  s .  c o m*/
 */
public void setPresetsPanel() {
    int w = 44, h = 12;
    releasedPresetsImage[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = releasedPresetsImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 224, 164, 224 + w, 164 + h, null);
    pressedPresetsImage[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g = pressedPresetsImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 224, 176, 224 + w, 176 + h, null);
    acPresets = new ActiveJButton();
    acPresets.setIcon(new ImageIcon(releasedPresetsImage[0]));
    acPresets.setPressedIcon(new ImageIcon(pressedPresetsImage[0]));
    acPresets.setConstraints(new AbsoluteConstraints(panelPresetsLocation[0], panelPresetsLocation[1],
            releasedPresetsImage[0].getWidth(null), releasedPresetsImage[0].getHeight(null)));
    acPresets.setToolTipText(getResource("equalizer.button.presets"));
    acPresets.setActionCommand(PlayerActionEvent.ACEQPRESETS);
}

From source file:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Set On/Off and Auto checkbox./*from w  w  w.java  2s .  co  m*/
 */
public void setOnOffAutoPanel() {
    // On/Off
    int w = 24, h = 12;
    releasedOAImage[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = releasedOAImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 10, 119, 10 + w, 119 + h, null);
    pressedOAImage[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g = pressedOAImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 69, 119, 69 + w, 119 + h, null);
    acOnOff = new ActiveJToggleButton();
    acOnOff.setIcon(new ImageIcon(releasedOAImage[0]));
    acOnOff.setSelectedIcon(new ImageIcon(pressedOAImage[0]));
    acOnOff.setPressedIcon(new ImageIcon(pressedOAImage[0]));
    acOnOff.setSelected(config.isEqualizerOn());
    acOnOff.setConstraints(new AbsoluteConstraints(panelOALocation[0], panelOALocation[1],
            releasedOAImage[0].getWidth(null), releasedOAImage[0].getHeight(null)));
    acOnOff.setToolTipText(getResource("equalizer.toggle.onoff"));
    acOnOff.setActionCommand(PlayerActionEvent.ACEQONOFF);
    // Auto
    w = 34;
    h = 12;
    releasedOAImage[1] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g = releasedOAImage[1].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 34, 119, 34 + w, 119 + h, null);
    pressedOAImage[1] = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g = pressedOAImage[1].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, w, h, 93, 119, 93 + w, 119 + h, null);
    acAuto = new ActiveJToggleButton();
    acAuto.setIcon(new ImageIcon(releasedOAImage[1]));
    acAuto.setPressedIcon(new ImageIcon(pressedOAImage[1]));
    acAuto.setSelectedIcon(new ImageIcon(pressedOAImage[1]));
    acAuto.setConstraints(new AbsoluteConstraints(panelOALocation[2], panelOALocation[3],
            releasedOAImage[1].getWidth(null), releasedOAImage[1].getHeight(null)));
    acAuto.setToolTipText(getResource("equalizer.toggle.auto"));
    acAuto.setActionCommand(PlayerActionEvent.ACEQAUTO);
}

From source file:javazoom.jlgui.player.amp.skin.Skin.java

/**
 * Set sliders for equalizer.//from  w  ww . j  a v a 2s. c  o m
 */
private void setSliderPanel() {
    releasedSliderImage[0] = new BufferedImage(12, 11, BufferedImage.TYPE_INT_RGB);
    Graphics g = releasedSliderImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, 12, 11, 0, 164, 0 + 12, 164 + 11, null);
    pressedSliderImage[0] = new BufferedImage(10, 11, BufferedImage.TYPE_INT_RGB);
    g = pressedSliderImage[0].getGraphics();
    g.drawImage(imFullEqualizer, 0, 0, 11, 11, 0, 176, 0 + 11, 176 + 11, null);
    for (int k = 0; k < sliderImage.length / 2; k++) {
        sliderImage[k] = new BufferedImage(13, 63, BufferedImage.TYPE_INT_RGB);
        g = sliderImage[k].getGraphics();
        g.drawImage(imSliders, 0, 0, 13, 63, k * 15, 0, k * 15 + 13, 0 + 63, null);
    }
    for (int k = 0; k < sliderImage.length / 2; k++) {
        sliderImage[k + (sliderImage.length / 2)] = new BufferedImage(13, 63, BufferedImage.TYPE_INT_RGB);
        g = sliderImage[k + (sliderImage.length / 2)].getGraphics();
        g.drawImage(imSliders, 0, 0, 13, 63, k * 15, 65, k * 15 + 13, 65 + 63, null);
    }
    // Setup sliders
    for (int i = 0; i < acSlider.length; i++) {
        sliderLocation[i][0] = sliderBarLocation[i][0] + 1;
        sliderLocation[i][1] = sliderBarLocation[i][1] + 1;// + deltaSlider * gainEqValue[i] / maxEqGain;
        acSlider[i] = new ActiveJSlider();
        acSlider[i].setMinimum(0);
        acSlider[i].setMaximum(100);
        acSlider[i].setValue(50);
        acSlider[i].setOrientation(JSlider.VERTICAL);
        ActiveSliderUI sUI = new ActiveSliderUI(acSlider[i]);
        sUI.setThumbImage(releasedSliderImage[0]);
        sUI.setThumbPressedImage(pressedSliderImage[0]);
        sUI.setBackgroundImages(sliderImage);
        sUI.setThumbXOffset(1);
        sUI.setThumbYOffset(-1);
        acSlider[i].setUI(sUI);
        acSlider[i].setConstraints(new AbsoluteConstraints(sliderLocation[i][0], sliderLocation[i][1],
                releasedSliderImage[0].getWidth(null), sliderImage[0].getHeight(null)));
    }
    acSlider[0].setEnabled(false);
}