Enabling Antialiasing - Java 2D Graphics

Java examples for 2D Graphics:Paint

Description

Enabling Antialiasing

Demo Code

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class Main {
  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);

    // Disable antialiasing for shapes
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_OFF);

    // Draw shapes...;

    // Enable antialiasing for text
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // Draw text...; 

    // Disable antialiasing for text
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  }/*from w  w  w .  j  av  a  2  s.com*/
}

Related Tutorials