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,getSiz e().width- 1,getSize( ).height,2 2,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().set Layout(new FlowLayout());
frame.setSize(250, 250);
frame.setVisible(true);
}
}
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,getSiz
}
Shape shape;
public boolean contains(int x, int y) {
if (shape == null ||
!shape.getBounds().equals(
shape = new Ellipse2D.Double(0, 0,
getSize().width,getSize().
}
return shape.contains(x, y);
}
public static void main(String[] args) {
JButton button = new RoundButton("Close");
JFrame frame = new JFrame();
frame.getContentPane().add
frame.getContentPane().set
frame.setSize(250, 250);
frame.setVisible(true);
}
}
frame.getContentPane().add (button);
frame.getContentPane().set Layout(new FlowLayout());
:)
why not?
frame.getContentPane().set Layout(new FlowLayout());
frame.getContentPane().add (button);
exactly copy code from
http://developer.java.sun.com/developer/TechTips/1999/tt0826.html#tip1
frame.getContentPane().set
:)
why not?
frame.getContentPane().set
frame.getContentPane().add
exactly copy code from
http://developer.java.sun.com/developer/TechTips/1999/tt0826.html#tip1
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 .
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 ?
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.
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.
ASKER
Adjusted points to 100
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good luck...