1 7 8 package org.jdesktop.swing; 9 10 import java.awt.*; 11 import java.awt.geom.Rectangle2D ; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import javax.swing.*; 20 21 import javax.swing.border.BevelBorder ; 22 import javax.swing.border.Border ; 23 24 29 class PaintUtils { 30 31 33 private static Border defaultBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED); 34 35 public static Border getDefaultBorder() { 36 return defaultBorder; 37 } 38 39 43 public static Rectangle getTextBounds(Graphics g, JLabel label) { 44 45 FontMetrics fm = g.getFontMetrics(); 46 Rectangle2D r2d = fm.getStringBounds(label.getText(), g); 47 Rectangle rect = r2d.getBounds(); 48 49 int xOffset = 0; 50 switch (label.getHorizontalAlignment()) { 51 case SwingConstants.RIGHT: 52 case SwingConstants.TRAILING: 53 xOffset = label.getBounds().width - rect.width; 54 break; 55 56 case SwingConstants.CENTER: 57 xOffset = (label.getBounds().width - rect.width)/2; 58 break; 59 60 default: 61 case SwingConstants.LEFT: 62 case SwingConstants.LEADING: 63 xOffset = 0; 64 break; 65 } 66 67 int yOffset = 0; 68 switch (label.getVerticalAlignment()) { 69 case SwingConstants.TOP: 70 yOffset = 0; 71 break; 72 case SwingConstants.CENTER: 73 yOffset = (label.getBounds().height - rect.height)/2; 74 break; 75 case SwingConstants.BOTTOM: 76 yOffset = label.getBounds().height - rect.height; 77 break; 78 } 79 return new Rectangle(xOffset, yOffset, rect.width, rect.height); 80 } 81 82 86 public static void paintGradient(Graphics g, JComponent comp, 87 Color color1, Color color2) { 88 GradientPaint paint = new GradientPaint(0, 0, color1, 89 0, comp.getHeight(), color2, 90 true); 91 Graphics2D g2 = (Graphics2D)g; 92 Paint oldPaint = g2.getPaint(); 93 94 g2.setPaint(paint); 95 g2.fillRect(0, 0, comp.getWidth(), comp.getHeight()); 96 g2.setPaint(oldPaint); 97 } 98 99 102 public static void setBackgroundColor(Container cont, Color color) { 103 cont.setBackground(color); 104 Component[] children = cont.getComponents(); 105 for (int i = 0; i < children.length; i++) { 106 if (children[i] instanceof Container) { 107 setBackgroundColor((Container)children[i], color); 108 } else { 109 children[i].setBackground(color); 110 } 111 } 112 } 113 114 117 public static void setForegroundColor(Container cont, Color color) { 118 cont.setForeground(color); 119 Component[] children = cont.getComponents(); 120 for (int i = 0; i < children.length; i++) { 121 if (children[i] instanceof Container) { 122 setForegroundColor((Container)children[i], color); 123 } else { 124 children[i].setForeground(color); 125 } 126 } 127 } 128 129 public static void setFont(Container cont, Font font) { 130 cont.setFont(font); 131 Component[] children = cont.getComponents(); 132 for (int i = 0; i < children.length; i++) { 133 if (children[i] instanceof Container) { 134 setFont((Container)children[i], font); 135 } else { 136 children[i].setFont(font); 137 } 138 } 139 } 140 } 141 | Popular Tags |