Example usage for java.awt Graphics2D setRenderingHints

List of usage examples for java.awt Graphics2D setRenderingHints

Introduction

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

Prototype

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

Source Link

Document

Replaces the values of all preferences for the rendering algorithms with the specified hints .

Usage

From source file:FontRenderContextRenderingHints.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2D.setRenderingHints(qualityHints);

    AffineTransform at = new AffineTransform();
    at.setToTranslation(-300, -400);/*w  w w  .  j a  va 2  s .  c o  m*/
    at.shear(-0.5, 0.0);
    FontRenderContext frc = new FontRenderContext(at, false, false);
    TextLayout tl = new TextLayout("World!", font, frc);
    Shape outline = tl.getOutline(null);
    g2D.setColor(Color.blue);

    BasicStroke wideStroke = new BasicStroke(2.0f);
    g2D.setStroke(wideStroke);
    g2D.draw(outline);

}

From source file:Main.java

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;

    int bottomLineY = height - thickness - shadowPad;

    RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(0 + strokePad, 0 + strokePad,
            width - thickness - shadowPad, bottomLineY, radius, radius);

    Area area = new Area(bubble);

    g2.setRenderingHints(hints);

    g2.setColor(color);//from  w  w w . ja  v a  2s .  c om
    g2.setStroke(stroke);
    g2.draw(area);

    Area shadowArea = new Area(new Rectangle(0, 0, width, height));
    shadowArea.subtract(area);
    g.setClip(shadowArea);
    Color shadow = new Color(color.getRed(), color.getGreen(), color.getBlue(), 128);
    g2.setColor(shadow);
    g2.translate(shadowPad, shadowPad);
    g2.draw(area);
}

From source file:business.model.CaptchaModel.java

/**
 *
 * @param CaptchaText//from ww  w  .  j  av a2  s  .c  om
 * @return
 */
public static BufferedImage CreateCaptchaImageOld(String CaptchaText) {
    BufferedImage localBufferedImage = new BufferedImage(width, height, 1);
    try {
        Graphics2D localGraphics2D = localBufferedImage.createGraphics();
        //        List<Font> fonts = FontFactory.getListFont();
        List<Font> fonts = new ArrayList<Font>();
        if (fonts.isEmpty()) {
            fonts.add(new Font("Nimbus Roman No9 L", 1, 30));
            fonts.add(new Font("VN-NTime", 1, 30));
            fonts.add(new Font("DT-Times", 1, 30));
            fonts.add(new Font("Times New Roman", 1, 30));
            fonts.add(new Font("Vni-Book123", 1, 30));
            fonts.add(new Font("VNI-Centur", 1, 30));
            fonts.add(new Font("DT-Brookly", 1, 30));
        }
        //        fonts.add(loadFont("BINHLBI.TTF"));
        RenderingHints localRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        localRenderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        localGraphics2D.setRenderingHints(localRenderingHints);
        localGraphics2D.fillRect(0, 0, width, height);
        localGraphics2D.setColor(new Color(50, 50, 50));
        Random localRandom = new Random();
        int i = 0;
        int j = 0;
        for (int k = 0; k < CaptchaText.length(); k++) {
            i += 25 + Math.abs(localRandom.nextInt()) % 5;
            j = 25 + Math.abs(localRandom.nextInt()) % 20;
            int tmp = localRandom.nextInt(fonts.size() - 1);
            localGraphics2D.setFont(fonts.get(tmp));

            localGraphics2D.drawChars(CaptchaText.toCharArray(), k, 1, i, j);
        }
        localBufferedImage = getDistortedImage(localBufferedImage);
        Graphics2D localGraphics2D2 = localBufferedImage.createGraphics();
        localGraphics2D2.setRenderingHints(localRenderingHints);
        //        localGraphics2D2.fillRect(0, 0, width, height);
        localGraphics2D2.setColor(new Color(50, 50, 50));
        CubicCurve2D c = new CubicCurve2D.Double();// draw QuadCurve2D.Float with set coordinates
        for (int l = 0; l < 7; l++) {
            int x1 = Util.rand(0, width / 2);
            ;
            int x2 = Util.rand(width / 2, width);
            int y1 = Util.rand(0, height);
            int y2 = Util.rand(0, height);
            int ctrlx1 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly1 = y1;
            int ctrlx2 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly2 = y2;

            c.setCurve(x1, y1, ctrlx2, ctrly2, ctrlx1, ctrly1, x2, y2);
            localGraphics2D2.draw(c);
            //            localGraphics2D2.drawLine(randomX1(), randomY1(), randomX2(), randomY2());
        }

        localGraphics2D.dispose();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return null;
    }
    return localBufferedImage;
}

From source file:dk.dma.epd.common.prototype.gui.voct.VOCTAdditionalInfoPanel.java

/**
 * {@inheritDoc}/*from  w  w w . ja  va  2  s.c om*/
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(GraphicsUtil.ANTIALIAS_HINT);

    // Define the content rectangle
    int x0 = pointerLeft ? pad + pointerWidth : pad;
    RoundRectangle2D.Double content = new RoundRectangle2D.Double(x0, pad, width - 2 * pad - pointerWidth,
            height - 2 * pad, cornerRadius, cornerRadius);

    // Define the pointer triangle
    int xp = pointerLeft ? pad + pointerWidth : width - pad - pointerWidth;
    int yp = pad + height - pointerFromBottom;
    Polygon pointer = new Polygon();
    pointer.addPoint(xp, yp);
    pointer.addPoint(xp, yp - pointerHeight);
    pointer.addPoint(xp + pointerWidth * (pointerLeft ? -1 : 1), yp - pointerHeight / 2);

    // Combine content rectangle and pointer into one area
    Area area = new Area(content);
    area.add(new Area(pointer));

    // Fill the pop-up background
    Color col = pointerLeft ? c.getBackground().darker() : c.getBackground().brighter();
    g2.setColor(col);
    g2.fill(area);

}

From source file:io.github.dsheirer.spectrum.SpectrumPanel.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D graphics = (Graphics2D) g;

    graphics.setBackground(mColorSpectrumBackground);

    graphics.setRenderingHints(RENDERING_HINTS);

    drawSpectrum(graphics);//from w ww  . jav  a 2s . c  o m
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreeRenderer.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension d = getSize();//from  www.  j  a  v a2  s .c o  m
    //System.out.println("d: "+d+"     "+g.getClipBounds());

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHints(renderingHints);

    FontMetrics fm = g2d.getFontMetrics();
    int imgY = img1 != null ? (d.height - 24) / 2 : 0;
    int imgY2 = img1 != null ? (d.height - 16) / 2 : 0;
    int txtY = ((d.height - fm.getHeight()) / 2) + fm.getAscent();
    int x = 0;

    Color color = g2d.getColor();

    if (img1 != null) {
        g2d.drawImage(img1.getImage(), x, imgY, null);
        x += img1.getIconWidth();
    }

    int iconInx = 0;

    if (txt1 != null) {
        x += getIconTextGap();
        g2d.setColor(getForeground());
        g.drawString(txt1, x, txtY);
        x += fm.stringWidth(txt1);
        g2d.setColor(color);
    }

    if (isContainer) {
        //if (isSelected  && isEditable)
        //{
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete the container
        //}

        if (hasColObj) {
            if (img2 != null) {
                x += 1;
                x += iconSep;
                g2d.drawImage(img2.getImage(), x, imgY2, null);
                x += img2.getIconWidth();
            }

            if (txt2 != null) {
                x += getIconTextGap();
                g2d.setColor(getForeground());
                g.drawString(txt2, x, txtY);
                x += fm.stringWidth(txt2);
                g2d.setColor(color);
            }

            if (isSelected) {
                x += iconSep;
                x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++);

                if (isEditable) {
                    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++);
                }
            }
        } else if (isSelected) // No Col Obj
        {
            x += iconSep;
            x += drawIcon(g2d, x, imgY2, schImgIcon, iconInx++);
            x += drawIcon(g2d, x, imgY2, addImgIcon, iconInx++);
        }

    } else if (isSelected) {
        x += iconSep;
        x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++); // View for Collection Object

        //if (!isViewMode)
        //{
        //    x += iconSep;
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete for Collection Object
        //}
    }

    g2d.dispose();
}

From source file:gg.msn.ui.panel.MainPanel.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    super.paintComponent(g2d);
    g2d.setRenderingHints(
            new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));

    try {/*from www  .  java 2 s.  c  om*/

        ImageIcon icon = ThemeManager.getTheme().get(ThemeManager.MAIN_BACKGROUND);

        if (icon != null) {
            Image image = icon.getImage();
            g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }

        icon = ThemeManager.getTheme().get(ThemeManager.MAIN_IMAGE);

        if (icon != null) {

            Image cartel = icon.getImage();
            g2d.drawImage(cartel, 0 - cartel.getWidth(this) / 10, getHeight() - cartel.getHeight(this), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }
    } catch (Exception e) {
        //            log.error(e);
    }
}

From source file:dk.dma.epd.common.prototype.gui.notification.ChatServicePanel.java

/**
 * {@inheritDoc}/*from   ww  w  .  j a  va 2  s.  co m*/
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(GraphicsUtil.ANTIALIAS_HINT);

    // Define the content rectangle
    int x0 = pointerLeft ? pad + pointerWidth : pad;
    RoundRectangle2D.Double content = new RoundRectangle2D.Double(x0, pad, width - 2 * pad - pointerWidth,
            height - 2 * pad, cornerRadius, cornerRadius);

    // Define the pointer triangle
    int xp = pointerLeft ? pad + pointerWidth : width - pad - pointerWidth;
    int yp = pad + height - pointerFromBottom;
    Polygon pointer = new Polygon();
    pointer.addPoint(xp, yp);
    pointer.addPoint(xp, yp - pointerHeight);
    pointer.addPoint(xp + pointerWidth * (pointerLeft ? -1 : 1), yp - pointerHeight / 2);

    // Combine content rectangle and pointer into one area
    Area area = new Area(content);
    area.add(new Area(pointer));

    // Fill the pop-up background
    Color col = pointerLeft ? c.getBackground().darker() : c.getBackground().brighter();
    g2.setColor(col);
    g2.fill(area);

    if (message.getSeverity() == MaritimeTextingNotificationSeverity.WARNING
            || message.getSeverity() == MaritimeTextingNotificationSeverity.ALERT
            || message.getSeverity() == MaritimeTextingNotificationSeverity.SAFETY) {
        g2.setStroke(new BasicStroke(2.0f));

        switch (message.getSeverity()) {
        case WARNING:
            g2.setColor(WARN_COLOR);
        case ALERT:
            g2.setColor(ALERT_COLOR);
        case SAFETY:
            g2.setColor(SAFETY_COLOR);
        default:
            g2.setColor(WARN_COLOR);
        }

        // g2.setColor(message.getSeverity() == MaritimeTextingNotificationSeverity.WARNING ? WARN_COLOR : ALERT_COLOR);

        g2.draw(area);
    }
}

From source file:org.kuali.mobility.people.service.PeopleServiceImpl.java

@Override
public BufferedImage generateObfuscatedImage(String text) {
    int width = 250;
    int height = 25;

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = bufferedImage.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 14);
    g2d.setFont(font);//from  w  w  w . j  a  v  a 2s.  com

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.setRenderingHints(rh);

    Paint bg = new Color(255, 255, 255);
    g2d.setPaint(bg);
    g2d.fillRect(0, 0, width, height);

    int x = 0;
    int y = height - 7;

    Paint textPaint = new Color(0, 0, 0);
    g2d.setPaint(textPaint);
    g2d.drawString(text, x, y);

    g2d.dispose();
    return bufferedImage;
}

From source file:org.hydroponics.dao.JDBCHydroponicsDaoImpl.java

public byte[] getThumbnail(BufferedImage buffImage) throws IOException {
    BufferedImage pDestImage = new BufferedImage(Constants.THUMBNAIL_WIDTH, Constants.THUMBNAIL_HEIGHT,
            BufferedImage.TYPE_3BYTE_BGR);

    AffineTransform transform = new AffineTransform();
    transform.scale((float) Constants.THUMBNAIL_WIDTH / (float) buffImage.getWidth(),
            (float) Constants.THUMBNAIL_HEIGHT / (float) buffImage.getHeight());

    Graphics2D g = (Graphics2D) pDestImage.getGraphics();

    //set the rendering hints for a good thumbnail image
    Map m = g.getRenderingHints();
    m.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    m.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    m.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    m.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHints(m);

    g.drawImage(buffImage, transform, null);
    g.dispose();/* w ww.  j  a va  2s  . c o  m*/

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(pDestImage, "JPEG", out);
    return out.toByteArray();
}