1 /* 2 * File: SystemColors2.java 3 * 4 * Display the colors used by the system in a scrollpane 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.Graphics; 11 import java.awt.Font; 12 import java.awt.FontMetrics; 13 import java.awt.Color; 14 import java.awt.Canvas; 15 import java.awt.SystemColor; 16 import java.awt.ScrollPane; 17 import java.awt.Adjustable; 18 import java.util.Hashtable; 19 import java.util.Enumeration; 20 21 public class SystemColors2 extends java.applet.Applet { 22 23 // Constants: 24 private ColorCanvas canvas; 25 26 // Initialize the applet: 27 public void init() { 28 29 canvas = new ColorCanvas(); 30 31 ScrollPane appletScroller = new ScrollPane(); 32 appletScroller.add( canvas ); 33 appletScroller.setSize( getSize().width, getSize().height ); 34 add( appletScroller ); 35 36 Adjustable horizontal = appletScroller.getHAdjustable(); 37 horizontal.setUnitIncrement( 5 ); 38 Adjustable vertical = appletScroller.getVAdjustable(); 39 vertical.setUnitIncrement( 5 ); 40 41 } 42 43 public void paint( Graphics g ) { 44 canvas.repaint(); 45 } 46 47 } 48 49 50 class ColorCanvas extends Canvas { 51 52 // size of each color swatch: 53 private int swatchSize; 54 // whitespace around each color swatch: 55 private int border; 56 // total size, including whitespace: 57 private int unitSize; 58 // number of color swatches per row: 59 private int cols; 60 // number of color swatches per column: 61 private int rows; 62 63 // Constructor: 64 public ColorCanvas() { 65 66 super(); 67 68 initColors(); 69 70 // initialize parameters: 71 swatchSize = 70; border = 15; 72 unitSize = swatchSize + 2*border; 73 cols = 4; 74 rows = ( int ) Math.ceil( ( double ) colors.size()/cols ); 75 76 // compute size of the canvas: 77 this.setSize( cols * unitSize, rows * unitSize ); 78 79 } 80 81 // A hashtable of colors: 82 private Hashtable colors = new Hashtable(); 83 84 // Put the system colors in the hashtable: 85 private void initColors() { 86 colors.put( "desktop", SystemColor.desktop ); 87 colors.put( "activeCaption", SystemColor.activeCaption ); 88 colors.put( "activeCaptionText", SystemColor.activeCaptionText ); 89 colors.put( "activeCaptionBorder", SystemColor.activeCaptionBorder ); 90 colors.put( "inactiveCaption", SystemColor.inactiveCaption ); 91 colors.put( "inactiveCaptionText", SystemColor.inactiveCaptionText ); 92 colors.put( "inactiveCaptionBorder", SystemColor.inactiveCaptionBorder ); 93 colors.put( "window", SystemColor.window ); 94 colors.put( "windowBorder", SystemColor.windowBorder ); 95 colors.put( "windowText", SystemColor.windowText ); 96 colors.put( "menu", SystemColor.menu ); 97 colors.put( "menuText", SystemColor.menuText ); 98 colors.put( "text", SystemColor.text ); 99 colors.put( "textText", SystemColor.textText ); 100 colors.put( "textHighlight", SystemColor.textHighlight ); 101 colors.put( "textHighlightText", SystemColor.textHighlightText ); 102 colors.put( "textInactiveText", SystemColor.textInactiveText ); 103 colors.put( "control", SystemColor.control ); 104 colors.put( "controlText", SystemColor.controlText ); 105 colors.put( "controlHighlight", SystemColor.controlHighlight ); 106 colors.put( "controlLtHighlight", SystemColor.controlLtHighlight ); 107 colors.put( "controlShadow", SystemColor.controlShadow ); 108 colors.put( "controlDkShadow", SystemColor.controlDkShadow ); 109 colors.put( "scrollbar", SystemColor.scrollbar ); 110 colors.put( "info", SystemColor.info ); 111 colors.put( "infoText", SystemColor.infoText ); 112 } 113 114 public void paint( Graphics g ) { 115 116 // local variables: 117 int x, y; 118 Color color; 119 String colorstring; 120 121 g.setFont( new Font( "SansSerif", Font.PLAIN, 9 ) ); 122 123 // vertical adjustment for color labels: 124 int descender = g.getFontMetrics().getDescent(); 125 126 // an enumeration of keys in the hashtable: 127 Enumeration colorstrings = colors.keys(); 128 129 // Display rows of color boxes: 130 for ( int i = 0; i < rows; i++ ) { 131 y = border + unitSize*i; 132 for ( int j = 0; j < cols; j++ ) { 133 if ( !colorstrings.hasMoreElements() ) break; 134 colorstring = ( String ) colorstrings.nextElement(); 135 color = ( Color ) colors.get( colorstring ); 136 x = border + unitSize*j; 137 g.setColor( color ); 138 g.fillRect( x, y, swatchSize, swatchSize ); 139 g.setColor( Color.black ); 140 g.drawString( colorstring, x, y - descender ); 141 } 142 } 143 144 } 145 146 }