1 33 34 package edu.rice.cs.util.swing; 35 36 import javax.swing.*; 37 import java.awt.*; 38 39 42 public class CenteredIcon implements Icon { 43 private Icon _base; 44 private int _w=0; 45 private int _h=0; 46 public CenteredIcon(Icon base, int width, int height) { 47 _base = base; 48 _w = width; 49 _h = height; 50 } 51 public int getIconHeight(){ 52 return _h; 53 } 54 public int getIconWidth(){ 55 return _w; 56 } 57 public void paintIcon(Component c, Graphics g, int x, int y){ 58 Shape oldClip = g.getClip(); 59 g.setClip(x, y, _w, _h); 60 int centerX = x + (_w/2); 61 int centerY = y + (_h/2); 62 int subRadX = _base.getIconWidth() / 2; 63 int subRadY = _base.getIconHeight() / 2; 64 _base.paintIcon(c, g, centerX - subRadX, centerY - subRadY); 65 g.setClip(oldClip); 66 } 67 public Icon getBaseIcon() { return _base; } 68 } | Popular Tags |