[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 664
  • Last Modified:

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);
                                      }
                                    }
0
shaveri
Asked:
shaveri
  • 3
  • 3
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now