1 7 package javax.swing.plaf.basic; 8 9 import javax.swing.*; 10 import java.awt.Component ; 11 import java.awt.Color ; 12 import java.awt.Dimension ; 13 import java.awt.Font ; 14 import java.awt.FontMetrics ; 15 import java.awt.Graphics ; 16 import java.awt.Insets ; 17 import java.awt.Rectangle ; 18 import java.awt.event.KeyEvent ; 19 import com.sun.java.swing.SwingUtilities2; 20 21 22 26 27 public class BasicGraphicsUtils 28 { 29 30 private static final Insets GROOVE_INSETS = new Insets (2, 2, 2, 2); 31 private static final Insets ETCHED_INSETS = new Insets (2, 2, 2, 2); 32 33 public static void drawEtchedRect(Graphics g, int x, int y, int w, int h, 34 Color shadow, Color darkShadow, 35 Color highlight, Color lightHighlight) 36 { 37 Color oldColor = g.getColor(); g.translate(x, y); 39 40 g.setColor(shadow); 41 g.drawLine(0, 0, w-1, 0); g.drawLine(0, 1, 0, h-2); 44 g.setColor(darkShadow); 45 g.drawLine(1, 1, w-3, 1); g.drawLine(1, 2, 1, h-3); 48 g.setColor(lightHighlight); 49 g.drawLine(w-1, 0, w-1, h-1); g.drawLine(0, h-1, w-1, h-1); 52 g.setColor(highlight); 53 g.drawLine(w-2, 1, w-2, h-3); g.drawLine(1, h-2, w-2, h-2); 56 g.translate(-x, -y); 57 g.setColor(oldColor); 58 } 59 60 61 67 public static Insets getEtchedInsets() { 68 return ETCHED_INSETS; 69 } 70 71 72 public static void drawGroove(Graphics g, int x, int y, int w, int h, 73 Color shadow, Color highlight) 74 { 75 Color oldColor = g.getColor(); g.translate(x, y); 77 78 g.setColor(shadow); 79 g.drawRect(0, 0, w-2, h-2); 80 81 g.setColor(highlight); 82 g.drawLine(1, h-3, 1, 1); 83 g.drawLine(1, 1, w-3, 1); 84 85 g.drawLine(0, h-1, w-1, h-1); 86 g.drawLine(w-1, h-1, w-1, 0); 87 88 g.translate(-x, -y); 89 g.setColor(oldColor); 90 } 91 92 98 public static Insets getGrooveInsets() { 99 return GROOVE_INSETS; 100 } 101 102 103 public static void drawBezel(Graphics g, int x, int y, int w, int h, 104 boolean isPressed, boolean isDefault, 105 Color shadow, Color darkShadow, 106 Color highlight, Color lightHighlight) 107 { 108 Color oldColor = g.getColor(); g.translate(x, y); 110 111 if (isPressed && isDefault) { 112 g.setColor(darkShadow); 113 g.drawRect(0, 0, w - 1, h - 1); 114 g.setColor(shadow); 115 g.drawRect(1, 1, w - 3, h - 3); 116 } else if (isPressed) { 117 drawLoweredBezel(g, x, y, w, h, 118 shadow, darkShadow, highlight, lightHighlight); 119 } else if (isDefault) { 120 g.setColor(darkShadow); 121 g.drawRect(0, 0, w-1, h-1); 122 123 g.setColor(lightHighlight); 124 g.drawLine(1, 1, 1, h-3); 125 g.drawLine(2, 1, w-3, 1); 126 127 g.setColor(highlight); 128 g.drawLine(2, 2, 2, h-4); 129 g.drawLine(3, 2, w-4, 2); 130 131 g.setColor(shadow); 132 g.drawLine(2, h-3, w-3, h-3); 133 g.drawLine(w-3, 2, w-3, h-4); 134 135 g.setColor(darkShadow); 136 g.drawLine(1, h-2, w-2, h-2); 137 g.drawLine(w-2, h-2, w-2, 1); 138 } else { 139 g.setColor(lightHighlight); 140 g.drawLine(0, 0, 0, h-1); 141 g.drawLine(1, 0, w-2, 0); 142 143 g.setColor(highlight); 144 g.drawLine(1, 1, 1, h-3); 145 g.drawLine(2, 1, w-3, 1); 146 147 g.setColor(shadow); 148 g.drawLine(1, h-2, w-2, h-2); 149 g.drawLine(w-2, 1, w-2, h-3); 150 151 g.setColor(darkShadow); 152 g.drawLine(0, h-1, w-1, h-1); 153 g.drawLine(w-1, h-1, w-1, 0); 154 } 155 g.translate(-x, -y); 156 g.setColor(oldColor); 157 } 158 159 public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h, 160 Color shadow, Color darkShadow, 161 Color highlight, Color lightHighlight) { 162 g.setColor(darkShadow); 163 g.drawLine(0, 0, 0, h-1); 164 g.drawLine(1, 0, w-2, 0); 165 166 g.setColor(shadow); 167 g.drawLine(1, 1, 1, h-2); 168 g.drawLine(1, 1, w-3, 1); 169 170 g.setColor(lightHighlight); 171 g.drawLine(0, h-1, w-1, h-1); 172 g.drawLine(w-1, h-1, w-1, 0); 173 174 g.setColor(highlight); 175 g.drawLine(1, h-2, w-2, h-2); 176 g.drawLine(w-2, h-2, w-2, 1); 177 } 178 179 180 186 public static void drawString(Graphics g,String text,int underlinedChar,int x,int y) { 187 int index=-1; 188 189 if (underlinedChar != '\0') { 190 char uc = Character.toUpperCase((char)underlinedChar); 191 char lc = Character.toLowerCase((char)underlinedChar); 192 int uci = text.indexOf(uc); 193 int lci = text.indexOf(lc); 194 195 if(uci == -1) { 196 index = lci; 197 } 198 else if(lci == -1) { 199 index = uci; 200 } 201 else { 202 index = (lci < uci) ? lci : uci; 203 } 204 } 205 drawStringUnderlineCharAt(g, text, index, x, y); 206 } 207 208 224 public static void drawStringUnderlineCharAt(Graphics g, String text, 225 int underlinedIndex, int x,int y) { 226 SwingUtilities2.drawStringUnderlineCharAt(null, g, text, 227 underlinedIndex, x, y); 228 } 229 230 public static void drawDashedRect(Graphics g,int x,int y,int width,int height) { 231 int vx,vy; 232 233 for (vx = x; vx < (x + width); vx+=2) { 235 g.fillRect(vx, y, 1, 1); 236 g.fillRect(vx, y + height-1, 1, 1); 237 } 238 239 for (vy = y; vy < (y + height); vy+=2) { 241 g.fillRect(x, vy, 1, 1); 242 g.fillRect(x+width-1, vy, 1, 1); 243 } 244 } 245 246 public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap) 247 { 248 if(b.getComponentCount() > 0) { 249 return null; 250 } 251 252 Icon icon = (Icon) b.getIcon(); 253 String text = b.getText(); 254 255 Font font = b.getFont(); 256 FontMetrics fm = b.getFontMetrics(font); 257 258 Rectangle iconR = new Rectangle (); 259 Rectangle textR = new Rectangle (); 260 Rectangle viewR = new Rectangle (Short.MAX_VALUE, Short.MAX_VALUE); 261 262 SwingUtilities.layoutCompoundLabel( 263 (JComponent) b, fm, text, icon, 264 b.getVerticalAlignment(), b.getHorizontalAlignment(), 265 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 266 viewR, iconR, textR, (text == null ? 0 : textIconGap) 267 ); 268 269 272 273 Rectangle r = iconR.union(textR); 274 275 Insets insets = b.getInsets(); 276 r.width += insets.left + insets.right; 277 r.height += insets.top + insets.bottom; 278 279 return r.getSize(); 280 } 281 282 286 static boolean isLeftToRight( Component c ) { 287 return c.getComponentOrientation().isLeftToRight(); 288 } 289 } 290 | Popular Tags |