/**
* Die Klasse stellt einen runden ToggleButton bereit. Für beide Zustände
* können mit den Methoden setPressedColor(Color c)
und
* setReleasedColor(Color c)
verschiedene Farben definiert werden.
*
*/
package swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicToggleButtonUI;
import javax.swing.text.View;
/**
* @JB_RoundToggleButton.java 22.04.2008 Copyright 2008-2012 javabeginners.de.
* All rights reserved
* @author Jörg Czeschla
*
* Displays a circle shaped gray ToggleButton
*/
/*
* This file is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* TexTEn; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307, USA.
*/
public class JB_RoundToggleButton extends JToggleButton {
private Color pressedColor = Color.GRAY;
private Color releasedColor = pressedColor;
private Font font;
public JB_RoundToggleButton() {
this("", null, false);
}
public JB_RoundToggleButton(Icon icon) {
this("", icon, false);
}
public JB_RoundToggleButton(String text) {
this(text, null, false);
}
public JB_RoundToggleButton(Action a) {
super(a);
}
public JB_RoundToggleButton(Icon icon, boolean selected) {
this("", icon, selected);
}
public JB_RoundToggleButton(String text, boolean selected) {
this(text, null, selected);
}
public JB_RoundToggleButton(String text, Icon icon) {
this(text, icon, false);
}
public JB_RoundToggleButton(String text, Icon icon, boolean selected) {
super(text, icon, selected);
setUI(new RoundToggleButtonUI());
Dimension size = getPreferredSize();
font = new Font("Sans-Serif", Font.PLAIN, 14);
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);
}
public void paint(Graphics g) {
super.paintComponent(g);
}
public void setPressedColor(Color c) {
pressedColor = c;
}
public void setReleasedColor(Color c) {
releasedColor = c;
}
public void setFont(Font f) {
font = f;
}
public Color getPressedColor() {
return pressedColor;
}
public Color getReleasedColor() {
return releasedColor;
}
public Font getFont() {
return font;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final JB_RoundToggleButton rtb = new JB_RoundToggleButton("Willi",
false);
rtb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
frame.add(rtb);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RoundToggleButtonUI extends BasicToggleButtonUI {
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
JB_RoundToggleButton b = (JB_RoundToggleButton) c;
Color pressedColor = b.getPressedColor();
Color releasedColor = b.getReleasedColor();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp;
int w = c.getWidth(), h = c.getHeight();
if (b.getModel().isSelected()) {
gp = new GradientPaint(0, 0, Color.WHITE, c.getWidth(),
c.getHeight(), pressedColor.darker());
g2d.setPaint(gp);
g2d.fillOval(0, 0, w, h);
g2d.setPaint(pressedColor.brighter());
g2d.fillOval(2, 2, w - 4, h - 4);
gp = new GradientPaint(3, 3, pressedColor.darker(), w, h,
Color.WHITE);
g2d.setPaint(gp);
g2d.fillOval(3, 3, w - 6, w - 6);
gp = new GradientPaint(4, 4, pressedColor, w - 8, h - 8,
pressedColor.brighter());
g2d.setPaint(gp);
g2d.fillOval(4, 4, w - 8, h - 8);
} else {
gp = new GradientPaint(0, 0, Color.WHITE, w, h,
releasedColor.darker());
g2d.setPaint(gp);
g2d.fillOval(0, 0, w, h);
gp = new GradientPaint(2, 2, releasedColor.brighter(), w - 4,
h - 4, releasedColor);
g2d.setPaint(gp);
g2d.fillOval(2, 2, w - 4, h - 4);
}
Font f = b.getFont();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
Rectangle iconRect = new Rectangle();
Rectangle textRect = new Rectangle();
Rectangle viewRect = new Rectangle(c.getSize());
String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(),
b.getIcon(), b.getVerticalAlignment(),
b.getHorizontalAlignment(), b.getVerticalTextPosition(),
b.getHorizontalTextPosition(), viewRect, iconRect, textRect,
b.getText() == null ? 0 : b.getIconTextGap());
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
paintText(g, (AbstractButton) c, textRect, text);
}
}
}
}