FileDocCategorySizeDatePackage
DefaultPreviewPanel.javaAPI DocJava SE 5 API6480Fri Aug 26 14:58:00 BST 2005javax.swing.colorchooser

DefaultPreviewPanel

public class DefaultPreviewPanel extends JPanel
The standard preview panel for the color chooser.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.13 12/19/03
author
Steve Wilson
see
JColorChooser

Fields Summary
private int
squareSize
private int
squareGap
private int
innerGap
private int
textGap
private Font
font
private String
sampleText
private int
swatchWidth
private Color
oldColor
Constructors Summary
Methods Summary
private javax.swing.JColorChoosergetColorChooser()


       
        return (JColorChooser)SwingUtilities.getAncestorOfClass(
                                   JColorChooser.class, this);
    
public java.awt.DimensiongetPreferredSize()

        JComponent host = getColorChooser();
        if (host == null) {
            host = this;
        }
	FontMetrics fm = host.getFontMetrics(getFont());

	int ascent = fm.getAscent();
	int height = fm.getHeight();
	int width = SwingUtilities2.stringWidth(host, fm, sampleText);

        int y = height*3 + textGap*3;
	int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
        return new Dimension( x,y );
    
public voidpaintComponent(java.awt.Graphics g)

        if (oldColor == null)
	    oldColor = getForeground();

        g.setColor(getBackground());
	g.fillRect(0,0,getWidth(),getHeight());

	if (this.getComponentOrientation().isLeftToRight()) {
	    int squareWidth = paintSquares(g, 0);
	    int textWidth = paintText(g, squareWidth);
	    paintSwatch(g, squareWidth + textWidth);
	} else {
	    int swatchWidth = paintSwatch(g, 0);
	    int textWidth = paintText(g, swatchWidth);
	    paintSquares(g , swatchWidth + textWidth);

	}
    
private intpaintSquares(java.awt.Graphics g, int offsetX)


	int squareXOffset = offsetX;
        Color color = getForeground();

        g.setColor(Color.white);
	g.fillRect(squareXOffset,0,squareSize,squareSize);
	g.setColor(color);
	g.fillRect(squareXOffset+innerGap, 
		   innerGap, 
		   squareSize - (innerGap*2), 
		   squareSize - (innerGap*2));
	g.setColor(Color.white);
	g.fillRect(squareXOffset+innerGap*2,
		   innerGap*2,
		   squareSize - (innerGap*4),
		   squareSize - (innerGap*4));

        g.setColor(color);
	g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);

	g.translate(squareSize+squareGap, 0);
        g.setColor(Color.black);
	g.fillRect(squareXOffset,0,squareSize,squareSize);
	g.setColor(color);
	g.fillRect(squareXOffset+innerGap, 
		   innerGap, 
		   squareSize - (innerGap*2), 
		   squareSize - (innerGap*2));
	g.setColor(Color.white);
	g.fillRect(squareXOffset+innerGap*2,
		   innerGap*2,
		   squareSize - (innerGap*4),
		   squareSize - (innerGap*4));
	g.translate(-(squareSize+squareGap), 0);

	g.translate(squareSize+squareGap, squareSize+squareGap);
        g.setColor(Color.white);
	g.fillRect(squareXOffset,0,squareSize,squareSize);
	g.setColor(color);
	g.fillRect(squareXOffset+innerGap, 
		   innerGap, 
		   squareSize - (innerGap*2), 
		   squareSize - (innerGap*2));
	g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
	


	g.translate((squareSize+squareGap)*2, 0);
        g.setColor(Color.white);
	g.fillRect(squareXOffset,0,squareSize,squareSize);
	g.setColor(color);
	g.fillRect(squareXOffset+innerGap, 
		   innerGap, 
		   squareSize - (innerGap*2), 
		   squareSize - (innerGap*2));
	g.setColor(Color.black);
	g.fillRect(squareXOffset+innerGap*2,
		   innerGap*2,
		   squareSize - (innerGap*4),
		   squareSize - (innerGap*4));
	g.translate(-((squareSize+squareGap)*2), 0);

	g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
        g.setColor(Color.black);
	g.fillRect(squareXOffset,0,squareSize,squareSize);
	g.setColor(color);
	g.fillRect(squareXOffset+innerGap, 
		   innerGap, 
		   squareSize - (innerGap*2), 
		   squareSize - (innerGap*2));
	g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));

	return (squareSize*3+squareGap*2);

    
private intpaintSwatch(java.awt.Graphics g, int offsetX)

        int swatchX = offsetX;
	g.setColor(oldColor);
	g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2));
	g.setColor(getForeground());
	g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) );
	return (swatchX+swatchWidth);
    
private intpaintText(java.awt.Graphics g, int offsetX)

	g.setFont(getFont());
        JComponent host = getColorChooser();
        if (host == null) {
            host = this;
        }
	FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);

	int ascent = fm.getAscent();
	int height = fm.getHeight();
	int width = SwingUtilities2.stringWidth(host, fm, sampleText);

	int textXOffset = offsetX + textGap;

        Color color = getForeground();

	g.setColor(color);

        SwingUtilities2.drawString(host, g, sampleText,textXOffset+(textGap/2),
                                   ascent+2);

	g.fillRect(textXOffset,
		   ( height) + textGap, 
		   width + (textGap),
		   height +2);

	g.setColor(Color.black);
	SwingUtilities2.drawString(host, g, sampleText, 
		     textXOffset+(textGap/2), 
		     height+ascent+textGap+2);


	g.setColor(Color.white);

	g.fillRect(textXOffset,
		   ( height + textGap) * 2, 
		   width + (textGap),
		   height +2);

	g.setColor(color);
	SwingUtilities2.drawString(host, g, sampleText,
		     textXOffset+(textGap/2), 
		     ((height+textGap) * 2)+ascent+2);

	return width + textGap*3;