I'm getting extremely pixilated corners when i try to make a rounded rectangle. Is there any way to smooth them out?
Here's an image (Notice the corners):

Here is the code for the Button that I subclass and override the paint method (The one with the pixilated corner):
public class ControlButton extends JButton {
    public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
    public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
    public ControlButton(String text) {
        setText(text);
    }
    public ControlButton() {
    }
    @Override
    protected void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setPaint(new GradientPaint(
                new Point(0, 0), 
                BUTTON_TOP_GRADIENT, 
                new Point(0, getHeight()), 
                BUTTON_BOTTOM_GRADIENT));
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
        g2.dispose();
    }
}
Try this:
RenderingHints qualityHints = new RenderingHints(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON );
qualityHints.put(
  RenderingHints.KEY_RENDERING,
  RenderingHints.VALUE_RENDER_QUALITY );
g2.setRenderingHints( qualityHints );
Take a look at the documentation:
Code:
import javax.swing.*;
import java.awt.*;
public class ControlButton extends JButton {
  public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
  public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
  public ControlButton(String text) {
    setText(text);
  }
  public ControlButton() {
  }
  @Override
  protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g.create();
    RenderingHints qualityHints =
      new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setRenderingHints(qualityHints);
    g2.setPaint(new GradientPaint(new Point(0, 0), BUTTON_TOP_GRADIENT, new Point(0, getHeight()),
                                  BUTTON_BOTTOM_GRADIENT));
    g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
    g2.dispose();
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new ControlButton("Hello, World"));
    frame.pack();
    frame.setVisible(true);
  }
}
I did this:
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
I feel it gave me smoother edges than Dave Jarvis' method but I could be wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With