|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JGradientBar.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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 | 0 |
public JGradientBar() {
|
| 34 |
|
|
| 35 |
// setOpaque( true );
|
|
| 36 |
// setBackground( Color.lightGray );
|
|
| 37 |
|
|
| 38 | 0 |
m_gradient = new JGradientLabel( EMPTY );
|
| 39 | 0 |
m_min = new JNumberLabel();
|
| 40 | 0 |
m_max = new JNumberLabel();
|
| 41 | 0 |
m_min.setHorizontalAlignment( SwingConstants.RIGHT ); |
| 42 | 0 |
m_max.setHorizontalAlignment( SwingConstants.LEFT ); |
| 43 |
|
|
| 44 | 0 |
Font font = getFont().deriveFont( Font.BOLD, 13.0f ); |
| 45 | 0 |
m_min.setFont( font ); |
| 46 | 0 |
m_max.setFont( font ); |
| 47 |
|
|
| 48 | 0 |
setLayout( new FlowLayout( FlowLayout.LEADING ) );
|
| 49 | 0 |
add( m_min ); |
| 50 | 0 |
add( m_gradient ); |
| 51 | 0 |
add( m_max ); |
| 52 |
} // end constructor
|
|
| 53 |
|
|
| 54 | 0 |
public void setColorMap( Color[] colorMap ) { |
| 55 | 0 |
m_gradient.m_colorMap = colorMap; |
| 56 |
} // end setColorMap
|
|
| 57 |
|
|
| 58 | 0 |
public void setLabels( double min, double max ) { |
| 59 | 0 |
m_min.setText( min ); |
| 60 | 0 |
m_max.setText( max ); |
| 61 |
} // end setLabels
|
|
| 62 |
|
|
| 63 |
} // end class JGradientBar
|
|
| 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 | 0 |
public JNumberLabel() {
|
| 74 | 0 |
super();
|
| 75 | 0 |
init(); |
| 76 |
} |
|
| 77 |
|
|
| 78 | 0 |
public JNumberLabel( double number ) { |
| 79 | 0 |
this();
|
| 80 | 0 |
m_regular.setMaximumFractionDigits( 3 ); |
| 81 | 0 |
setText( number ); |
| 82 |
} |
|
| 83 |
|
|
| 84 | 0 |
protected void init() { |
| 85 |
// setOpaque( true );
|
|
| 86 |
// setBackground( Color.lightGray );
|
|
| 87 | 0 |
Dimension d = new Dimension( WIDTH, JGradientLabel.HEIGHT );
|
| 88 | 0 |
setSize( d ); |
| 89 | 0 |
setPreferredSize( d ); |
| 90 |
} |
|
| 91 |
|
|
| 92 | 0 |
public void setText( double number ) { |
| 93 |
|
|
| 94 |
// Only very small numbers (except for zero) as well as very large numbers
|
|
| 95 |
// should be displayed in scientific notation
|
|
| 96 | 0 |
String text; |
| 97 | 0 |
if ( ( number != 0 && Math.abs( number ) < 0.01 )
|
| 98 |
|| Math.abs( number ) > 999 ) {
|
|
| 99 | 0 |
text = m_scientificNotation.format( number ); |
| 100 |
} else {
|
|
| 101 | 0 |
text = m_regular.format( number ); |
| 102 |
} |
|
| 103 |
|
|
| 104 | 0 |
super.setText( text );
|
| 105 |
} |
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
class JGradientLabel extends JLabel { |
|
| 109 |
|
|
| 110 |
// fields
|
|
| 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 | 0 |
public JGradientLabel( Color[] colorMap ) {
|
| 121 |
|
|
| 122 |
// colorMap should contain at least two colors
|
|
| 123 | 0 |
if ( 0 == colorMap.length ) {
|
| 124 |
|
|
| 125 |
// if there are no colors, default to grey for both colors
|
|
| 126 |
|
|
| 127 | 0 |
colorMap = new Color[2];
|
| 128 | 0 |
colorMap[0] = colorMap[1] = Color.LIGHT_GRAY; |
| 129 | 0 |
} else if ( 1 == colorMap.length ) { |
| 130 |
|
|
| 131 |
// if there is only one color, make the second color the same
|
|
| 132 | 0 |
Color color = colorMap[0]; |
| 133 | 0 |
colorMap = new Color[2];
|
| 134 | 0 |
colorMap[0] = colorMap[1] = color; |
| 135 |
} |
|
| 136 |
|
|
| 137 | 0 |
m_colorMap = colorMap; |
| 138 |
|
|
| 139 | 0 |
Dimension d = new Dimension( WIDTH, HEIGHT );
|
| 140 | 0 |
setSize( d ); |
| 141 | 0 |
setPreferredSize( d ); |
| 142 |
} // end constructor
|
|
| 143 |
|
|
| 144 | 0 |
protected void paintComponent( Graphics g ) { |
| 145 |
|
|
| 146 | 0 |
Graphics2D g2 = ( Graphics2D ) g; |
| 147 |
|
|
| 148 | 0 |
final int width = getWidth();
|
| 149 | 0 |
final int height = getHeight();
|
| 150 |
|
|
| 151 | 0 |
int x = 0;
|
| 152 | 0 |
int y = 0;
|
| 153 |
|
|
| 154 |
// Go from one color to another, creating a gradient in-between,
|
|
| 155 |
// painting from left to right on this component
|
|
| 156 | 0 |
int intervalCount = m_colorMap.length - 1;
|
| 157 | 0 |
int intervalWidth = width / intervalCount;
|
| 158 |
|
|
| 159 | 0 |
for ( int i = 0; i < intervalCount; i++ ) { |
| 160 |
|
|
| 161 | 0 |
Color color1 = m_colorMap[i]; |
| 162 | 0 |
Color color2 = m_colorMap[i + 1]; |
| 163 |
|
|
| 164 | 0 |
GradientPaint oneColorToAnother = new GradientPaint( x, y, color1, x
|
| 165 |
+ intervalWidth, y, color2 ); |
|
| 166 | 0 |
g2.setPaint( oneColorToAnother ); |
| 167 | 0 |
g2.fillRect( x, y, width, height ); |
| 168 |
|
|
| 169 |
// Move to paint the next vertical screen slice of this component
|
|
| 170 | 0 |
x += ( width / intervalCount ); |
| 171 |
} |
|
| 172 |
} // end paintComponent
|
|
| 173 |
} // end JGradientLabel
|
|
| 174 |
|
|
||||||||||