See the effect, the line in middle is set VALUE_ANTIALIAS_ON:
package javaswing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaTestSwing {
static JFrameWin jFrameWindow;
public static class MyComponent extends JComponent{
Dimension myDimension = new Dimension(300, 500);
@Override
public Dimension getPreferredSize() {
return myDimension;
}
@Override
public Dimension getMaximumSize() {
return myDimension;
}
@Override
public Dimension getMinimumSize() {
return myDimension;
}
@Override
protected void paintComponent(Graphics g) {
//Create a Graphics2D object from g
Graphics2D graphics2D = (Graphics2D)g;
//Default Antialiasing
graphics2D.drawLine(50, 50, 250, 250);
//Antialiasing ON
graphics2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawLine(50, 150, 250, 350);
//Antialiasing OFF
graphics2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
graphics2D.drawLine(50, 250, 250, 450);
}
}
public static class JFrameWin extends JFrame{
public JFrameWin(){
this.setTitle("java-buddy.blogspot.com");
this.setSize(300, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent myComponent = new MyComponent();
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(myComponent);
this.add(horizontalBox);
}
}
public static void main(String[] args){
Runnable doSwingLater = new Runnable(){
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(doSwingLater);
}
}