title: How to create a JButton extension with rounded corners?
body: <p>This is a continuation of the question <a href="https://stackoverflow.com/questions/778222/make-a-button-round">"Java rounded Swing JButton"</a>. I have searched for an extension of javax.swing.JButton which will inherit all runtime behavior and just override drawing of the corners.</p>
  
  <p>Using the code given by <a href="http://forums.sun.com/profile.jspa?userID=81148" rel="nofollow noreferrer">noah.w</a> on <a href="http://forums.sun.com/thread.jspa?threadID=284897&amp;tstart=28696" rel="nofollow noreferrer">sun forums page</a> the result looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/sZX30.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sZX30.png" alt="alt text"></a>
  </p>
  
  <p>I'd like to be able to have the same gradient in the background, on mouse over change etc. Does anybody know how to do that?</p>
  
  <p>Code which creates the Java Swing window from the picture is:</p>
  
  <pre><code>public class XrButton extends JButton implements MouseListener {
  
      private static final long serialVersionUID = 9032198251140247116L;
  
      String text;
      boolean mouseIn = false;
  
      public XrButton(String s) {
          super(s);
          text = s;
          setBorderPainted(false);
          addMouseListener(this);
          setContentAreaFilled(false);
      }
  
      @Override
      public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          if (getModel().isPressed()) {
              g.setColor(g.getColor());
              g2.fillRect(3, 3, getWidth() - 6, getHeight() - 6);
          }
          super.paintComponent(g);
  
          if (mouseIn)
              g2.setColor(Color.red);
          else
              g2.setColor(new Color(128, 0, 128));
  
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                  RenderingHints.VALUE_ANTIALIAS_ON);
          g2.setStroke(new BasicStroke(1.2f));
          g2.draw(new RoundRectangle2D.Double(1, 1, (getWidth() - 3),
                  (getHeight() - 3), 12, 8));
          g2.setStroke(new BasicStroke(1.5f));
          g2.drawLine(4, getHeight() - 3, getWidth() - 4, getHeight() - 3);
  
          g2.dispose();
      }
  
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.getContentPane().setLayout(new FlowLayout());
  
          XrButton xrButton = new XrButton("XrButton");
          JButton jButton = new JButton("JButton");
  
          frame.getContentPane().add(xrButton);
  
          frame.getContentPane().add(jButton);
  
          frame.setSize(150, 150);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  
      public void mouseClicked(MouseEvent e) {
      }
  
      public void mouseEntered(MouseEvent e) {
          mouseIn = true;
      }
  
      public void mouseExited(MouseEvent e) {
          mouseIn = false;
      }
  
      public void mousePressed(MouseEvent e) {
      }
  
      public void mouseReleased(MouseEvent e) {
      }
  </code></pre>