awt
Draw using Antialiasing example
In this example we are going to see how to draw an image with Antialiasing enabled. The notion of antialiasing is one of the most famous among the graphics world. This will help you to make sharper graphics and make your images look very clear and avoid pixelation.
In short, in order to enable antialiasing in your drawing, you should:
- Use
Graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
to turn antialiasin on. - Use
Graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
to turn antialiasing off.
Let’s see the code snippet that follows:
package com.javacodegeeks.snippets.desktop; import java.awt.Component; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; public class AntialiasingDrawing { public static void main(String[] args) { // Create a frame Frame frame = new Frame(); // Add a component with a custom paint method frame.add(new CustomPaintComponent()); // Display the frame int frameWidth = 300; int frameHeight = 300; frame.setSize(frameWidth, frameHeight); frame.setVisible(true); } /** * To draw on the screen, it is first necessary to subclass a Component * and override its paint() method. The paint() method is automatically called * by the windowing system whenever component's area needs to be repainted. */ static class CustomPaintComponent extends Component { public void paint(Graphics g) { // Retrieve the graphics context; this object is used to paint shapes Graphics2D g2d = (Graphics2D)g; /** * The coordinate system of a graphics context is such that the * origin is at the northwest corner and x-axis increases toward the * right while the y-axis increases toward the bottom */ int x = 0; int y = 0; int width = getSize().width-1; int height = getSize().height-1; // Enable antialiasing for shapes g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw an oval that fills the window g2d.drawOval(width/8,height/8, 3*width/4, 3*height/4); // Disable antialiasing for shapes g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); // Draw an oval that fills half window g2d.drawOval(width/4, height/4, width/2, height/2); // Enable antialiasing for text g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Set the desired font if different from default font Font font = new Font("Serif", Font.PLAIN, 12); g2d.setFont(font); FontMetrics fontMetrics = g2d.getFontMetrics(); // Draw a string such that the top-left corner is at x, y g2d.drawString("Antialiazing is ON", x, y+fontMetrics.getAscent()); // Disable antialiasing for text g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Draw a string below the last one g2d.drawString("Antialiazing is OFF", x, y+2*fontMetrics.getAscent()); } } }
This was ane example on how to draw using Antialiasing.