VectorButtonpublic class VectorButton extends JButton implements MouseListener
Fields Summary |
---|
protected boolean | pressed |
Constructors Summary |
---|
public VectorButton()
this.addMouseListener(this);
|
Methods Summary |
---|
protected static java.awt.Color | alphaColor(java.awt.Color color, int alpha)
return new Color(color.getRed(), color.getGreen(),
color.getBlue(), alpha);
| protected void | drawBorder(int w, int h, float scale, java.awt.Graphics2D g2)
// draw the border
g2.setColor(new Color(0,0,0,150));
this.drawRoundRect(g2,
scale*(0f),
scale*(0f),
w,h,scale,scale);
| protected void | drawButtonBody(int w, int h, float scale, java.awt.Color base, java.awt.Graphics2D g2)
// draw the button body
Color grad_top = base.brighter();
Color grad_bot = base.darker();
GradientPaint bg = new GradientPaint(
new Point(0,0), grad_top,
new Point(0,h), grad_bot);
g2.setPaint(bg);
this.fillRoundRect(g2,
(0)*scale,
(0)*scale,
w,h,1*scale,1*scale);
// draw the inner color
Color inner = base.brighter();
inner = alphaColor(inner,75);
g2.setColor(inner);
this.fillRoundRect(g2,
scale*(.4f),
scale*(.4f),
w-scale*.8f, h-scale*.5f,
scale*.6f,scale*.4f);
| protected void | drawDropShadow(int w, int h, float scale, java.awt.Graphics2D g2)
// draw the outer drop shadow
g2.setColor(new Color(0,0,0,50));
this.fillRoundRect(g2,
(-.04f)*scale,
(.02f)*scale,
w+.08f*scale, h+0.08f*scale,
scale*1.04f, scale*1.04f);
g2.setColor(new Color(0,0,0,100));
this.fillRoundRect(g2,0,0.06f*scale,w,h,scale,scale);
| protected void | drawHighlight(int w, int h, float scale, java.awt.Color base, java.awt.Graphics2D g2)
// create the highlight
GradientPaint highlight = new GradientPaint(
new Point2D.Float(scale*0.2f,scale*0.2f),
new Color(255,255,255,175),
new Point2D.Float(scale*0.2f,scale*0.55f),
new Color(255,255,255,0)
);
g2.setPaint(highlight);
this.fillRoundRect(g2, scale*0.2f, scale*0.1f,
w-scale*0.4f, scale*0.4f, scale*0.8f, scale*0.4f);
this.drawRoundRect(g2, scale*0.2f, scale*0.1f,
w-scale*0.4f, scale*0.4f, scale*0.8f, scale*0.4f);
| protected void | drawLiquidButton(java.awt.Color base, int width, int height, java.lang.String text, float scale, java.awt.Graphics2D g2)
// calculate inset
int inset = (int)(scale*0.04f);
int w = width - inset*2 - 1;
int h = height - (int)(scale*0.1f) - 1;
g2.translate(inset,0);
drawDropShadow(w,h,scale,g2);
if(pressed) {
g2.translate(0, 0.04f*scale);
}
drawButtonBody(w,h,scale,base,g2);
drawText(w,h,scale,text,g2);
drawHighlight(w,h,scale,base,g2);
drawBorder(w,h,scale,g2);
if(pressed) {
g2.translate(0, 0.04f*scale);
}
g2.translate(-inset,0);
| protected static void | drawRoundRect(java.awt.Graphics2D g2, float x, float y, float w, float h, float ax, float ay)
g2.drawRoundRect(
(int)x, (int)y,
(int)w, (int)h,
(int)ax, (int)ay
);
| protected void | drawText(int w, int h, float scale, java.lang.String text, java.awt.Graphics2D g2)
// calculate the width and height
int fw = g2.getFontMetrics().stringWidth(text);
int fh = g2.getFontMetrics().getAscent() -
g2.getFontMetrics().getDescent();
int textx = (w-fw)/2;
int texty = h/2 + fh/2;
// draw the text
g2.setColor(new Color(0,0,0,70));
g2.drawString(text,(int)((float)textx+scale*(0.04f)),
(int)((float)texty + scale*(0.04f)));
g2.setColor(Color.black);
g2.drawString(text, textx, texty);
| protected static void | fillRoundRect(java.awt.Graphics2D g2, float x, float y, float w, float h, float ax, float ay)
g2.fillRoundRect(
(int)x, (int)y,
(int)w, (int)h,
(int)ax, (int)ay
);
| public java.awt.Dimension | getPreferredSize()
String text = getText();
FontMetrics fm = this.getFontMetrics(getFont());
float scale = (50f/30f)*this.getFont().getSize2D();
int w = fm.stringWidth(text);
w += (int)(scale*1.4f);
int h = fm.getHeight();
h += (int)(scale*.3f);
return new Dimension(w,h);
| public void | mouseClicked(java.awt.event.MouseEvent evt)
| public void | mouseEntered(java.awt.event.MouseEvent evt)
| public void | mouseExited(java.awt.event.MouseEvent evt)
| public void | mousePressed(java.awt.event.MouseEvent evt)
pressed = true;
| public void | mouseReleased(java.awt.event.MouseEvent evt)
pressed = false;
| public static void | p(java.lang.String s)
System.out.println(s);
| public void | paintComponent(java.awt.Graphics g)
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(this.getBackground());
g2.fillRect(0,0,this.getWidth(),this.getHeight());
float scale = (50f/30f)*this.getFont().getSize2D();
drawLiquidButton(this.getForeground(),
this.getWidth(), this.getHeight(),
getText(), scale,
g2);
|
|