1 19 20 package taskblocks.app; 21 22 import java.awt.AlphaComposite ; 23 import java.awt.Color ; 24 import java.awt.Font ; 25 import java.awt.Graphics2D ; 26 import java.awt.Insets ; 27 import java.awt.image.BufferedImage ; 28 29 import javax.swing.Action ; 30 import javax.swing.Icon ; 31 import javax.swing.ImageIcon ; 32 import javax.swing.JButton ; 33 import javax.swing.JToolBar ; 34 35 public class MyToolBar extends JToolBar { 36 37 public JButton add(Action a) { 38 JButton b = createActionComponent(a); 39 b.putClientProperty("hideActionText", Boolean.FALSE); 40 b.setAction(a); 41 42 b.putClientProperty("JButton.buttonType", "icon"); 44 45 b.setMargin(new Insets (10,10,10,10)); 46 b.setBorderPainted(false); 47 b.setFocusable(false); 48 49 String longD = (String ) a.getValue(Action.LONG_DESCRIPTION); 50 if (longD != null) { 51 b.setToolTipText(longD); 52 } 53 Icon icon = b.getIcon(); 54 if(icon != null) { 55 BufferedImage bufIm = new BufferedImage (icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 56 Graphics2D g2 = (Graphics2D )bufIm.getGraphics(); 57 icon.paintIcon(this, g2, 0, 0); 58 g2.setColor(Color.BLACK); 59 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.4f)); 60 g2.fillRect(0,0,icon.getIconWidth(), icon.getIconHeight()); 61 ImageIcon pressedIcon = new ImageIcon (bufIm); 62 b.setPressedIcon(pressedIcon); 63 64 bufIm = new BufferedImage (icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 65 g2 = (Graphics2D )bufIm.getGraphics(); 66 icon.paintIcon(this, g2, 0, 0); 67 g2.setColor(Color.WHITE); 68 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.4f)); 69 g2.fillRect(0,0,icon.getIconWidth(), icon.getIconHeight()); 70 ImageIcon hoverIcon = new ImageIcon (bufIm); 71 b.setRolloverIcon(hoverIcon); 72 } 73 74 b.setOpaque(false); 75 b.setRolloverEnabled(false); 76 b.setFont(b.getFont().deriveFont(Font.PLAIN)); 77 b.setContentAreaFilled(false); 78 79 add(b); 80 return b; 81 } 82 83 } 84 | Popular Tags |