Academic Java    Java Tutorial  >  Graphics  >  RenderingHints

RenderingHints Example

Java RenderingHints EXAMPLE output The applet draws the same ellipse shape twice, once without RenderingHints and once with, using the method Graphics.setRenderingHint() with parameters RenderingHints.KEY_ANTIALIASING and RenderingHints.VALUE_ANTIALIAS_ON.

The first time there is no correction to the rendering process. And if you look at the rendered ellipse you will see the jaggies, the jagged outlines of the ellipse.

The second drawing uses antialiasing, a technique that smoothes out rough edges. It is generally advisable to set it in the paint() method, as here, where it makes a visible difference
// RenderingHints Example
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

class RenderingHintsExample extends JApplet {

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

      Graphics2D g2 = (Graphics2D)g;

      g2.setStroke(new BasicStroke(10));

      Ellipse2D e = new Ellipse2D.Float(130,50,40,200);
      g2.draw(e);

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

      g2.translate(80,0);
      g2.setPaint(Color.red);
      g2.setStroke(new BasicStroke(10));
      g2.draw(e);

   }
}