Gangstersheep (07.02.2012), Mardcore (11.02.2012), Mentos (07.02.2012)
Thema: KButton
-
07.02.2012, 21:57 #1
- Registriert seit
- 19.12.2011
- Beiträge
- 356
Thanked 164 Times in 101 PostsKButton
PHP-Code:package KCClient.UI.Components;
import java.awt.*;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
/**
*
* @author DeKaDeNz
*/
public class KButton extends JButton {
public class KButton_MouseEvents extends MouseAdapter
{
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
KButton button = (KButton)evt.getComponent();
button.hover = true;
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
KButton button = (KButton)evt.getComponent();
button.hover = false;
}
@Override
public void mousePressed(java.awt.event.MouseEvent evt)
{
KButton button = (KButton)evt.getComponent();
button.clicked = true;
}
@Override
public void mouseReleased(java.awt.event.MouseEvent evt)
{
KButton button = (KButton)evt.getComponent();
button.clicked = false;
}
}
private GradientPaint paint2;
public KButton()
{
super();
this.addMouseListener(new KButton_MouseEvents());
}
private Color background2 = Color.WHITE;
private Color borderColor = Color.BLACK;
public Color getBorderColor()
{
return borderColor;
}
public void setBorderColor(Color c)
{
borderColor = c;
}
public Color getBackground2()
{
return background2;
}
public void setBackground2(Color c)
{
background2 = c;
}
public boolean hover = false;
public boolean clicked = false;
public Paint paintBackground(Graphics g)
{
//background
Graphics2D g2 = (Graphics2D)g;
Paint old = g2.getPaint();
//background
g2.setColor(this.getBackground());
g2.fillRoundRect(0,0,this.getWidth()-1, this.getHeight()-1,6,6);
//bckground//shine
Color transp1 = new Color(this.getBackground2().getRed(),this.getBackground2().getBlue(), this.getBackground2().getGreen(), 180);
Color transp2 = new Color(this.getBackground2().getRed(),this.getBackground2().getBlue(), this.getBackground2().getGreen(), 50);
paint2 = new GradientPaint(0,0,transp1, 0,this.getHeight()/2,transp2,true);
g2.setPaint(paint2);
if(!clicked)
g2.fillRoundRect(0,0,this.getWidth()-1, (this.getHeight())/2,6,6);
else
g2.fillRoundRect(0,(this.getHeight())/2,this.getWidth()-1,(this.getHeight()-2)/2,6,0);
//hovereffectz
g2.setPaint(old);
if(hover)
{
//background
Color transp3 = new Color(this.getBackground2().getRed(),this.getBackground2().getBlue(), this.getBackground2().getGreen(), 30);
g2.setColor(transp3);
g2.fillRoundRect(0,0,this.getWidth()-1, this.getHeight()-1,6,6);
}
return old;
}
@Override
public void paintBorder(Graphics gtmp)
{
Graphics2D g = (Graphics2D)gtmp;
g.setColor(this.getBorderColor());
g.drawRoundRect(0,0,this.getWidth()-1, this.getHeight()-1,6,6);
}
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Paint oldPaint = paintBackground(g2);
//schrift
g2.setPaint(oldPaint);
String text = this.getText();
g2.setFont(this.getFont());
g2.setColor(this.getForeground());
FontMetrics metrics = g2.getFontMetrics();
int w=metrics.stringWidth(text);
int h=metrics.getHeight();
Dimension d=getSize();
int x=(d.width-w)/2;
int y=(d.height-h)/2+metrics.getAscent();
g2.drawString(text, x, y);
}
}
Geändert von DeKaDeNz (08.02.2012 um 00:35 Uhr)
-
The Following 3 Users Say Thank You to DeKaDeNz For This Useful Post:
Ähnliche Themen
-
[JAVA] KButton
Von Mentos im Forum SourcecodeAntworten: 0Letzter Beitrag: 04.04.2012, 17:49 -
[SCR] KButton - Knuddels Button Control
Von Devon im Forum SourcecodeAntworten: 4Letzter Beitrag: 02.03.2012, 00:43
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.