64 private int textGap = 5; 65 private Font font = new Font(Font.DIALOG, Font.PLAIN, 12); 66 private String sampleText; 67 68 private int swatchWidth = 50; 69 70 private Color oldColor = null; 71 72 private JColorChooser getColorChooser() { 73 return (JColorChooser)SwingUtilities.getAncestorOfClass( 74 JColorChooser.class, this); 75 } 76 77 public Dimension getPreferredSize() { 78 JComponent host = getColorChooser(); 79 if (host == null) { 80 host = this; 81 } 82 FontMetrics fm = host.getFontMetrics(getFont()); 83 84 int ascent = fm.getAscent(); 85 int height = fm.getHeight(); 86 int width = SwingUtilities2.stringWidth(host, fm, getSampleText()); 87 88 int y = height*3 + textGap*3; 89 int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3; 90 return new Dimension( x,y ); 91 } 92 93 public void paintComponent(Graphics g) { 94 if (oldColor == null) 95 oldColor = getForeground(); 96 97 g.setColor(getBackground()); 98 g.fillRect(0,0,getWidth(),getHeight()); 99 100 if (this.getComponentOrientation().isLeftToRight()) { 101 int squareWidth = paintSquares(g, 0); 102 int textWidth = paintText(g, squareWidth); 103 paintSwatch(g, squareWidth + textWidth); 104 } else { 105 int swatchWidth = paintSwatch(g, 0); 106 int textWidth = paintText(g, swatchWidth); 111 112 private int paintSwatch(Graphics g, int offsetX) { 113 int swatchX = offsetX; 114 g.setColor(oldColor); 115 g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2)); 116 g.setColor(getForeground()); 117 g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) ); 118 return (swatchX+swatchWidth); 119 } 120 121 private int paintText(Graphics g, int offsetX) { 122 g.setFont(getFont()); 123 JComponent host = getColorChooser(); 124 if (host == null) { 125 host = this; 126 } 127 FontMetrics fm = SwingUtilities2.getFontMetrics(host, g); 128 129 int ascent = fm.getAscent(); 130 int height = fm.getHeight(); 131 int width = SwingUtilities2.stringWidth(host, fm, getSampleText()); 132 133 int textXOffset = offsetX + textGap; 134 135 Color color = getForeground(); 136 137 g.setColor(color); 138 139 SwingUtilities2.drawString(host, g, getSampleText(),textXOffset+(textGap/2), 140 ascent+2); 141 142 g.fillRect(textXOffset, 143 ( height) + textGap, 144 width + (textGap), 145 height +2); 146 147 g.setColor(Color.black); 148 SwingUtilities2.drawString(host, g, getSampleText(), 149 textXOffset+(textGap/2), 150 height+ascent+textGap+2); 151 152 153 g.setColor(Color.white); 154 155 g.fillRect(textXOffset, 156 ( height + textGap) * 2, 157 width + (textGap), 158 height +2); 159 160 g.setColor(color); 161 SwingUtilities2.drawString(host, g, getSampleText(), 162 textXOffset+(textGap/2), 163 ((height+textGap) * 2)+ascent+2); 164 165 return width + textGap*3; 166 167 } 168 169 private int paintSquares(Graphics g, int offsetX) { 170 171 int squareXOffset = offsetX; 172 Color color = getForeground(); 173 174 g.setColor(Color.white); 175 g.fillRect(squareXOffset,0,squareSize,squareSize); 176 g.setColor(color); 177 g.fillRect(squareXOffset+innerGap, 178 innerGap, 179 squareSize - (innerGap*2), 180 squareSize - (innerGap*2)); 181 g.setColor(Color.white); | 64 private int textGap = 5; 65 private Font font = new Font(Font.DIALOG, Font.PLAIN, 12); 66 private String sampleText; 67 68 private int swatchWidth = 50; 69 70 private Color oldColor = null; 71 72 private JColorChooser getColorChooser() { 73 return (JColorChooser)SwingUtilities.getAncestorOfClass( 74 JColorChooser.class, this); 75 } 76 77 public Dimension getPreferredSize() { 78 JComponent host = getColorChooser(); 79 if (host == null) { 80 host = this; 81 } 82 FontMetrics fm = host.getFontMetrics(getFont()); 83 84 int height = fm.getHeight(); 85 int width = SwingUtilities2.getTextUIDrawing(host) 86 .getStringWidth(host, fm, getSampleText()); 87 88 int y = height*3 + textGap*3; 89 int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3; 90 return new Dimension( x,y ); 91 } 92 93 public void paintComponent(Graphics g) { 94 if (oldColor == null) 95 oldColor = getForeground(); 96 97 g.setColor(getBackground()); 98 g.fillRect(0,0,getWidth(),getHeight()); 99 100 if (this.getComponentOrientation().isLeftToRight()) { 101 int squareWidth = paintSquares(g, 0); 102 int textWidth = paintText(g, squareWidth); 103 paintSwatch(g, squareWidth + textWidth); 104 } else { 105 int swatchWidth = paintSwatch(g, 0); 106 int textWidth = paintText(g, swatchWidth); 111 112 private int paintSwatch(Graphics g, int offsetX) { 113 int swatchX = offsetX; 114 g.setColor(oldColor); 115 g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2)); 116 g.setColor(getForeground()); 117 g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) ); 118 return (swatchX+swatchWidth); 119 } 120 121 private int paintText(Graphics g, int offsetX) { 122 g.setFont(getFont()); 123 JComponent host = getColorChooser(); 124 if (host == null) { 125 host = this; 126 } 127 FontMetrics fm = SwingUtilities2.getFontMetrics(host, g); 128 129 int ascent = fm.getAscent(); 130 int height = fm.getHeight(); 131 int width = SwingUtilities2.getTextUIDrawing(host) 132 .getStringWidth(host, fm, getSampleText()); 133 134 int textXOffset = offsetX + textGap; 135 136 Color color = getForeground(); 137 138 g.setColor(color); 139 140 SwingUtilities2.getTextUIDrawing(host) 141 .drawString(host, g, getSampleText(),textXOffset+(textGap/2), 142 ascent+2); 143 144 g.fillRect(textXOffset, 145 ( height) + textGap, 146 width + (textGap), 147 height +2); 148 149 g.setColor(Color.black); 150 SwingUtilities2.getTextUIDrawing(host) 151 .drawString(host, g, getSampleText(), 152 textXOffset+(textGap/2), 153 height+ascent+textGap+2); 154 155 156 g.setColor(Color.white); 157 158 g.fillRect(textXOffset, 159 ( height + textGap) * 2, 160 width + (textGap), 161 height +2); 162 163 g.setColor(color); 164 SwingUtilities2.getTextUIDrawing(host) 165 .drawString(host, g, getSampleText(), 166 textXOffset+(textGap/2), 167 ((height+textGap) * 2)+ascent+2); 168 169 return width + textGap*3; 170 171 } 172 173 private int paintSquares(Graphics g, int offsetX) { 174 175 int squareXOffset = offsetX; 176 Color color = getForeground(); 177 178 g.setColor(Color.white); 179 g.fillRect(squareXOffset,0,squareSize,squareSize); 180 g.setColor(color); 181 g.fillRect(squareXOffset+innerGap, 182 innerGap, 183 squareSize - (innerGap*2), 184 squareSize - (innerGap*2)); 185 g.setColor(Color.white); |