1 package baseCode.gui;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.Font;
7 import java.awt.GradientPaint;
8 import java.awt.Graphics;
9 import java.awt.Graphics2D;
10 import java.text.DecimalFormat;
11
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.SwingConstants;
15
16 /***
17 * A GUI legend component that displays a color map as a color gradient from min to max, traversing all the colors in
18 * the color map.
19 *
20 * @author Will Braynen
21 * @version $Id: JGradientBar.java,v 1.8 2004/07/27 03:18:58 pavlidis Exp $
22 */
23 public class JGradientBar extends JPanel {
24
25 protected JNumberLabel m_min;
26 protected JNumberLabel m_max;
27 protected JGradientLabel m_gradient;
28 protected final static Color[] EMPTY = {
29 Color.GRAY, Color.GRAY
30 };
31
32 /*** Creates a new instance of JGradientBar */
33 public JGradientBar() {
34
35
36
37
38 m_gradient = new JGradientLabel( EMPTY );
39 m_min = new JNumberLabel();
40 m_max = new JNumberLabel();
41 m_min.setHorizontalAlignment( SwingConstants.RIGHT );
42 m_max.setHorizontalAlignment( SwingConstants.LEFT );
43
44 Font font = getFont().deriveFont( Font.BOLD, 13.0f );
45 m_min.setFont( font );
46 m_max.setFont( font );
47
48 setLayout( new FlowLayout( FlowLayout.LEADING ) );
49 add( m_min );
50 add( m_gradient );
51 add( m_max );
52 }
53
54 public void setColorMap( Color[] colorMap ) {
55 m_gradient.m_colorMap = colorMap;
56 }
57
58 public void setLabels( double min, double max ) {
59 m_min.setText( min );
60 m_max.setText( max );
61 }
62
63 }
64
65 class JNumberLabel extends JLabel {
66
67 protected static final DecimalFormat m_scientificNotation = new DecimalFormat(
68 "0.##E0" );
69 protected static final DecimalFormat m_regular = new DecimalFormat();
70 protected static final int MINIMUM_WIDTH = 100;
71 protected static final int WIDTH = 40;
72
73 public JNumberLabel() {
74 super();
75 init();
76 }
77
78 public JNumberLabel( double number ) {
79 this();
80 m_regular.setMaximumFractionDigits( 3 );
81 setText( number );
82 }
83
84 protected void init() {
85
86
87 Dimension d = new Dimension( WIDTH, JGradientLabel.HEIGHT );
88 setSize( d );
89 setPreferredSize( d );
90 }
91
92 public void setText( double number ) {
93
94
95
96 String text;
97 if ( ( number != 0 && Math.abs( number ) < 0.01 )
98 || Math.abs( number ) > 999 ) {
99 text = m_scientificNotation.format( number );
100 } else {
101 text = m_regular.format( number );
102 }
103
104 super.setText( text );
105 }
106 }
107
108 class JGradientLabel extends JLabel {
109
110
111 protected static final int WIDTH = 100;
112 protected static final int HEIGHT = 20;
113 protected Color[] m_colorMap;
114
115 /***
116 * Creates a new instance of JGradientLabel
117 *
118 * @param colorMap Color[]
119 */
120 public JGradientLabel( Color[] colorMap ) {
121
122
123 if ( 0 == colorMap.length ) {
124
125
126
127 colorMap = new Color[2];
128 colorMap[0] = colorMap[1] = Color.LIGHT_GRAY;
129 } else if ( 1 == colorMap.length ) {
130
131
132 Color color = colorMap[0];
133 colorMap = new Color[2];
134 colorMap[0] = colorMap[1] = color;
135 }
136
137 m_colorMap = colorMap;
138
139 Dimension d = new Dimension( WIDTH, HEIGHT );
140 setSize( d );
141 setPreferredSize( d );
142 }
143
144 protected void paintComponent( Graphics g ) {
145
146 Graphics2D g2 = ( Graphics2D ) g;
147
148 final int width = getWidth();
149 final int height = getHeight();
150
151 int x = 0;
152 int y = 0;
153
154
155
156 int intervalCount = m_colorMap.length - 1;
157 int intervalWidth = width / intervalCount;
158
159 for ( int i = 0; i < intervalCount; i++ ) {
160
161 Color color1 = m_colorMap[i];
162 Color color2 = m_colorMap[i + 1];
163
164 GradientPaint oneColorToAnother = new GradientPaint( x, y, color1, x
165 + intervalWidth, y, color2 );
166 g2.setPaint( oneColorToAnother );
167 g2.fillRect( x, y, width, height );
168
169
170 x += ( width / intervalCount );
171 }
172 }
173 }