KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > welcome > OvalButton


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.bluej.welcome;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import java.awt.event.*;
26 import org.openide.util.Utilities;
27
28
29 /**
30  * Transparent button with bullet rollover effect.
31  *
32  * @author Petr Kuzel
33  */

34 public class OvalButton extends JButton implements MouseListener {
35
36     private float scale = 1.0f;
37
38     // conatants dictated by graphics
39
private static final int MAX_FONT = 18;
40     private static final int MIN_FONT = 13;
41
42     private boolean mouseOver;
43
44     public OvalButton() {
45         super();
46         addMouseListener(this);
47     }
48
49     
50     public void paint(Graphics g) {
51         Graphics2D g2d = (Graphics2D)g;
52         int xx = getWidth();
53         int yy = getHeight();
54         
55         Image bullet;
56         if (getModel().isArmed() || mouseOver) {
57             bullet = Utilities.loadImage("org/netbeans/bluej/welcome/button-over.png"); // NOI18N
58
} else {
59             bullet = Utilities.loadImage("org/netbeans/bluej/welcome/button-gray.png"); // NOI18N
60
}
61         ImageIcon icon = new ImageIcon(bullet);
62         int icon_h = icon.getIconHeight();
63         int icon_w = icon.getIconWidth();
64         g.drawImage(bullet, 0, (yy-icon_w)/2, this);
65                 
66         int text_x = icon_w + 6;
67         Font f = getFont();
68         FontMetrics fm = g.getFontMetrics(f);
69         g.setFont(f);
70         g.setColor(Color.WHITE);
71         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
72         g.drawString(getText(),text_x, yy/2 + (fm.getAscent()+fm.getDescent())/2 - fm.getDescent());
73     }
74     
75     public void setScale(float scale) {
76         this.scale = scale;
77     }
78     
79     public Font getFont() {
80         Font oldFont = super.getFont();
81         if (oldFont!=null) {
82             int size = Math.max(oldFont.getSize(), MIN_FONT);
83             size *= scale;
84             size = Math.min(size, MAX_FONT);
85             Font newFont = new Font("SansSerif", Font.BOLD, size); // NOI18N
86
return newFont;
87         } else {
88             return null;
89         }
90     }
91
92     public boolean isOpaque() {
93         return false;
94     }
95
96     public void mouseClicked(MouseEvent e) {
97     }
98
99     public void mousePressed(MouseEvent e) {
100     }
101
102     public void mouseReleased(MouseEvent e) {
103     }
104
105     public void mouseEntered(MouseEvent e) {
106         mouseOver = true;
107         repaint(0);
108     }
109
110     public void mouseExited(MouseEvent e) {
111         mouseOver = false;
112         repaint(0);
113     }
114     
115 }
116
Popular Tags