Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
a Jbutton with a rounded border
Barry Brashear
Ranch Hand
Posts: 303
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is there a way to have a JButton with rounded edges?
Thanks.
Jennifer Sohl
Ranch Hand
Posts: 455
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is a class that you can use to get rounded border :
import javax.swing.border.*; import java.awt.*; /** * @author jsohl * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class OvalBorder implements Border { protected int m_w = 6; protected int m_h = 6; protected Color m_topColor = Color.white; protected Color m_bottomColor = Color.gray; public OvalBorder() { m_w = 6; m_h = 6; } public OvalBorder(int w, int h) { m_w = w; m_h = h; } public OvalBorder(int w, int h, Color topColor, Color bottomColor) { m_w = w; m_h = h; m_topColor = topColor; m_bottomColor = bottomColor; } public Insets getBorderInsets(Component c) { return new Insets(m_h,m_w,m_h,m_w); } public boolean isBorderOpaque() { return true; } public void paintBorder(Component c,Graphics g, int x, int y, int w, int h) { w--; h--; g.setColor(m_topColor); g.drawLine(x, y+h-m_h, x, y+m_h); g.drawArc(x,y, 2*m_w,2*m_h,180,-90); g.drawLine(x+m_w,y, x+w-m_w,y); g.drawArc(x+w-2*m_w, y, 2*m_w,2*m_h,90,-90); g.setColor(m_bottomColor); g.drawLine(x+w, y+m_h, x+w, y+h-m_h); g.drawArc(x+w-2*m_w, y+h-2*m_h, 2*m_w, 2*m_h, 0,-90); g.drawLine(x+m_w, y+h, x+w-m_w, y+h); g.drawArc(x, y+h-2*m_h, 2*m_w, 2*m_h, -90,-90); } }
or a class to paint a perfectly round button :
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); } // 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.black); frame.getContentPane().add(button); frame.getContentPane().setLayout(new FlowLayout()); frame.setSize(150, 150); frame.setVisible(true); } }
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
from rectangular shape to ellipse shape with jquery
Custom Buttons
Let's spin a yarn
Question regarding the BigDecimal class
0 or 1
More...