1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ubic.basecode.graphics;
16
17 import java.awt.Color;
18 import java.awt.Dimension;
19 import java.awt.Graphics;
20 import java.awt.Graphics2D;
21
22 import javax.swing.JLabel;
23
24
25
26
27
28
29
30 class JGradientLabel extends JLabel {
31
32 private static final int MINHEIGHT = 10;
33 private static final int MINWIDTH = 100;
34 private static final long serialVersionUID = 348823467068723730L;
35 private Color[] m_colorMap;
36
37
38
39
40
41
42 public JGradientLabel( Color[] colorMap ) {
43
44 if ( 0 == colorMap.length ) {
45
46 colorMap = new Color[2];
47 colorMap[0] = colorMap[1] = Color.LIGHT_GRAY;
48 } else if ( 1 == colorMap.length ) {
49
50 Color color = colorMap[0];
51 colorMap = new Color[2];
52 colorMap[0] = colorMap[1] = color;
53 }
54
55 m_colorMap = colorMap;
56
57 Dimension d = new Dimension( MINWIDTH, MINHEIGHT );
58 setSize( d );
59 this.setText( "Scale bar should be here" );
60 setMinimumSize( d );
61 }
62
63
64
65
66
67
68
69 public int drawAtLocation( Graphics g, int x, int y ) {
70 Graphics2D g2 = ( Graphics2D ) g;
71 Color oldColor = g2.getColor();
72 final int width = getWidth();
73 final int height = getHeight();
74
75 assert width > 0;
76 assert height > 0;
77
78 int numColors = m_colorMap.length;
79
80 int intervalWidth = ( int ) Math.ceil( width / ( double ) numColors );
81
82 int currentX = x;
83 for ( int i = 0; i < numColors; i++ ) {
84
85 Color color1 = m_colorMap[i];
86
87 g2.setPaint( color1 );
88 g2.fillRect( currentX, y, intervalWidth, height );
89
90 currentX += ( double ) width / numColors;
91 }
92 g2.setColor( oldColor );
93 return currentX - x;
94 }
95
96 public Color[] getColorMap() {
97 return m_colorMap;
98 }
99
100 public void setColorMap( Color[] mColorMap ) {
101 m_colorMap = mColorMap;
102 this.repaint();
103 }
104
105
106
107
108
109
110 @Override
111 protected void paintComponent( Graphics g ) {
112 super.paintComponent( g );
113 drawAtLocation( g, 0, 0 );
114 }
115 }