Friday 19 July 2013

Rounded Text Fields

     
import javax.swing.*;
import java.awt.*; import javax.swing.plaf.basic.*;
import java.awt.geom.*;
import javax.swing.text.JTextComponent;
public  class RoundedFieldUI extends BasicTextFieldUI{
private int round = 5;
private int shadeWidth = 2;
private int textSpacing = 3;

public void installUI ( JComponent c )
{
    super.installUI ( c );

    c.setOpaque ( false );

    int s = shadeWidth + 1 + textSpacing;
    c.setBorder ( BorderFactory.createEmptyBorder ( s, s, s, s ) );
}

protected void paintSafely ( Graphics g )
{
    Graphics2D g2d = ( Graphics2D ) g;
    g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON );

    Shape border = getBorderShape ();

    Stroke os = g2d.getStroke ();
    //g2d.setStroke ( new BasicStroke ( shadeWidth * 2 ) );
    //g2d.setPaint ( Color.LIGHT_GRAY );
    //g2d.draw ( border );
    g2d.setStroke ( os );

    g2d.setPaint ( Color.WHITE );
    g2d.fill ( border );

    //g2d.setStroke ( new BasicStroke ( 1 ) );
    g2d.setPaint ( Color.LIGHT_GRAY );
    g2d.draw ( border );

    g2d.setPaint ( new Color(0x999999) );
    RoundRectangle2D tr = new RoundRectangle2D.Double(6, shadeWidth,
    getComponent ().getWidth ()- 9, 1, 10, 10);
    g2d.fill(tr);

    g2d.setPaint ( new Color(41, 117, 200) );
    RoundRectangle2D tr2 = new RoundRectangle2D.Double(6,                                                   getComponent ().getHeight ()-shadeWidth,
                             getComponent ().getWidth ()- 12, 1, 10, 10);
    g2d.fill(tr2);
    /*
    g2d.setPaint ( Color.GRAY );
    g2d.draw(new Arc2D.Double(shadeWidth, shadeWidth,
    round,
    round,
    90, 135,
    Arc2D.OPEN));
    */
    super.paintSafely ( g );
}

private Shape getBorderShape ()
{
    JTextComponent component = getComponent ();
    if ( round > 0 )
    {
        return new RoundRectangle2D.Double ( shadeWidth, shadeWidth,
        component.getWidth () - shadeWidth * 2 - 1,
        component.getHeight () - shadeWidth * 2 - 1, round * 2, round * 2 );
    }
    else
    {
        return new Rectangle2D.Double ( shadeWidth, shadeWidth,
        component.getWidth () - shadeWidth * 2 - 1,
        component.getHeight () - shadeWidth * 2 - 1 );
    }
}

public static void main ( String[] args )
{

    try {
        // Set System L&F
        UIManager.setLookAndFeel(
        UIManager.getSystemLookAndFeelClassName());
    }
    catch (UnsupportedLookAndFeelException e) {
        // handle exception
    }
    catch (ClassNotFoundException e) {
        // handle exception
    }
    catch (InstantiationException e) {
        // handle exception
    }
    catch (IllegalAccessException e) {
        // handle exception
    }

    JFrame frame = new JFrame ();
    frame.setUndecorated(true);
    JPanel panel = new JPanel ( new BorderLayout ( 5, 5 ) ){
        private  final Color LIGHT_BLUE = new Color(41, 117, 200);
        private  final Color DARK_BLUE = new Color(2, 47, 106);
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            GradientPaint gradPaint = new GradientPaint(0, 0, LIGHT_BLUE, 0, getHeight(), DARK_BLUE);
            g2.setPaint(gradPaint);
            g2.fillRect(0, 0, getWidth(), getHeight());
        }
    };
    panel.setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
    frame.add ( panel );

    panel.add ( new JLabel ( "Field:" ), BorderLayout.NORTH );

    JTextField field = new JTextField ( 20 );
    field.setFont(new Font("Verdana", Font.PLAIN, 18));

    field.setBorder(BorderFactory.createCompoundBorder(
    field.getBorder(),
    BorderFactory.createEmptyBorder(20, 20, 20, 5)));
    field.setUI ( new RoundedFieldUI () );
    panel.add ( field );

    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}
}http://metamug.com

No comments:

Post a Comment