1 7 8 package javax.swing.colorchooser; 9 10 import javax.swing.*; 11 import javax.swing.border.*; 12 import javax.swing.event.*; 13 import javax.swing.text.*; 14 import java.awt.*; 15 import java.awt.image.*; 16 import java.awt.event.*; 17 import java.beans.PropertyChangeEvent ; 18 import java.beans.PropertyChangeListener ; 19 import java.io.Serializable ; 20 import com.sun.java.swing.SwingUtilities2; 21 22 23 39 class DefaultPreviewPanel extends JPanel { 40 41 private int squareSize = 25; 42 private int squareGap = 5; 43 private int innerGap = 5; 44 45 46 private int textGap = 5; 47 private Font font = new Font("Dialog", Font.PLAIN, 12); 48 private String sampleText = UIManager.getString("ColorChooser.sampleText"); 49 50 private int swatchWidth = 50; 51 52 private Color oldColor = null; 53 54 private JColorChooser getColorChooser() { 55 return (JColorChooser)SwingUtilities.getAncestorOfClass( 56 JColorChooser.class, this); 57 } 58 59 public Dimension getPreferredSize() { 60 JComponent host = getColorChooser(); 61 if (host == null) { 62 host = this; 63 } 64 FontMetrics fm = host.getFontMetrics(getFont()); 65 66 int ascent = fm.getAscent(); 67 int height = fm.getHeight(); 68 int width = SwingUtilities2.stringWidth(host, fm, sampleText); 69 70 int y = height*3 + textGap*3; 71 int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3; 72 return new Dimension( x,y ); 73 } 74 75 public void paintComponent(Graphics g) { 76 if (oldColor == null) 77 oldColor = getForeground(); 78 79 g.setColor(getBackground()); 80 g.fillRect(0,0,getWidth(),getHeight()); 81 82 if (this.getComponentOrientation().isLeftToRight()) { 83 int squareWidth = paintSquares(g, 0); 84 int textWidth = paintText(g, squareWidth); 85 paintSwatch(g, squareWidth + textWidth); 86 } else { 87 int swatchWidth = paintSwatch(g, 0); 88 int textWidth = paintText(g, swatchWidth); 89 paintSquares(g , swatchWidth + textWidth); 90 91 } 92 } 93 94 private int paintSwatch(Graphics g, int offsetX) { 95 int swatchX = offsetX; 96 g.setColor(oldColor); 97 g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2)); 98 g.setColor(getForeground()); 99 g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) ); 100 return (swatchX+swatchWidth); 101 } 102 103 private int paintText(Graphics g, int offsetX) { 104 g.setFont(getFont()); 105 JComponent host = getColorChooser(); 106 if (host == null) { 107 host = this; 108 } 109 FontMetrics fm = SwingUtilities2.getFontMetrics(host, g); 110 111 int ascent = fm.getAscent(); 112 int height = fm.getHeight(); 113 int width = SwingUtilities2.stringWidth(host, fm, sampleText); 114 115 int textXOffset = offsetX + textGap; 116 117 Color color = getForeground(); 118 119 g.setColor(color); 120 121 SwingUtilities2.drawString(host, g, sampleText,textXOffset+(textGap/2), 122 ascent+2); 123 124 g.fillRect(textXOffset, 125 ( height) + textGap, 126 width + (textGap), 127 height +2); 128 129 g.setColor(Color.black); 130 SwingUtilities2.drawString(host, g, sampleText, 131 textXOffset+(textGap/2), 132 height+ascent+textGap+2); 133 134 135 g.setColor(Color.white); 136 137 g.fillRect(textXOffset, 138 ( height + textGap) * 2, 139 width + (textGap), 140 height +2); 141 142 g.setColor(color); 143 SwingUtilities2.drawString(host, g, sampleText, 144 textXOffset+(textGap/2), 145 ((height+textGap) * 2)+ascent+2); 146 147 return width + textGap*3; 148 149 } 150 151 private int paintSquares(Graphics g, int offsetX) { 152 153 int squareXOffset = offsetX; 154 Color color = getForeground(); 155 156 g.setColor(Color.white); 157 g.fillRect(squareXOffset,0,squareSize,squareSize); 158 g.setColor(color); 159 g.fillRect(squareXOffset+innerGap, 160 innerGap, 161 squareSize - (innerGap*2), 162 squareSize - (innerGap*2)); 163 g.setColor(Color.white); 164 g.fillRect(squareXOffset+innerGap*2, 165 innerGap*2, 166 squareSize - (innerGap*4), 167 squareSize - (innerGap*4)); 168 169 g.setColor(color); 170 g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize); 171 172 g.translate(squareSize+squareGap, 0); 173 g.setColor(Color.black); 174 g.fillRect(squareXOffset,0,squareSize,squareSize); 175 g.setColor(color); 176 g.fillRect(squareXOffset+innerGap, 177 innerGap, 178 squareSize - (innerGap*2), 179 squareSize - (innerGap*2)); 180 g.setColor(Color.white); 181 g.fillRect(squareXOffset+innerGap*2, 182 innerGap*2, 183 squareSize - (innerGap*4), 184 squareSize - (innerGap*4)); 185 g.translate(-(squareSize+squareGap), 0); 186 187 g.translate(squareSize+squareGap, squareSize+squareGap); 188 g.setColor(Color.white); 189 g.fillRect(squareXOffset,0,squareSize,squareSize); 190 g.setColor(color); 191 g.fillRect(squareXOffset+innerGap, 192 innerGap, 193 squareSize - (innerGap*2), 194 squareSize - (innerGap*2)); 195 g.translate(-(squareSize+squareGap), -(squareSize+squareGap)); 196 197 198 199 g.translate((squareSize+squareGap)*2, 0); 200 g.setColor(Color.white); 201 g.fillRect(squareXOffset,0,squareSize,squareSize); 202 g.setColor(color); 203 g.fillRect(squareXOffset+innerGap, 204 innerGap, 205 squareSize - (innerGap*2), 206 squareSize - (innerGap*2)); 207 g.setColor(Color.black); 208 g.fillRect(squareXOffset+innerGap*2, 209 innerGap*2, 210 squareSize - (innerGap*4), 211 squareSize - (innerGap*4)); 212 g.translate(-((squareSize+squareGap)*2), 0); 213 214 g.translate((squareSize+squareGap)*2, (squareSize+squareGap)); 215 g.setColor(Color.black); 216 g.fillRect(squareXOffset,0,squareSize,squareSize); 217 g.setColor(color); 218 g.fillRect(squareXOffset+innerGap, 219 innerGap, 220 squareSize - (innerGap*2), 221 squareSize - (innerGap*2)); 222 g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap)); 223 224 return (squareSize*3+squareGap*2); 225 226 } 227 228 } 229 | Popular Tags |