Java AWT Graphics2D set anti aliasing

Description

Java AWT Graphics2D set anti aliasing


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
  Random rnd = new Random();
  @Override//from   w w  w . j ava 2  s.  co m
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    
    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.BLACK);
    g2d.clearRect(0, 0, getParent().getWidth(), getParent().getHeight());
    // antialising
    // 
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


    // Blue line
    g2d.setPaint(Color.BLUE);
    Stroke solidStroke = new BasicStroke(10, BasicStroke.CAP_ROUND, 
            BasicStroke.JOIN_ROUND);
    g2d.setStroke(solidStroke);
    g2d.draw(new Line2D.Float(new Point2D.Float(200, 10), 
            new Point2D.Double(10, 210)));
  }

  public static void main(String[] args) {
    // create frame for Main
    JFrame frame = new JFrame("java2s.com");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Main Main = new Main();
    frame.add(Main);
    frame.setSize(300, 210);
    frame.setVisible(true);
  }
}



PreviousNext

Related