/**
 * Die Klasse stellt einen blauen, runden <code>JToggleButton</code> bereit.
 * 
 */
package swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JToggleButton;

/**
 * @JB_RoundToggleButton.java 22.04.2008 Copyright 2008-2010 yourwebs.de. All
 *                            rights reserved
 * @author J&ouml;rg Czeschla
 */
/*
 * 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.
 */
@SuppressWarnings("serial")
public class JB_RoundToggleButton extends JToggleButton {

	private Ellipse2D sensibleOval;

	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);
		setContentAreaFilled(false);
		setFont(new Font("Times New Roman", Font.CENTER_BASELINE, getFont()
				.getSize()));
		setBorderPainted(false);
		Dimension size = getPreferredSize();
		size.width = size.height = Math.max(size.width, size.height);
		setPreferredSize(size);
		setSize(size);
		sensibleOval = new Ellipse2D.Float(0, 0, size.width, size.height);
	}

	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;

		float diameter = (float) sensibleOval.getBounds2D().getHeight();
		float radius = diameter / 2;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		Point2D center = new Point2D.Float(diameter / 10f * 5f,
				diameter / 10f * 5f);
		
		
		RadialGradientPaint paint;
		if (getModel().isSelected()) {
			setForeground(new Color(220, 220, 0));
			
			float[] dist = { 0.01f, 0.9f, 1f };
			Color[] colors = { new Color(100, 100, 150),
					new Color(10, 10, 100), new Color(200, 200, 255) };
			paint = new RadialGradientPaint(center, radius, dist, colors,
					CycleMethod.NO_CYCLE);
		} else {
			setForeground(new Color(150, 150, 50));
			
			float[] dist = { 0.01f, 0.80f, 0.9f, 1f };
			Color[] colors = { new Color(10, 10, 100), new Color(15, 15, 150),
					new Color(200, 200, 255), new Color(100, 100, 150) };
			paint = new RadialGradientPaint(center, radius, dist, colors,
					CycleMethod.NO_CYCLE);
		}
		
		g2d.setPaint(paint);
		g2d.fill(sensibleOval);
		super.paintComponent(g);
	}

	public boolean contains(int x, int y) {
		return sensibleOval.contains(x, y);
	}
}

