Java 2D Rendering Hints

Rendering hints request Java 2D how you would like rendering to be performed. Rendering hints trade-off quality and performance. All rendering hints are set as follows:

public void setRenderingHint(RenderingHints.Key key, Object value)

Specifying a rendering hint does not guarantee that it will be used, as not all platforms support all rendering hints.

When a rendering hint is set to default, the platform rendering default is used.

Antialiasing Text

The text antialiasing keyword is:

RenderingHints.KEY_TEXT_ANTIALIAS

Antialiasing can be turned on or off. If it is turned on, it provides a much
better image quality, but rendering takes longer.

RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT
RenderingHints.VALUE_TEXT_ANTIALIAS_ON
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF

RenderingHints_AntialiasText_Demo Example (Run Applet)

import java.awt.*;
import javax.swing.*;

public class RenderingHints_AntialiasText_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            final Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Times New Roman", Font.ITALIC, 250));

            // draw without anti-aliasing
            g2.drawString("A", 20, 200);

            // draw with anti-aliasing
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2.drawString("A", 180, 200);
        }
    }
}

Antialiasing Shapes

The shape antialiasing keyword is:

RenderingHints.KEY_ANTIALIAS

Antialiasing can be turned on or off. If it is turned on, it provides a much better image quality, but rendering takes longer.

RenderingHints.VALUE_ANTIALIAS_DEFAULT
RenderingHints.VALUE_ANTIALIAS_ON
RenderingHints.VALUE_ANTIALIAS_OFF

RenderingHints_AntialiasShapes_Demo Example (Run Applet)

import java.awt.*;
import javax.swing.*;

public class RenderingHints_AntialiasShapes_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            final Graphics2D g2 = (Graphics2D) g;
            final int gap = 5;

            // draw without antialiasing
            g2.drawOval(0, 0, getWidth() / 2 - gap, getHeight() - gap);

            // draw with antialiasing
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.drawOval(getWidth() / 2, 0, getWidth() / 2 - gap, getHeight() - gap);
        }
    }
}

Scaling Images

RenderingHints.KEY_INTERPOLATION

Various hints can be used to control image scaling quality. The fastest, but poorest quality, scaling is:

RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR

It can be particularly bad if you are scaling down, where it can result in a lot of the original information being lost in the scaled image.

A reasonably good quality scaling with reasonably fast processing is:

RenderingHints.VALUE_INTERPOLATION_BILINEAR

For best quality and the slowest processing, use:

RenderingHints.VALUE_INTERPOLATION_BICUBIC

RenderingHints_Scale_Demo Example (Run Applet)

import java.awt.*;
import javax.swing.*;

public class RenderingHints_Scale_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();
        }

        @Override
        public void paintComponent(Graphics g)
        {
            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/moon.gif")).getImage();
            final Graphics2D g2 = (Graphics2D) g;
            final int width = getWidth();
            final int height = getHeight();

            // default
            g2.drawImage(image, 0, 0, width / 2, height / 2, this);

            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            g2.drawImage(image, width / 2, 0, width / 2, height / 2, this);

            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(image, 0, height / 2, width / 2, height / 2, this);

            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.drawImage(image, width / 2, height / 2, width / 2, height / 2, this);
        }
    }
}

Other Rendering Hints

Other rendering hints include:

 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>