Example usage for java.awt Graphics2D setRenderingHint

List of usage examples for java.awt Graphics2D setRenderingHint

Introduction

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

Prototype

public abstract void setRenderingHint(Key hintKey, Object hintValue);

Source Link

Document

Sets the value of a single preference for the rendering algorithms.

Usage

From source file:net.pms.util.GenericIcons.java

/**
 * Add the format(container) name of the media to the generic icon image.
 *
 * @param image BufferdImage to be the label added
 * @param label the media container name to be added as a label
 * @param renderer the renderer configuration
 *
 * @return the generic icon with the container label added and scaled in accordance with renderer setting
 *///from  w  w  w. j  a va 2  s .c o  m
private DLNAThumbnail addFormatLabelToImage(String label, ImageFormat imageFormat, IconType iconType)
        throws IOException {

    BufferedImage image;
    switch (iconType) {
    case AUDIO:
        image = genericAudioIcon;
        break;
    case IMAGE:
        image = genericImageIcon;
        break;
    case VIDEO:
        image = genericVideoIcon;
        break;
    default:
        image = genericUnknownIcon;
    }

    if (image != null) {
        // Make a copy
        ColorModel colorModel = image.getColorModel();
        image = new BufferedImage(colorModel, image.copyData(null), colorModel.isAlphaPremultiplied(), null);
    }

    ByteArrayOutputStream out = null;

    if (label != null && image != null) {
        out = new ByteArrayOutputStream();
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        try {
            int size = 40;
            Font font = new Font(Font.SANS_SERIF, Font.BOLD, size);
            FontMetrics metrics = g.getFontMetrics(font);
            while (size > 7 && metrics.stringWidth(label) > 135) {
                size--;
                font = new Font(Font.SANS_SERIF, Font.BOLD, size);
                metrics = g.getFontMetrics(font);
            }
            // Text center point 127x, 49y - calculate centering coordinates
            int x = 127 - metrics.stringWidth(label) / 2;
            int y = 46 + metrics.getAscent() / 2;
            g.drawImage(image, 0, 0, null);
            g.setColor(Color.WHITE);
            g.setFont(font);
            g.drawString(label, x, y);

            ImageIO.setUseCache(false);
            ImageIOTools.imageIOWrite(image, imageFormat.toString(), out);
        } finally {
            g.dispose();
        }
    }
    return out != null ? DLNAThumbnail.toThumbnail(out.toByteArray(), 0, 0, ScaleType.MAX, imageFormat, false)
            : null;
}

From source file:dcstore.web.ImagesWeb.java

private void resizeImage(String inPath, int w, int h, String outPath) {
    try {/* w  w  w  .ja  v a2  s  .c om*/
        BufferedImage originalImage = ImageIO.read(new File(inPath));
        int ow, oh;
        ow = originalImage.getWidth();
        oh = originalImage.getHeight();
        double ratio = (double) ow / (double) oh;
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        int ch = (int) Math.round(w / ratio);

        BufferedImage resizedImage = new BufferedImage(w, h, type);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        g.drawImage(originalImage, 0, (int) (((float) h - (float) ch) / 2), w, ch, null);
        g.dispose();
        ImageIO.write(resizedImage, "jpg", new File(outPath));
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage("",
                new FacesMessage("Error while resizeing image: " + e.getMessage()));
    }
}

From source file:eu.udig.style.advanced.utils.Utilities.java

/**
 * Creates an image from a set of {@link RuleWrapper}s.
 * //from  ww w .j  a v  a  2 s .co m
 * @param ruleWrapperList the list of rule wrappers.
 * @param width the image width.
 * @param height the image height.
 * @return the new created {@link BufferedImage}.
 */
public static BufferedImage pointRulesWrapperToImage(final List<RuleWrapper> ruleWrapperList, int width,
        int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (RuleWrapper ruleWrapper : ruleWrapperList) {
        BufferedImage tmpImage = Utilities.pointRuleWrapperToImage(ruleWrapper, width, height);
        g2d.drawImage(tmpImage, 0, 0, null);
    }
    g2d.dispose();
    return image;
}

From source file:eu.udig.style.advanced.utils.Utilities.java

/**
 * Creates an image from a set of {@link RuleWrapper}s.
 * /*from w  w  w . j  a v a  2 s  . com*/
 * @param rulesWrapperList the list of rules wrapper.
 * @param width the image width.
 * @param height the image height.
 * @return the new created {@link BufferedImage}.
 */
public static BufferedImage polygonRulesWrapperToImage(final List<RuleWrapper> rulesWrapperList, int width,
        int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (RuleWrapper rule : rulesWrapperList) {
        BufferedImage tmpImage = Utilities.polygonRuleWrapperToImage(rule, width, height);
        g2d.drawImage(tmpImage, 0, 0, null);
    }
    g2d.dispose();
    return image;
}

From source file:eu.udig.style.advanced.utils.Utilities.java

/**
 * Creates an image from a set of {@link RuleWrapper}s.
 * /*  w  ww.  ja v a 2 s.c o m*/
 * @param rulesWrapperList the list of rules wrapper.
 * @param width the image width.
 * @param height the image height.
 * @return the new created {@link BufferedImage}.
 */
public static BufferedImage lineRulesWrapperToImage(final List<RuleWrapper> rulesWrapperList, int width,
        int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (RuleWrapper rule : rulesWrapperList) {
        BufferedImage tmpImage = Utilities.lineRuleWrapperToImage(rule, width, height);
        g2d.drawImage(tmpImage, 0, 0, null);
    }
    g2d.dispose();
    return image;
}

From source file:org.uva.itast.blended.omr.pages.PDFPageImage.java

/**
 * @param x//from  w  w w. ja va2  s. com
 * @param y
 * @param w
 * @param h
 * @return
 */
private SubImage getSubimageWithPartialRendering(Rectangle2D rect, int imageType) {
    double pageHeight = getPage().getHeight();
    // Area in pixels according to preferred resolution
    Point upperLeft = toPixels(rect.getX(), rect.getY());

    Rectangle imageBBox = this.toPixels(rect); // subImage Bounding Box in pixels      
    Rectangle2D pdfAreaBBox = toPDFUnits(imageBBox); // subImage Bounding Box in PDF units

    Rectangle imageSize = new Rectangle(imageBBox.width, imageBBox.height); // subImage Size in pixels

    Rectangle2D clippingArea = new Rectangle(); // area of interest in the PDF
    clippingArea.setFrame(pdfAreaBBox.getX(), pageHeight - pdfAreaBBox.getY() - pdfAreaBBox.getHeight(), //PDF-Page coordinate space counts from bottomleft
            pdfAreaBBox.getWidth(), pdfAreaBBox.getHeight());

    SubImage img_pdf = new SubImage(imageSize.width, imageSize.height, imageType);
    // se configura la imagen con las medidas necesarias

    Graphics2D g2 = img_pdf.createGraphics(); // se crea un objeto grfico en dos dimensiones
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); // prefer to get sharp edges

    PDFRenderer renderer = new PDFRenderer(getPage(), g2, imageSize, clippingArea, Color.RED); // se renderiza la imgen 
    try {
        getPage().waitForFinish();
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    renderer.run();

    img_pdf.setReference(upperLeft);
    img_pdf.setBoundingBox(imageBBox);

    return img_pdf;
}

From source file:org.uva.itast.blended.omr.pages.PDFPageImage.java

/**
 * Mtodo que lee una imagen pdf y la transforma en un objeto de tipo
 * BufferedImage reescalado//from ww  w .  j  av a2 s  .com
 * 
 * @param page
 * @return img_pdf
 * @throws InterruptedException
 * @throws Exception
 */
private BufferedImage renderFullPageImage(PDFPage page) throws InterruptedException {
    BufferedImage img_pdf = null; // bufferedimage para almacenar la imagen

    // se elige el ancho y el alto de la imagen
    int resizeWidth = OMRProcessor._PAGE_WIDTH_PIXELS;
    int resizeHeight = OMRProcessor._PAGE_HEIGHT_PIXELS;

    img_pdf = new BufferedImage(resizeWidth, resizeHeight, BufferedImage.TYPE_INT_RGB); // se configura la imagen con las
    // medidas especificas y en
    // escala de grises
    Graphics2D g2 = img_pdf.createGraphics(); // se crea un objeto grfico
    // en dos dimensiones
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    PDFRenderer renderer = new PDFRenderer(page, g2,
            new Rectangle(0, 0, OMRProcessor._PAGE_WIDTH_PIXELS, OMRProcessor._PAGE_HEIGHT_PIXELS), null,
            Color.RED); // se
    // renderiza
    // la
    // imgen
    page.waitForFinish();
    renderer.run();
    img_pdf.createGraphics().drawImage(img_pdf, 0, 0, resizeWidth, resizeHeight, null); // por ltimo se dibuja la imagen

    return img_pdf;
}

From source file:org.esa.s2tbx.dataio.s2.l1b.L1bSceneDescription.java

public BufferedImage createTilePicture(int width) {

    Color[] colors = new Color[] { Color.GREEN, Color.RED, Color.BLUE, Color.YELLOW };

    double scale = width / sceneRectangle.getWidth();
    int height = (int) Math.round(sceneRectangle.getHeight() * scale);

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = image.createGraphics();
    graphics.scale(scale, scale);//  w ww.j  a  v a  2s  .  c o m
    graphics.translate(-sceneRectangle.getX(), -sceneRectangle.getY());
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setPaint(Color.WHITE);
    graphics.fill(sceneRectangle);
    graphics.setStroke(new BasicStroke(100F));
    graphics.setFont(new Font("Arial", Font.PLAIN, 800));

    for (int i = 0; i < tileInfos.length; i++) {
        Rectangle rect = tileInfos[i].rectangle;
        graphics.setPaint(addAlpha(colors[i % colors.length].brighter(), 100));
        graphics.fill(rect);
    }
    for (int i = 0; i < tileInfos.length; i++) {
        Rectangle rect = tileInfos[i].rectangle;
        graphics.setPaint(addAlpha(colors[i % colors.length].darker(), 100));
        graphics.draw(rect);
        graphics.setPaint(colors[i % colors.length].darker().darker());
        graphics.drawString("Tile " + (i + 1) + ": " + tileInfos[i].id, rect.x + 1200F, rect.y + 2200F);
    }
    return image;
}

From source file:org.openstreetmap.gui.jmapviewer.Tile.java

/**
 * Creates a image for the current Tile//from w  w w.j  a  v a2s. co  m
 * 
 * @param image2
 *            origin
 * @param imageW
 *            width for the tile
 * @param imageH
 *            heigth for the tile
 */
private void createGraphicsFromImage(BufferedImage image2, int imageW, int imageH) {
    BufferedImage unScaledImage = image2;

    // Create new (blank) image of required (scaled) size
    BufferedImage scaledImage = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_ARGB);

    image = scaledImage;

    // Paint scaled version of image to new image
    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(unScaledImage, 0, 0, imageW, imageH, null);

    // clean up
    graphics2D.dispose();
}

From source file:MyCanvas.java

public Graphics2D createDemoGraphics2D(Graphics g) {
    Graphics2D g2 = null;

    if (offImg == null || offImg.getWidth() != getSize().width || offImg.getHeight() != getSize().height) {
        offImg = (BufferedImage) createImage(getSize().width, getSize().height);
    }/*from w w  w. j av  a 2s . c o m*/

    if (offImg != null) {
        g2 = offImg.createGraphics();
        g2.setBackground(getBackground());
    }

    // .. set attributes ..
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // .. clear canvas ..
    g2.clearRect(0, 0, getSize().width, getSize().height);

    return g2;
}