Ask Questions?

View Latest Questions

Advertisement


 
 

Antialiasing
Posted on: July 26, 2006 at 12:00 AM
Antialiased versus Aliased Graphics

Java: Antialiasing

Antialiased versus Aliased Graphics

 


When lines are drawn on the screen, vertical and horizontal lines appear perfectly straight.

 

However, when they are on a diagonal, especially near vertical or horizontal, "jaggies" appear, giving the line a step-like appearance.

 

This can be avoided in Java 2's Graphics2D class by requesting that edges are antialiased.


public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;  // See note below
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    // Use g2 for all following operations
    g2.drawLine(0, 0, 100, 80);
    . . .
}

Casting to Graphics2D

The downcast in the above example from type Graphics to Graphics2D is possible because Graphics2D is a subclass of Graphics, and the parameter of the paintComponent method is already a Graphics2D object.
Copyleft 2003 Fred Swartz MIT License, Last update: 2002-04-21
Advertisement


DMCA.com