Link to home
Start Free TrialLog in
Avatar of shaveri
shaveri

asked on

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);
                                      }
                                    }
Avatar of ehartanto
ehartanto

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...
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                       
Avatar of shaveri

ASKER

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 .
You already have tested a code from http://developer.java.sun.com/developer/TechTips/1999/tt0826.html#tip1                       ?
Avatar of shaveri

ASKER

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.
Avatar of shaveri

ASKER

Adjusted points to 100
ASKER CERTIFIED SOLUTION
Avatar of vladi21
vladi21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial