1 30 31 package com.jgoodies.looks.common; 32 33 import java.awt.Component ; 34 import java.awt.Dimension ; 35 import java.awt.Graphics ; 36 37 import javax.swing.Icon ; 38 39 import com.jgoodies.looks.Options; 40 41 48 49 public class MinimumSizedIcon implements Icon { 50 51 private final Icon icon; 52 private final int width; 53 private final int height; 54 private final int xOffset; 55 private final int yOffset; 56 57 58 public MinimumSizedIcon() { 59 this(null); 60 } 61 62 public MinimumSizedIcon(Icon icon) { 63 Dimension minimumSize = Options.getDefaultIconSize(); 64 this.icon = icon; 65 int iconWidth = icon == null ? 0 : icon.getIconWidth(); 66 int iconHeight = icon == null ? 0 : icon.getIconHeight(); 67 width = Math.max(iconWidth, Math.max(20, minimumSize.width)); 68 height = Math.max(iconHeight, Math.max(20, minimumSize.height)); 69 xOffset = Math.max(0, (width - iconWidth) / 2); 70 yOffset = Math.max(0, (height - iconHeight) / 2); 71 } 72 73 74 public int getIconHeight() { return height; } 75 public int getIconWidth() { return width; } 76 77 78 public void paintIcon(Component c, Graphics g, int x, int y) { 79 if (icon != null) 80 icon.paintIcon(c, g, x + xOffset, y + yOffset); 81 } 82 83 84 } | Popular Tags |