user14563118
user14563118

Reputation:

How to remove this blur at the edge of JTextField?

I'm Rounding my JTextField but after the field gets rounded the edges are blurry and don't look professional.

This is the code I'm using to round the JTextField. I'm extending the JTextFiled and changing the paintComponent values.

//Rouding the field
protected void paintComponent(Graphics g) {
    g.setColor(getBackground());
    g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
    super.paintComponent(g);
}

protected void paintBorder(Graphics g) {
    g.setColor(getForeground());
    g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}

public boolean contains(int x, int y) {
    if (shape == null || !shape.getBounds().equals(getBounds())) {
        shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
    }
    return shape.contains(x, y);
}

Upvotes: 2

Views: 85

Answers (1)

camickr
camickr

Reputation: 324118

In your painting methods you can try using antialiasing for the Graphics.

You would set the painting properties on a separate Graphics object:

Graphics2D g2d = (Graphics2D)g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// add painting logic
g2d.dispose();

Upvotes: 2

Related Questions