Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

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

Prototype

public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

Source Link

Document

Renders a BufferedImage that is filtered with a BufferedImageOp .

Usage

From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java

@Override
protected void paintComponent(Graphics graphics) {
    Graphics2D g = (Graphics2D) graphics;

    Rectangle rect = getInternalBounds();
    int width = rect.width;
    int height = rect.height;

    if (useBGImage) {
        // Create a translucent intermediate image in which we can perform
        // the soft clipping
        GraphicsConfiguration gc = g.getDeviceConfiguration();
        if (img == null || img.getWidth() != width || img.getHeight() != height) {
            img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        }//from ww w .j  ava  2s. co m
        Graphics2D g2 = img.createGraphics();

        // Clear the image so all pixels have zero alpha
        g2.setComposite(AlphaComposite.Clear);
        g2.fillRect(0, 0, width, height);

        g2.setComposite(AlphaComposite.Src);
        g2.setColor(new Color(0, 0, 0, 85));
        g2.fillRect(0, 0, width, height);

        if (delegateRenderer != null) {
            delegateRenderer.render(g, g2, img);
        }

        g2.dispose();

        // Copy our intermediate image to the screen
        g.drawImage(img, rect.x, rect.y, null);
    }

    super.paintComponent(graphics);

    if (StringUtils.isNotEmpty(text)) {
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(fillColor);
        g2.fillRect(margin.left, margin.top, rect.width, rect.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(text);
        int th = fm.getHeight();
        int tx = (rect.width - tw) / 2;
        int ty = (rect.height - th) / 2;

        if (yPos != null) {
            ty = yPos;
        }

        int expand = 20;
        int arc = expand * 2;

        g2.setColor(new Color(0, 0, 0, 50));

        int x = margin.left + tx - (expand / 2);
        int y = margin.top + ty - fm.getAscent() - (expand / 2);

        drawBGContainer(g2, true, x + 4, y + 6, tw + expand, th + expand, arc, arc);

        g2.setColor(new Color(255, 255, 255, 220));
        drawBGContainer(g2, true, x, y, tw + expand, th + expand, arc, arc);

        g2.setColor(Color.DARK_GRAY);
        drawBGContainer(g2, false, x, y, tw + expand, th + expand, arc, arc);

        g2.setColor(textColor == null ? Color.BLACK : textColor);
        g2.drawString(text, tx, ty);
    }
}

From source file:Filter3dTest.java

public void draw(Graphics2D g) {

    // draw background
    g.setColor(new Color(0x33cc33));
    g.fillRect(0, 0, screen.getWidth(), screen.getHeight());

    // draw listener
    g.drawImage(listener.getImage(), Math.round(listener.getX()), Math.round(listener.getY()), null);

    // draw fly//ww w . j a  v  a 2  s .c  o  m
    g.drawImage(fly.getImage(), Math.round(fly.getX()), Math.round(fly.getY()), null);
}

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

private Object prepareLegend(String legendKey, XMLAdapter jrxmlAdapter, List<OrderedDatasource<?>> datasources,
        String type, int resolution) throws ProcessletException {

    if ("net.sf.jasperreports.engine.JRRenderable".equals(type)) {
        return new LegendRenderable(datasources, resolution);
    } else {//from w  ww .j a v a 2 s.c om
        OMElement legendRE = jrxmlAdapter.getElement(jrxmlAdapter.getRootElement(), new XPath(
                ".//jasper:image[jasper:imageExpression/text()='$P{" + legendKey + "}']/jasper:reportElement",
                nsContext));

        if (legendRE != null) {
            LOG.debug("Found legend with key '" + legendKey + "'.");
            int width = jrxmlAdapter.getRequiredNodeAsInteger(legendRE, new XPath("@width", nsContext));
            int height = jrxmlAdapter.getRequiredNodeAsInteger(legendRE, new XPath("@height", nsContext));
            width = adjustSpan(width, resolution);
            height = adjustSpan(height, resolution);

            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            // TODO: bgcolor?
            Color bg = Color.decode("0xFFFFFF");
            g.setColor(bg);
            g.fillRect(0, 0, width, height);
            g.setColor(Color.BLACK);
            int k = 0;

            for (int i = 0; i < datasources.size(); i++) {
                if (k > height) {
                    LOG.warn("The necessary legend size is larger than the available legend space.");
                }
                List<Pair<String, BufferedImage>> legends = datasources.get(i).getLegends(width);
                for (Pair<String, BufferedImage> legend : legends) {
                    BufferedImage img = legend.second;
                    if (img != null) {
                        if (img.getWidth(null) < 50) {
                            // it is assumed that no label is assigned
                            g.drawImage(img, 0, k, null);
                            g.drawString(legend.first, img.getWidth(null) + 10, k + img.getHeight(null) / 2);
                        } else {
                            g.drawImage(img, 0, k, null);
                        }
                        k = k + img.getHeight(null) + 10;
                    } else {
                        g.drawString("- " + legend.first, 0, k + 10);
                        k = k + 20;
                    }
                }
            }
            g.dispose();
            return convertImageToReportFormat(type, bi);
        }
    }
    return null;
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Create the front cover/*from  w ww.  j  a  v a 2  s.co m*/
 * 
 * @param coverId
 * @param logoBytes
 * @param authorStr
 * @param titleStr
 * @param textColor
 *            - ex. #FFFFFF
 * @param width
 * @param height
 * @param type
 *            - ex. BufferedImage.TYPE_INT_ARGB
 * @param imgFormat
 *            - ex. ImageFormat.IMAGE_FORMAT_PNG
 * @return
 * @throws Exception
 */
public byte[] createFrontCover(String coverId, byte[] logoBytes, String authorStr, String titleStr,
        String spineColor, String authorTextColor, String titleTextColor, int width, int height, int type,
        ImageFormat imgFormat, int maxColors) throws Exception {

    Graphics2D g = null;

    try {
        // Front cover first
        // Read in base cover image
        BufferedImage coverImg = Imaging.getBufferedImage(coverMap.get(coverId));

        // Resize cover image to the basic 300 X 400 for front cover
        BufferedImage frontCoverImg = resize(coverImg, 300, 400, type);
        g = (Graphics2D) frontCoverImg.getGraphics();

        // Draw logo if present
        if (logoBytes != null && logoBytes.length > 0) {
            // Resize logo to 200x100
            BufferedImage logoImg = Imaging.getBufferedImage(logoBytes);
            BufferedImage outLogo = null;
            int logoHeight = logoImg.getHeight();
            int logoWidth = logoImg.getWidth();

            if (logoHeight > 100 || logoWidth > 200) {
                outLogo = this.resize(logoImg, logoWidth > 200 ? 200 : logoWidth,
                        logoHeight > 100 ? 100 : logoHeight, type);
            } else {
                outLogo = logoImg;
            }

            // Add to coverImg
            g.drawImage(outLogo, 32, 25, null);
        }

        // Add spine if present
        if (spineColor != null && spineColor.length() > 0) {
            g.setColor(Color.decode(spineColor));
            g.fillRect(0, 0, 2, frontCoverImg.getHeight());
        }

        // Add author if present
        if (authorStr != null && authorStr.length() > 0) {
            // Add author text to image
            BufferedImage authorTextImg = createText(40, 220, authorStr, authorTextColor, false, authorFontMap,
                    type);
            g.drawImage(authorTextImg, 30, 215, null);
        }

        // Add title if present
        if (titleStr != null && titleStr.length() > 0) {
            BufferedImage titleTextImg = createText(100, 220, titleStr, titleTextColor, false, titleFontMap,
                    type);
            g.drawImage(titleTextImg, 30, 240, null);
        }

        // If the requested size is not 300X400 convert the image
        BufferedImage outImg = null;
        if (width != 300 || height != 400) {
            outImg = resize(frontCoverImg, width, height, type);
        } else {
            outImg = frontCoverImg;
        }

        // Do we want a PNG with a fixed number of colors
        if (maxColors >= 2 && imgFormat == ImageFormat.IMAGE_FORMAT_PNG) {
            outImg = ImageUtils.reduce32(outImg, maxColors);
        }

        // Return bytes
        Map<String, Object> params = new HashMap<String, Object>();
        byte[] outBytes = Imaging.writeImageToBytes(outImg, imgFormat, params);
        return outBytes;
    } finally {
        if (g != null) {
            g.dispose();
        }
    }
}

From source file:com.moviejukebox.plugin.DefaultImagePlugin.java

/**
 * Draw the set logo onto a poster/*w w  w.j a v a  2  s.com*/
 *
 * @param movie the movie to check
 * @param bi the image to draw on
 * @return the new buffered image
 */
private BufferedImage drawSet(Movie movie, BufferedImage bi) {
    try {
        BufferedImage biSet = GraphicTools.loadJPEGImage(getResourcesPath() + FILENAME_SET);

        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(biSet, bi.getWidth() - biSet.getWidth() - 5, 1, null);
        g2d.dispose();
    } catch (FileNotFoundException ex) {
        LOG.warn(LOG_FAILED_TO_LOAD, FILENAME_SET);
    } catch (IOException error) {
        LOG.warn("Failed drawing set logo to thumbnail for {}", movie.getBaseFilename());
        LOG.warn("Please check that set graphic ({}) is in the resources directory. ", FILENAME_SET,
                error.getMessage());
    }

    return bi;
}

From source file:com.moviejukebox.plugin.DefaultImagePlugin.java

/**
 * Draw the SubTitle logo on the image//from w w w .  j  a  v  a  2  s.  com
 *
 * @param movie
 * @param bi
 * @return
 */
private BufferedImage drawSubTitle(Movie movie, BufferedImage bi) {
    // If the doesn't have subtitles, then quit
    if (StringTools.isNotValidString(movie.getSubtitles()) || "NO".equalsIgnoreCase(movie.getSubtitles())) {
        return bi;
    }

    File logoFile = new File(getResourcesPath() + FILENAME_SUBTITLE);

    if (!logoFile.exists()) {
        LOG.debug("Missing SubTitle logo ({}) unable to draw logo", FILENAME_SUBTITLE);
        return bi;
    }

    try {
        BufferedImage biSubTitle = GraphicTools.loadJPEGImage(logoFile);
        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(biSubTitle, bi.getWidth() - biSubTitle.getWidth() - 5, 5, null);
        g2d.dispose();
    } catch (FileNotFoundException ex) {
        LOG.warn(LOG_FAILED_TO_LOAD, logoFile);
    } catch (IOException ex) {
        LOG.warn(
                "Failed drawing SubTitle logo to thumbnail file: Please check that {} is in the resources directory.",
                FILENAME_SUBTITLE);
    }

    return bi;
}

From source file:org.tsho.dmc2.core.chart.AbsorbingAreaRenderer.java

public void chooseSegments(Graphics2D g2, BufferedImage image, int imageX, int imageY) {
    boolean atLeastOneSegmentEntered = false;
    disableAllActionsExceptStop();//from w  w  w .  j  ava 2 s .  c  o  m
    det.clearSegments();
    this.segmentsIteratesCount = 0;
    mouseClicked = false;
    while (!stopped) {
        while (!mouseClicked && !stopped) {
            delayExecution();
        }
        if (stopped)
            break;
        mouseClicked = false;
        //else a mouse was clicked, and its coordinates (not Point coordinates) are stored in xClicked,yClicked. This click chooses a segment
        Point p = new Point();
        p.set(xClicked - imageX, yClicked - imageY);
        Segment s = det.getSegment(p);
        //color the segment with color -100000, say.
        det.plotSegment(s, -100000);
        g2.drawImage(image, null, imageX, imageY);
        //here the left point of the segment is chosen. If a point is clicked whose projection to the x-axis does not belong to the
        //projection of the segment, the leftmost point of the segment is chosen automatically. If stopped, the segment is stored as it is.
        while (!mouseClicked && !stopped) {
            delayExecution();
        }
        if (stopped) {
            //store segment
            det.addSegmentToIterate(s);
            break;
        }
        mouseClicked = false;
        int xlc = xClicked - imageX;
        if (xlc < s.xl)
            xlc = s.xl;
        if (xlc > s.xr)
            xlc = s.xr;
        det.plotSegment(s, -10000);//color the segment in the original color
        det.plotSegment(s, -100000, xlc, s.xr);
        g2.drawImage(image, null, imageX, imageY);
        //here the left point of the segment is chosen. If a point is clicked whose projection to the x-axis does not belong to the
        //projection of the segment, the rightmost point of the segment is chosen automatically. If stopped, the segment is stored with user chosen leftmost point.
        while (!mouseClicked && !stopped) {
            delayExecution();
        }
        if (stopped) {
            //store segment
            Segment s1 = new Segment(xlc, s.xr, s.branchIndex);
            det.addSegmentToIterate(s1);
            break;
        }
        mouseClicked = false;
        int xrc = xClicked - imageX;
        if (xrc < s.xl)
            xrc = s.xl;
        if (xrc > s.xr)
            xrc = s.xr;
        det.plotSegment(s, -10000);//color the segment in the original color
        det.plotSegment(s, -100000, xlc, xrc);
        g2.drawImage(image, null, imageX, imageY);
        if (xlc <= xrc) {
            Segment s2 = new Segment(xlc, xrc, s.branchIndex);
            det.addSegmentToIterate(s2);
        }
    }
    stopped = false;
    this.enableAllActionsExceptStop();
}

From source file:com.moviejukebox.plugin.DefaultImagePlugin.java

/**
 * Draw the TV logo onto the image file//from   w w w  .  j av  a 2s  .com
 *
 * @param movie The source movie
 * @param bi The original image
 * @param addOtherLogo Do we need to draw the HD logo as well?
 * @return The new image file
 */
private BufferedImage drawLogoTV(Movie movie, BufferedImage bi, Boolean addOtherLogo) {
    if (movie.isTVShow()) {
        try {
            BufferedImage biTV = GraphicTools.loadJPEGImage(getResourcesPath() + FILENAME_TV);
            Graphics2D g2d = bi.createGraphics();

            if (addOtherLogo && movie.isHD()) {
                // Both logos are required, so put the TV logo on the RIGHT
                g2d.drawImage(biTV, bi.getWidth() - biTV.getWidth() - 5, bi.getHeight() - biTV.getHeight() - 5,
                        null);
                LOG.debug("Drew TV logo on the right");
            } else {
                // Only the TV logo is required so set it in the centre
                g2d.drawImage(biTV, bi.getWidth() / 2 - biTV.getWidth() / 2,
                        bi.getHeight() - biTV.getHeight() - 5, null);
                LOG.debug("Drew TV logo in the middle");
            }

            g2d.dispose();
        } catch (FileNotFoundException ex) {
            LOG.warn(LOG_FAILED_TO_LOAD, FILENAME_TV);
        } catch (IOException error) {
            LOG.warn(
                    "Failed drawing TV logo to thumbnail file: Please check that {} is in the resources directory.",
                    FILENAME_TV);
            LOG.error(SystemTools.getStackTrace(error));
        }
    }

    return bi;
}

From source file:com.moviejukebox.plugin.DefaultImagePlugin.java

/**
 * Draw the appropriate HD logo onto the image file
 *
 * @param movie The source movie/*from w w  w  .j a v  a2  s.  c  om*/
 * @param bi The original image
 * @param addOtherLogo Do we need to draw the TV logo as well?
 * @return The new image file
 */
private BufferedImage drawLogoHD(Movie movie, BufferedImage bi, Boolean addOtherLogo) {
    // If the movie isn't high definition, then quit
    if (!movie.isHD()) {
        return bi;
    }

    String logoFilename;
    File logoFile;

    // Determine which logo to use.
    if (highdefDiff) {
        if (movie.isHD1080()) {
            // Use the 1080p logo
            logoFilename = FILENAME_HD1080;
        } else {
            // Otherwise use the 720p
            logoFilename = FILENAME_HD720;
        }
    } else {
        // We don't care, so use the default HD logo.
        logoFilename = FILENAME_HD;
    }

    logoFile = new File(getResourcesPath() + logoFilename);
    if (!logoFile.exists()) {
        LOG.debug("Missing HD logo ({}) using default {}", logoFilename, FILENAME_HD);
        logoFilename = FILENAME_HD;
    }

    try {
        BufferedImage biHd = GraphicTools.loadJPEGImage(getResourcesPath() + logoFilename);
        Graphics2D g2d = bi.createGraphics();

        if (addOtherLogo && (movie.isTVShow())) {
            // Both logos are required, so put the HD logo on the LEFT
            g2d.drawImage(biHd, 5, bi.getHeight() - biHd.getHeight() - 5, null);
            LOG.debug("Drew HD logo ({}) on the left", logoFilename);
        } else {
            // Only the HD logo is required so set it in the centre
            g2d.drawImage(biHd, bi.getWidth() / 2 - biHd.getWidth() / 2, bi.getHeight() - biHd.getHeight() - 5,
                    null);
            LOG.debug("Drew HD logo ({}) in the middle", logoFilename);
        }

        g2d.dispose();
    } catch (FileNotFoundException ex) {
        LOG.warn("Failed to load {}{}, please ensure it is valid", overlayResources, logoFilename);
    } catch (IOException ex) {
        LOG.warn(
                "Failed drawing HD logo to thumbnail file. Please check that {} is in the resources directory.",
                logoFilename);
    }

    return bi;
}

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

public void grayscale() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();//w ww .j  a  va 2s  .c o  m
    image(img);
}