2

i used class to make rounded border

the class is :

public class RoundedBorder implements Border {
        int radius;

        public RoundedBorder(int radius) {
            this.radius = radius;
        }
    @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius/2, this.radius, this.radius/2, this.radius);
        }
    @Override
        public boolean isBorderOpaque() {
            return true;
        }
    @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D graphics = (Graphics2D) g;
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

            g.drawRoundRect(x,y,width-1,height-1,radius,radius);           
        }
    }

and for button i used :

JTextField login_nickname = new JTextField();

login_nickname.setBorder(new RoundedBorder(10));
login_nickname.setPreferredSize(new Dimension(150, 25));

and it's worked fine , but i want remove unused background outside rounded border in corner , i attached image to explain more what i mean,

enter image description here

thank you

Kara
  • 6,115
  • 16
  • 50
  • 57
Jason4Ever
  • 1,439
  • 4
  • 23
  • 43
  • 1) *"i used class to make.."* Please use shift key to make upper case letter at the start of sentences, as well as for the word 'I'. Doing so helps the reader. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 20 '12 at 11:34
  • Possible duplicate - http://stackoverflow.com/questions/8416295/component-painting-outside-custom-border – mre Mar 20 '12 at 12:18

2 Answers2

2

I would do something like this in paintBorder():

@Override
 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
     Graphics2D graphics = (Graphics2D) g;
     graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     if (c.getParent() != null) {
         Color bc = g.getColor();
         g.setColor(c.getParent().getBackground());
         for (int r = 0; r<radius;r++){
            g.drawRoundRect(x, y, width - 1, height - 1, r, r);
         }
         g.setColor(bc);
     }
     g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
 }

If the component has some parent container, i would draw first the border with the background color, then on top of it - my round border.

yggdraa
  • 2,002
  • 20
  • 23
  • Edited the underlying border so that a rounded border will look smooth with any radius – yggdraa Mar 20 '12 at 12:21
  • Thank you very much :) , i have additional small question , if i want to add BevelBorder.RAISED to the painted rounded border , what is method which i will add or will modify ? – Jason4Ever Mar 22 '12 at 09:26
  • You should look into paintBorder method of the BevelBorder.RAISED and make your own border wich uses the similar code but with rounded corners. Beveled border is painted with 2 different colors and by 2 lines if i remember, so you just do the same but using g.drawRoundRect. And keep in mind that if you drow border with 2 lines, inner line radius should be less than outer one :) – yggdraa Mar 22 '12 at 10:18
1

What's returned by the ?

boolean isBorderOpaque();

Should not be 'false'?

StanislavL
  • 56,971
  • 9
  • 68
  • 98