Example usage for java.awt Graphics2D addRenderingHints

List of usage examples for java.awt Graphics2D addRenderingHints

Introduction

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

Prototype

public abstract void addRenderingHints(Map<?, ?> hints);

Source Link

Document

Sets the values of an arbitrary number of preferences for the rendering algorithms.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ImageIcon ii = new ImageIcon("C:/Java_Dev/test.jpg");
    BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    boolean b = g2d.drawImage(ii.getImage(), 0, 0, 50, 50, null);
    System.out.println(b);/*from   w w  w .  j  a  v  a2  s. c o  m*/
    ImageIO.write(bi, "jpg", new File("C:/Java_Dev/test.jpg"));
}

From source file:Main.java

/**
 * Restores text antialiasing hints into specified graphics context
 *
 * @param g2d/* ww  w  . ja v a2s  .c o m*/
 *            graphics context
 * @param hints
 *            old text antialiasing hints
 */
public static void restoreTextAntialias(final Graphics2D g2d, final Map hints) {
    if (hints != null) {
        g2d.addRenderingHints(hints);
    }
}

From source file:GUI.Main.java

public static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = bi.createGraphics();
    g2d.addRenderingHints(
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();//www.  j a  v  a2  s. c  o m
    return bi;
}

From source file:RenderingUtils.java

/**
 * Draws the string at the specified location underlining the specified
 * character./*from w  w w .  j  a  v a2 s.  c o m*/
 *
 * @param c JComponent that will display the string, may be null
 * @param g Graphics to draw the text to
 * @param text String to display
 * @param underlinedIndex Index of a character in the string to underline
 * @param x X coordinate to draw the text at
 * @param y Y coordinate to draw the text at
 */
public static void drawStringUnderlineCharAt(JComponent c, Graphics g, String text, int underlinedIndex, int x,
        int y) {

    if (drawStringUnderlineCharAtMethod != null) {
        try {
            drawStringUnderlineCharAtMethod.invoke(null,
                    new Object[] { c, g, text, new Integer(underlinedIndex), new Integer(x), new Integer(y) });
            return;
        } catch (IllegalArgumentException e) {
            // Use the BasicGraphicsUtils as fallback
        } catch (IllegalAccessException e) {
            // Use the BasicGraphicsUtils as fallback
        } catch (InvocationTargetException e) {
            // Use the BasicGraphicsUtils as fallback
        }
    }
    Graphics2D g2 = (Graphics2D) g;
    Map oldRenderingHints = installDesktopHints(g2);
    BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, underlinedIndex, x, y);
    if (oldRenderingHints != null) {
        g2.addRenderingHints(oldRenderingHints);
    }
    return;

    BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, underlinedIndex, x, y);
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static void setDefaultRendering(Graphics2D context) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Map desktopHints = (Map) (tk.getDesktopProperty("awt.font.desktophints"));

    if (desktopHints != null) {
        context.addRenderingHints(desktopHints);
    }/*  w w w.  j  a  v  a 2 s . c om*/
}

From source file:RenderingUtils.java

private static Map installDesktopHints(Graphics2D g2) {
    Map oldRenderingHints = null;

    Map desktopHints = desktopHints(g2);
    if (desktopHints != null && !desktopHints.isEmpty()) {
        oldRenderingHints = new HashMap(desktopHints.size());
        RenderingHints.Key key;
        for (Iterator i = desktopHints.keySet().iterator(); i.hasNext();) {
            key = (RenderingHints.Key) i.next();
            oldRenderingHints.put(key, g2.getRenderingHint(key));
        }/*from   www  .  j a v a 2s.c  o  m*/
        g2.addRenderingHints(desktopHints);
    }

    return oldRenderingHints;
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Draws an arrow from <code>(xCenter,yCenter)</code> to <code>(x,y)</code>.
 * Code stolen from http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135.
 * //from   w  w w  .  j a  va 2s  .  c  o m
 * @param g the graphics context to draw in
 * @param headSize the size of the arrow head
 * @param xCenter the x-coord of the arrow tail
 * @param yCenter the y-coord of the arrow tail
 * @param x the x-coord of the arrow head's tip
 * @param y the y-coord of the arrow head's tip
 * @param stroke the <code>Stroke</code> to use
 */
public static void drawArrow(Graphics g, int xCenter, int yCenter, int x, int y, int headSize, float stroke) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.addRenderingHints(hints);

    double aDir = Math.atan2(xCenter - x, yCenter - y);
    Stroke origStroke = g2d.getStroke();
    g2d.setStroke(new BasicStroke(stroke)); // make the arrow head solid even if dash pattern has been specified
    g2d.drawLine(x, y, xCenter, yCenter);
    Polygon tmpPoly = new Polygon();
    int i1 = 2 * headSize + (int) stroke; //(stroke * 2);
    int i2 = headSize + (int) stroke; // make the arrow head the same size regardless of the length length
    tmpPoly.addPoint(x, y); // arrow tip
    tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5));
    tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
    tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5));
    tmpPoly.addPoint(x, y); // arrow tip
    g2d.drawPolygon(tmpPoly);
    g2d.fillPolygon(tmpPoly); // remove this line to leave arrow head unpainted
    g2d.setStroke(origStroke);
}

From source file:FontHints.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);/*from  w ww .java  2s  . c o m*/
    g2d.fillRect(0, 0, getWidth(), getHeight());
    g2d.setColor(Color.BLACK);

    g2d.drawString("Unhinted string", 10, 20);
    if (desktopHints != null) {
        g2d.addRenderingHints(desktopHints);
    }
    g2d.drawString("Desktop-hinted string", 10, 40);
}

From source file:org.jrecruiter.service.impl.DataServiceImpl.java

/** {@inheritDoc} */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Image getGoogleMapImage(final BigDecimal latitude, final BigDecimal longitude, final Integer zoomLevel) {

    if (longitude == null) {
        throw new IllegalArgumentException("Longitude cannot be null.");
    }//from   w w  w  . j  av a  2  s . co m

    if (latitude == null) {
        throw new IllegalArgumentException("Latitude cannot be null.");
    }

    if (zoomLevel == null) {
        throw new IllegalArgumentException("ZoomLevel cannot be null.");
    }

    final URI url = GoogleMapsUtils.buildGoogleMapsStaticUrl(latitude, longitude, zoomLevel);

    BufferedImage img;
    try {
        URLConnection conn = url.toURL().openConnection();
        img = ImageIO.read(conn.getInputStream());
    } catch (UnknownHostException e) {
        LOGGER.error("Google static MAPS web service is not reachable (UnknownHostException).", e);
        img = new BufferedImage(GoogleMapsUtils.defaultWidth, 100, BufferedImage.TYPE_INT_RGB);

        final Graphics2D graphics = img.createGraphics();

        final Map<Object, Object> renderingHints = CollectionUtils.getHashMap();
        renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.addRenderingHints(renderingHints);
        graphics.setBackground(Color.WHITE);
        graphics.setColor(Color.GRAY);
        graphics.clearRect(0, 0, GoogleMapsUtils.defaultWidth, 100);

        graphics.drawString("Not Available", 30, 30);

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    return img;
}

From source file:org.limy.eclipse.qalab.outline.sequence.SequenceImageCreator.java

/**
 * V?[PX?}?? layoutData, pngFile ?o?B// w ww  .  jav  a  2s  .co  m
 * @param root V?[PX?}Bean
 * @throws IOException I/OO
 * @throws ParserException 
 */
private void writeSequence(SequenceBean root) throws IOException, ParserException {
    StringWriter out = new StringWriter();
    Context context = new VelocityContext();
    context.put("root", root);
    VelocitySupport.write(new File(LimyQalabPlugin.getDefault().getPluginRoot(), "resource/sequence/index.vm")
            .getAbsolutePath(), context, out);
    File txtFile = LimyQalabUtils.createTempFile(env.getProject(), "sequence.txt");
    pngFile = LimyQalabUtils.createTempFile(env.getProject(), "sequence.png");
    FileUtils.writeByteArrayToFile(txtFile, out.toString().getBytes());

    Parser parser = ParserFactory.getInstance().getDefaultParser();
    NodeFactory nodeFactory = ParserFactory.getInstance().getNodeFactoryForParser(parser);
    Diagram diagram = new Diagram(parser, nodeFactory);

    PushbackReader reader = new PushbackReader(new FileReader(txtFile));
    try {
        diagram.parse(reader);
    } finally {
        reader.close();
    }

    Model model = new Model(new ExceptionHandler() {
        public void exception(Exception e) {
            e.printStackTrace();
        }
    }, diagram);

    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bi.createGraphics();
    layoutData = new LayoutData(new SwingStringMeasure(graphics));
    diagram.layout(layoutData);

    int height = layoutData.getHeight();
    int width = layoutData.getWidth();

    BufferedImage png = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D pngGraphics = png.createGraphics();
    pngGraphics.setClip(0, 0, width, height);
    Map<Key, Object> hintsMap = new HashMap<Key, Object>();
    hintsMap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    pngGraphics.addRenderingHints(hintsMap);
    pngGraphics.setBackground(Prefs.getColorValue(Prefs.BACKGROUND_COLOR));
    pngGraphics.fillRect(0, 0, width, height);

    SwingPainter painter = new SwingPainter();
    painter.setGraphics(pngGraphics);
    model.layout(layoutData);
    layoutData.paint(painter);

    ImageIO.write(png, "png", pngFile);
}