View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.common.ui.button;
5   
6   import java.awt.Color;
7   import java.awt.GradientPaint;
8   import java.awt.Graphics;
9   import java.awt.Graphics2D;
10   
11  import javax.swing.JButton;
12  import javax.swing.JFrame;
13   
14  public class GradientButton extends JButton {
15  	private Color color1;
16  	private Color color2;
17  	
18  	public GradientButton() {
19  		this(Color.WHITE, Color.LIGHT_GRAY);
20  	}
21  	
22  	public GradientButton(Color c1, Color c2) {
23  		this.color1 = c1;
24  		this.color2 = c2;
25  	}
26  	
27  	public void setColor1(Color c1) {
28  		this.color1 = c1;
29  		repaint();
30  	}
31  	
32  	public void setColor2(Color c2) {
33  		this.color2 = c2;
34  		repaint();
35  	}
36  	
37  //	 Overloaded in order to paint the background
38  	protected void paintComponent(Graphics g) {
39  		final Graphics2D g2 = (Graphics2D) g;
40  		int w = getWidth();
41  		int h = getHeight();
42  		GradientPaint gradient = new GradientPaint(20, 0, color1, 20, h, color2,
43  				false);
44  		g2.setPaint(gradient);
45  		g2.fillRect(0, 0, w, h);
46  		super.paintComponent(g);
47  	}
48  	
49  	public static void main(String args[]) {
50  		JFrame frame = new JFrame();
51  		frame.setSize(600,400);
52  		
53  		GradientButton button = new GradientButton(Color.blue,Color.blue);
54  		button.setForeground(Color.BLACK);
55  		button.setText("Test");	
56  		button.setOpaque(false);
57  		
58  		frame.getContentPane().add(button);
59  		frame.pack();
60  		frame.setVisible(true);
61  	}
62  }