Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By camickr

Thread: Custom JButton

  1. #1
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Custom JButton

    I would like to create a new button like this one Google Images.
    Should i Override paintComponent() ?
    If yes, how exactly ?

    EDIT: I managed to create the button, here is the code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.RoundRectangle2D;
    
    
    public class JRButton extends JButton {
    	
    	private int w ;
    	private int h ;
    	private RoundRectangle2D re;
    	private final BasicStroke st = new BasicStroke(2f);
    	private double wRatio = 200/15;
    	private double hRatio = 25/12;
    
    	private double arcw;
    	private double arch;
    	
    	public JRButton(String name, int w, int h){
    		
    		super(name);
    		setContentAreaFilled(false);
    		setPreferredSize(new Dimension(w,h));		
    		setFocusable(false);
    		;
    	}
    
    	@Override
    	public void paintComponent(Graphics g){
    		this.w = getWidth();
    		this.h = getHeight();
    		this.arcw = getWidth()/wRatio;
    		this.arch = getHeight()/hRatio;
    		re = new RoundRectangle2D.Double(st.getLineWidth()/2,st.getLineWidth()/2,w-(st.getLineWidth()/0.5), h-(st.getLineWidth()/0.5), arcw, arch);
    
    		GradientPaint push = new GradientPaint((w/2),(h/2),Color.LIGHT_GRAY,(w/2),h,Color.WHITE,false);
    		GradientPaint up = new GradientPaint((w/2),(h/2),Color.WHITE,(w/2),h,Color.LIGHT_GRAY,false);
    		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	
    		if (getModel().isArmed()) {
    			g2.setPaint(push);
    			g2.fill(re);
    		}
    		else{
    			g2.setPaint(up);
    			g2.fill(re);
     		}
    		
    		super.paintComponent(g2);
    			
    	}		
    		
    		
    		
    	
    
    	@Override
    	public void paintBorder(Graphics g){
    		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setColor(Color.GRAY);
    		g2.setStroke(st);
    		g2.draw(re);
    		g2.dispose();
    	
    	}
    /*//Test
    	public static void main(String[] args) {
    		
    		 JFrame j = new JFrame();
    		 JButton po = new JRButton("JRButton",200,28);
    		 j.setLayout(new FlowLayout(FlowLayout.CENTER));
    		 j.getContentPane().add(po);
    		 j.getRootPane().setDefaultButton(po);
    		 j.setSize(400,400);
    		 j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		 j.setVisible(true);
    		 
    	}
    */
    }
    Last edited by adwart; 07-29-2011 at 10:28 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    To override a method, just code its definitional signature in the class where you are extending the class with the method you want to override. To see what the definition should be, read the class's API doc.
    Add an @Override statement just before it to have the compiler check it for you.
    adwart likes this.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Here is some code from an old Java Tech Tip:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class RoundButton extends JButton {
        public RoundButton(String label) {
            super(label);
    
            // These statements enlarge the button so that it
            // becomes a circle rather than an oval.
            Dimension size = getPreferredSize();
            size.width = size.height = Math.max(size.width, size.height);
            setPreferredSize(size);
    
            // This call causes the JButton not to paint the background.
            // This allows us to paint a round background.
            setContentAreaFilled(false);
        }
    
        // Paint the round background and label.
        protected void paintComponent(Graphics g) {
    	if (getModel().isArmed()) {
                // You might want to make the highlight color
                // a property of the RoundButton class.
                g.setColor(Color.lightGray);
            } else {
                g.setColor(getBackground());
            }
    	g.fillOval(0, 0, getSize().width-1, getSize().height-1);
    
            // This call will paint the label and the focus rectangle.
    	super.paintComponent(g);
        }
    
        // Paint the border of the button using a simple stroke.
        protected void paintBorder(Graphics g) {
            g.setColor(getForeground());
            g.drawOval(0, 0, getSize().width-1, getSize().height-1);
        }
    
        // Hit detection.
        Shape shape;
        public boolean contains(int x, int y) {
            // If the button has changed size, make a new shape object.
            if (shape == null || !shape.getBounds().equals(getBounds())) {
                shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
            }
            return shape.contains(x, y);
        }
    
        // Test routine.
        public static void main(String[] args) {
            // Create a button with the label "Jackpot".
            JButton button = new RoundButton("Jackpot");
            button.setBackground(Color.green);
            button.setBounds(0, 0, 100, 100);
    
            JButton button2 = new RoundButton("Jackpot2");
            button2.setBackground(Color.red);
            button2.setBounds(50, 50, 100, 100);
    
            // Create a frame in which to show the button.
            JFrame frame = new JFrame();
            frame.getContentPane().setBackground(Color.yellow);
            frame.getContentPane().setLayout(null);
            frame.getContentPane().add(button);
            frame.getContentPane().add(button2);
    //        frame.getContentPane().setLayout(new FlowLayout());
            frame.setSize(200, 200);
            frame.setVisible(true);
    
    		MouseListener mouseListener = new MouseAdapter() {
    			public void mouseEntered( MouseEvent e )
    			{}
    
    			public void mouseExited( MouseEvent e )
    			{}
    
    			public void mouseClicked( MouseEvent e )
    			{
    				System.out.println( "clicked " );
    			}
    
    			public void mousePressed( MouseEvent e )
    			{
    				System.out.println( "pressed " );
    			}
    
    			public void mouseReleased( MouseEvent e )
    			{
    				System.out.println( "released " );
    			}
    		};
    		button.addMouseListener( mouseListener );
    
        }
    }
    adwart likes this.

Similar Threads

  1. jbutton
    By Patea2000 in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2011, 09:59 AM
  2. Custom JButton with flow layout
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 01-17-2011, 11:44 PM
  3. Custom GUI...Help?!
    By wadhah.alhaddad in forum New To Java
    Replies: 1
    Last Post: 02-07-2010, 04:54 PM
  4. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 11:48 AM
  5. Custom tgs in JSP
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-29-2007, 06:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •