package abstrasy.bedesk.ui;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.JButton;
/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
* http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*
* Note:
* ====
*
* Le code suivant est offert sous sous la licence mentionnée ci-dessus par
* la société BEDESK sprl dans le cadre du support du projet Abstrasy.
*
*/
public class RoundButton extends JButton {
private Color border0 = new Color(96, 96, 96);
private Color border1 = new Color(255, 255, 255);
private Color bg = null;
private Color bg1 = null;
private int roundBorderDiameter = 10;
boolean leftConnected = false;
boolean rightConnected = false;
private int w = -1; // forcer la réinit...
private int w1;
private int h = -1;
private int h1;
public RoundButton() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x1 = 0; //offests...
//int y1 = 0;
int x2 = 0;
//int y2 = 0;
Point pointZero = new Point(0, 0);
if (leftConnected) {
x1 = roundBorderDiameter;
}
if (rightConnected) {
x2 = roundBorderDiameter;
}
bg = this.getBackground();
bg1 = bg.darker();
w = this.getWidth();
;
w1 = w - 1;
h = this.getHeight() - 1;
h1 = h - 1;
// ombre..
g2.setPaint(new Color(border0.getRed(), border0.getGreen(), border0.getBlue(), 64));
g2.fillRoundRect(-x1, 0, w1 + x2 + x1, h + 1, roundBorderDiameter, roundBorderDiameter);
//
g2.setPaint(new GradientPaint(pointZero, bg, new Point(0, h), bg1));
g2.fillRoundRect(-x1, 1, w1 + x2 + x1, h - 2, roundBorderDiameter, roundBorderDiameter);
// aspect plus glossy de BeDesk 2012
Graphics2D ng2 = (Graphics2D) g2.create();
ng2.setClip(0, 0, this.getWidth(), h / 2);
GradientPaint gpx = new GradientPaint(pointZero, new Color(255, 255, 255, 64), new Point(0, h / 2), new Color(255, 255, 255, 32));
ng2.setPaint(gpx);
ng2.fillRoundRect(-x1, 1, w1 + x2 + x1, h - 2, roundBorderDiameter, roundBorderDiameter);
//aspect plus glossy de BeDesk 2012 - bord supérieur
GradientPaint gpxbt =
new GradientPaint(pointZero, new Color(border1.getRed(), border1.getGreen(), border1.getBlue(), 128), new Point(0, h / 2), new Color(border1.getRed(),
border1.getGreen(), border1.getBlue(), 64));
ng2.setPaint(gpxbt);
ng2.drawRoundRect(-x1, 1, w1 + x2 + x1, h - 2, roundBorderDiameter, roundBorderDiameter);
// aspect plus glossy de BeDesk 2012 - bord inférieur
Graphics2D ngbb = (Graphics2D) g2.create();
ngbb.setClip(0, h / 2, this.getWidth(), h);
GradientPaint gpxbb =
new GradientPaint(new Point(0, h / 2), new Color(border1.getRed(), border1.getGreen(), border1.getBlue(), 32), new Point(0, h), new Color(border1.getRed(),
border1.getGreen(), border1.getBlue(), 32));
ngbb.setPaint(gpxbb);
ngbb.drawRoundRect(-x1, 1, w1 + x2 + x1, h - 3, roundBorderDiameter, roundBorderDiameter);
//
//
//Bordure normale
g2.setPaint(border0);
g2.drawRoundRect(-x1, 0, w1 + x2 + x1, h1, roundBorderDiameter, roundBorderDiameter);
//
if (leftConnected) {
g2.setPaint(new Color(border1.getRed(), border1.getGreen(), border1.getBlue(), 96));
g2.drawLine(0, 1, 0, h - 2);
}
if (rightConnected) {
g2.setPaint(new Color(border0.getRed(), border0.getGreen(), border0.getBlue(), 96));
g2.drawLine(w1, 0, w1, h1);
}
Graphics2D g3 = (Graphics2D) g2.create();
if (!this.isEnabled()) {
g3.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));
}
super.paintComponent(g3);
}
private void jbInit() throws Exception {
this.setEnabled(true);
this.setFont(new Font("Dialog", Font.PLAIN, 10));
this.setBorder(BorderFactory.createEmptyBorder(2, 2, 3, 2));
this.setBorderPainted(false);
this.setContentAreaFilled(false);
this.setFocusPainted(false);
}
public boolean isLeftConnected() {
return leftConnected;
}
public void setLeftConnected(boolean leftConnected) {
this.leftConnected = leftConnected;
repaint();
}
public boolean isRightConnected() {
return rightConnected;
}
public void setRightConnected(boolean rightConnected) {
this.rightConnected = rightConnected;
repaint();
}
public int getRoundBorderDiameter() {
return roundBorderDiameter;
}
public void setRoundBorderDiameter(int roundDiameter) {
this.roundBorderDiameter = roundDiameter;
repaint();
}
}