Creating Round Swing Buttons
.,.
There are two things you need to do when creating a round button. The first is to override the appropriate painting methods in order to paint the round shape. The second is to set things up so that the button responds only when you click within the round Button (Not Just Withnin The Rectangle That Contains the Round Button).
Here's An Example Program That Implements A Round Button:
Import java.awt. *;
Import java.awt.geom. *;
Import javax.swing. *;
Public Class RoundButton Extends JButton {
Public roundbutton (String label) {
Super (label);
// Thase Statements Enlarge the Button SO That IT
// becomes a circle rather tran.
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 pieint 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 labr 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 elipse2d.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);
}
}
The RoundButton class extends JButton because we want to keep most of functionality of a JButton. In the RoundButton constructor, the method setContentAreaFilled () is called. This causes the button to paint the focus rectangle, but not to paint the background. Now we need to paint the circular background. That's done by overriding the paintComponent () method. That method uses Graphics.fillOval () to paint a solid circle. Then paintComponent () calls super.paintComponent () to paint the label on top of the solid circle . This example also overrides paintBorder () in order to paint a border around the round button. If you do not want a border, you would not override this method. This method calls Graphics.drawOval () to paint a thin border around the circle. This example also overrides paintBorder () in order to paint a border around the round button. If you do not want a border, you would not override this method. This method calls Graphics.drawOval () to paint a thin border around THE CIRCLE. NO te: In JDKTM 1.2.2, there's a small bug in how JButton behaves when you drag the mouse in and out of its perimeter Ideally, if you clickon the circular button and then drag the mouse outside of the button's perimeter, the button should. change its appearance. When you drag the mouse back into the circular button's perimeter, the button should restore its appearance. Unfortunately, the code that implements this behavior does not call the contains () method. Instead it just uses the 'bounds'