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:org.kalypso.contribs.eclipse.jobs.BufferPaintJob.java

/**
 * Configures the graphics-context before actual painting is started (i.e. {@link IPaintable#paint(Graphics2D, IProgressMonitor)} is called).<br>
 * Default behaviour is to set activate anti-aliasing (normal and text).<br>
 * Overwrite to change./*ww w .  j  a va 2  s.com*/
 */
private void configureGraphics(final Graphics2D gr) {
    gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}

From source file:com.epiq.bitshark.ui.FrequencyDomainMouseMarker.java

/**
 * Draws the marker//  ww w .j a v a 2s .  co m
 * @param g2
 * @param rect
 */
public void draw(Graphics2D g2, Rectangle2D rect) {

    g2.setColor(outlineColor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.fillOval((int) Math.round(rect.getX()), (int) Math.round(rect.getY()), (int) Math.round(rect.getWidth()),
            (int) Math.round(rect.getHeight()));
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

@Override
public BufferedImage createCaptcha(char[] text) {
    if (text == null || text.length == 0) {
        throw new IllegalArgumentException("No captcha text given");
    }/*from   w  w  w  . j av  a2s.  c o  m*/

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setBackground(Color.WHITE);
    g2d.setColor(Color.BLACK);

    clearCanvas(g2d);

    if (showGrid) {
        drawGrid(g2d);
    }

    int charMaxWidth = width / text.length;
    int xPos = 0;
    for (char ch : text) {
        drawCharacter(g2d, ch, xPos, charMaxWidth);
        xPos += charMaxWidth;
    }

    g2d.dispose();
    return image;
}

From source file:savant.view.variation.swing.VariantMap.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Paint a gradient from top to bottom
    int h = getHeight();
    int w = getWidth();
    GradientPaint gp0 = new GradientPaint(0, 0, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_TOP), 0,
            h, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_BOTTOM));
    g2.setPaint(gp0);/*w  ww  .jav a  2  s. co m*/
    g2.fillRect(0, 0, w, h);

    List<VariantRecord> data = controller.getData();
    if (data != null && !data.isEmpty()) {
        double boxTop = Double.NaN;
        double boxBottom = Double.NaN;
        Range browseRange = LocationController.getInstance().getRange();

        int participantCount = controller.getParticipantCount();
        unitHeight = (double) h / data.size();
        unitWidth = (double) w / participantCount;

        boolean gappable = unitHeight > GAP_HEIGHT * 2.0;

        ColourScheme cs = new ColourScheme(ColourKey.A, ColourKey.C, ColourKey.G, ColourKey.T,
                ColourKey.INSERTED_BASE, ColourKey.DELETED_BASE, ColourKey.N);
        ColourAccumulator accumulator = new ColourAccumulator(cs);

        double y = 0.0;
        double topGap = 0.0;
        for (int i = 0; i < data.size(); i++) {
            VariantRecord varRec = data.get(i);

            double bottomGap = 0.0;
            if (gappable && i + 1 < data.size()) {
                VariantRecord nextRec = data.get(i + 1);
                if (nextRec.getPosition() - varRec.getPosition() > 1) {
                    bottomGap = GAP_HEIGHT * 0.5;
                }
            }

            if (Double.isNaN(boxTop) && browseRange.getFrom() <= varRec.getPosition()
                    && browseRange.getTo() >= varRec.getPosition()) {
                boxTop = y + topGap;
                boxBottom = boxTop + unitHeight;
            } else if (varRec.getPosition() <= browseRange.getTo()) {
                boxBottom = y + unitHeight - bottomGap;
            }
            double x = 0.0;
            for (int j = 0; j < participantCount; j++) {
                VariantTrackRenderer.accumulateZygoteShapes(varRec.getVariantsForParticipant(j), accumulator,
                        new Rectangle2D.Double(x, y + topGap, unitWidth, unitHeight - topGap - bottomGap));
                x += unitWidth;
            }
            topGap = bottomGap;
            y += unitHeight;
        }
        accumulator.fill(g2);

        if (gappable) {
            drawGapSizes(g2);
        } else {
            // Not enough room to draw gaps, so just label the axes.
            labelVerticalAxis(g2);
        }

        g2.setClip(null);
        g2.setColor(Color.BLUE);
        g2.draw(new Rectangle2D.Double(0.0, boxTop, w - 1.0, boxBottom - boxTop - 1.0));
    }
}

From source file:RadialGradientApp.java

@Override
protected void paintComponent(Graphics g) {
    setFont(getFont().deriveFont(70.f).deriveFont(Font.BOLD));

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Retains the previous state
    Paint oldPaint = g2.getPaint();

    // Fills the circle with solid blue color
    g2.setColor(new Color(0x0153CC));
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds shadows at the top
    Paint p;// ww  w .  j a v  a  2  s. c om
    p = new GradientPaint(0, 0, new Color(0.0f, 0.0f, 0.0f, 0.4f), 0, getHeight(),
            new Color(0.0f, 0.0f, 0.0f, 0.0f));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds highlights at the bottom 
    p = new GradientPaint(0, 0, new Color(1.0f, 1.0f, 1.0f, 0.0f), 0, getHeight(),
            new Color(1.0f, 1.0f, 1.0f, 0.4f));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Creates dark edges for 3D effect
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 2.0f,
            new float[] { 0.0f, 1.0f },
            new Color[] { new Color(6, 76, 160, 127), new Color(0.0f, 0.0f, 0.0f, 0.8f) });
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds oval inner highlight at the bottom
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() * 1.5), getWidth() / 2.3f,
            new Point2D.Double(getWidth() / 2.0, getHeight() * 1.75 + 6), new float[] { 0.0f, 0.8f },
            new Color[] { new Color(64, 142, 203, 255), new Color(64, 142, 203, 0) },
            RadialGradientPaint.CycleMethod.NO_CYCLE, RadialGradientPaint.ColorSpaceType.SRGB,
            AffineTransform.getScaleInstance(1.0, 0.5));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds oval specular highlight at the top left
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 1.4f,
            new Point2D.Double(45.0, 25.0), new float[] { 0.0f, 0.5f },
            new Color[] { new Color(1.0f, 1.0f, 1.0f, 0.4f), new Color(1.0f, 1.0f, 1.0f, 0.0f) },
            RadialGradientPaint.CycleMethod.NO_CYCLE);
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Restores the previous state
    g2.setPaint(oldPaint);

    // Draws the logo        
    //        FontRenderContext context = g2.getFontRenderContext();
    //        TextLayout layout = new TextLayout("R", getFont(), context);
    //        Rectangle2D bounds = layout.getBounds();
    //        
    //        float x = (getWidth() - (float) bounds.getWidth()) / 2.0f;
    //        float y = (getHeight() + (float) bounds.getHeight()) / 2.0f;
    //        
    //        g2.setColor(Color.WHITE);
    //        layout.draw(g2, x, y);
    //        
    //        Area shadow = new Area(layout.getOutline(null));
    //        shadow.subtract(new Area(layout.getOutline(AffineTransform.getTranslateInstance(1.0, 1.0))));
    //        g2.setColor(Color.BLACK);
    //        g2.translate(x, y);
    //        g2.fill(shadow);
    //        g2.translate(-x, -y);
}

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Creates a thumbnail//from ww  w .  j  a  va  2  s.co  m
 * 
 * @param is data source
 * @param os data source
 * @throws IOException if there is a problem reading is
 */
public static void createThumbnail(final InputStream is, final OutputStream os, final int target)
        throws IOException {
    final float compression = 0.85F;
    ImageWriter writer = null;
    MemoryCacheImageOutputStream mos = null;
    Graphics2D graphics2D = null;
    try {
        final BufferedImage bi = ImageIO.read(is);
        final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG");
        if (!iter.hasNext()) {
            throw new IllegalStateException("can't find JPG subsystem");
        }

        int w = bi.getWidth(), h = bi.getHeight();
        if (w < target && h < target) {
            // nothing to recalculate, ya es chiquita.
        } else {
            if (w > h) {
                h = target * bi.getHeight() / bi.getWidth();
                w = target;
            } else {
                w = target * bi.getWidth() / bi.getHeight();
                h = target;
            }
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, w, h, null);

        writer = (ImageWriter) iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(compression);
        mos = new MemoryCacheImageOutputStream(os);
        writer.setOutput(mos);
        writer.write(null, new IIOImage(thumbImage, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (mos != null) {
            mos.close();
        }
        if (graphics2D != null) {
            graphics2D.dispose();
        }
        is.close();
        os.close();
    }
}

From source file:org.aludratest.cloud.selenium.SeleniumResourceBean.java

private byte[] takeSeleniumResourceScreenshot(String seleniumUrl) {
    String url = seleniumUrl;// w w  w .  ja v  a  2  s  . co m
    url += "/selenium-server/driver/?cmd=captureScreenshotToString";

    InputStream in = null;
    try {
        in = new URL(url).openStream();
        in.read(new byte[3]); // read away "OK,"
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(in, baos);

        // decode Base64
        byte[] rawImageData = Base64.decodeBase64(baos.toByteArray());

        // create image from bytes
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(rawImageData));

        // shrink image
        float sizeFactor = 2;
        BufferedImage imgSmall = new BufferedImage((int) (img.getWidth() / sizeFactor),
                (int) (img.getHeight() / sizeFactor), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = imgSmall.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(img, 0, 0, imgSmall.getWidth(), imgSmall.getHeight(), 0, 0, img.getWidth(),
                img.getHeight(), null);
        g2d.dispose();

        // get PNG bytes
        baos = new ByteArrayOutputStream();
        ImageIO.write(imgSmall, "png", baos);
        return baos.toByteArray();
    } catch (IOException e) {
        LOG.warn("Could not take Selenium screenshot: " + e.getMessage());
        return null;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    // Retrieve the graphics context; this object is used to paint shapes
    Graphics2D g2d = (Graphics2D) g;

    // Determine if antialiasing is enabled
    RenderingHints rhints = g2d.getRenderingHints();
    boolean antialiasOn = rhints.containsValue(RenderingHints.VALUE_ANTIALIAS_ON);

    // Enable antialiasing for shapes
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

}

From source file:ddf.catalog.transformer.input.pdf.PdfInputTransformer.java

private byte[] generatePdfThumbnail(PDDocument pdfDocument) throws IOException {

    PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);

    if (pdfDocument.getNumberOfPages() < 1) {
        /*/*from   ww w  .  j  a va 2s  .  c om*/
         * Can there be a PDF with zero pages??? Should we throw an error or what? The
         * original implementation assumed that a PDF would always have at least one
         * page. That's what I've implemented here, but I don't like make those
         * kinds of assumptions :-( But I also don't want to change the original
         * behavior without knowing how it will impact the system.
         */
    }

    PDPage page = pdfDocument.getPage(0);

    BufferedImage image = pdfRenderer.renderImageWithDPI(0, RESOLUTION_DPI, ImageType.RGB);

    int largestDimension = Math.max(image.getHeight(), image.getWidth());
    float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension;
    int scaledHeight = (int) (image.getHeight() * scalingFactor);
    int scaledWidth = (int) (image.getWidth() * scalingFactor);

    BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = scaledImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
    graphics.dispose();

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        ImageIOUtil.writeImage(scaledImage, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY);
        return outputStream.toByteArray();
    }
}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

private void drawCircle(Graphics2D g) {
    g.setStroke(new BasicStroke(1));
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawOval(-arrowHeight / 2, -arrowHeight / 2, arrowHeight, arrowHeight);
}