public class RoundedLiveFilter extends JTextField{
private Shape shape;
private final int arcWidth = 30; //vorher 15 & 15!
private final int arcHeight = 30;
private final String placeholderText = "Search";
public RoundedLiveFilter(){
super(15);
setOpaque(false);
// setPlaceholderText(placeholderText);
}
private void setPlaceholderText(String placeholderText){
setForeground(Color.GRAY);
setText(placeholderText);
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, arcWidth, arcHeight);
super.paintComponent(g);
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D gr = (Graphics2D)g.create(); //Graphics2D-Objekt benötigt für Antialiasing...
gr.setColor(getForeground());
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, arcWidth, arcHeight);
paintIcon(gr);
}
private void paintIcon(Graphics2D gr){
Rectangle clip = gr.getClipBounds();
// float width = (float) clip.getWidth();
float height = (float) clip.getHeight()/4;
gr.setColor(new Color(101, 101, 101));
gr.setStroke(new BasicStroke(2f));
gr.draw(new Ellipse2D.Float(height, height, 9, 9));
gr.draw(new Line2D.Float(height+8, height+8, 19.5f, 19.5f));
}
@Override
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, arcWidth, arcHeight);
}
return shape.contains(x, y);
}