1 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 34 public class OvalButton extends JButton implements MouseListener { 35 36 private float scale = 1.0f; 37 38 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"); } else { 59 bullet = Utilities.loadImage("org/netbeans/bluej/welcome/button-gray.png"); } 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); 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 |