Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to smooth out corners in rounded rectangle, Swing?

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):

enter image description here

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();
    }
}
like image 624
sinθ Avatar asked Jan 14 '23 05:01

sinθ


2 Answers

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:

  • http://docs.oracle.com/javase/tutorial/2d/advanced/quality.html

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);
  }
}
like image 71
Dave Jarvis Avatar answered Jan 21 '23 23:01

Dave Jarvis


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.

like image 33
Pouria P Avatar answered Jan 21 '23 23:01

Pouria P