3D effect for button(Question for top 15 experts)

I tried creating a round(soft) cornered button with the following piece of code.I
                   got the expected result, but the problem is that the button is not having 3D
                                                        look.

                                    import java.awt.*;
                                    import java.awt.geom.*;
                                    import javax.swing.*;
                  import java.awt.event.*;
                       
                                    public class RoundButton extends JButton {
                                      public RoundButton(String label) {
                                        super(label);
                                        setContentAreaFilled(false);
                                      }
                                   
                                      protected void paintComponent(Graphics g) {
                                         if (getModel().isArmed()) {
                                             g.setColor(Color.white);
                                         }
                                         else {
                                             g.setColor(getBackground());
                                        }
                                         
                                        super.paintComponent(g);
                                      }

                                      protected void paintBorder(Graphics g) {
                                        g.setColor(getForeground());
                                        g.drawRoundRect(0,0,getSize().width-1,getSize().height,22,22);

                                      }

                                      Shape shape;
                                      public boolean contains(int x, int y) {
                                     
                                        if (shape == null ||
                                          !shape.getBounds().equals(getBounds())) {
                                          shape = new Ellipse2D.Double(0, 0,
                                            getSize().width,getSize().height);
                }
                                        return shape.contains(x, y);
                                      }
                                                         
                                      public static void main(String[] args) {
                                                 
                                        JButton button = new RoundButton("Close");
                                        JFrame frame = new JFrame();
                frame.getContentPane().add(button);
                frame.getContentPane().setLayout(new FlowLayout());
                                        frame.setSize(250, 250);
                                        frame.setVisible(true);
                                      }
                                    }
LVL 2
shaveriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ehartantoCommented:
How about making 2 pictures.  The first one is a normal button, the second one is a pushed button, instead of make geometry calculation ?

Good luck...
0
vladi21Commented:
frame.getContentPane().add(button);
                frame.getContentPane().setLayout(new FlowLayout());
                                       

:)
why not?
                frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);

                 
exactly copy code from
http://developer.java.sun.com/developer/TechTips/1999/tt0826.html#tip1                       
0
shaveriAuthor Commented:
vladi21,
         See whatever comment you've passed it doesn't work, and for the comment of
 ehartanto,
          it is not a feasible solution to create two buttons .
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

vladi21Commented:
You already have tested a code from http://developer.java.sun.com/developer/TechTips/1999/tt0826.html#tip1                       ?
0
shaveriAuthor Commented:
vladi21,
       I already tested a code from the site you've mentioned, in other words I can say the same code which was written for creating round/circular shaped buttons was modified by me to get the soft cornered button.Tell me one thing is it not possible to give 3D effect (like Raised border) to swing components like AWT components.
0
shaveriAuthor Commented:
Adjusted points to 100
0
vladi21Commented:
look at new lines in
protected void paintBorder(Graphics g) {

import java.awt.*;
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);

    //new lines
    g.drawRoundRect(0,0,getSize().width-1,getSize().height-1,22,22);
    g.setColor(getModel().isArmed()?Color.red:Color.white);
    int d=1;
    g.drawRoundRect(d,d,getSize().width-2*d-1,getSize().height-2*d-1,21,21);
  }

// 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);

// Create a frame in which to show the button.
    JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.yellow);
    frame.getContentPane().add(button);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setSize(150, 150);
    frame.setVisible(true);
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.