View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2011 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13   * specific language governing permissions and limitations under the License.
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   * Color gradient without any text
26   * 
27   * @author wbraynen
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       * Creates a new instance of JGradientLabel
39       * 
40       * @param colorMap Color[]
41       */
42      public JGradientLabel( Color[] colorMap ) {
43  
44          if ( 0 == colorMap.length ) {
45              // if there are no colors, default to grey for both colors
46              colorMap = new Color[2];
47              colorMap[0] = colorMap[1] = Color.LIGHT_GRAY;
48          } else if ( 1 == colorMap.length ) {
49              // if there is only one color, make the second color the same
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" ); // forces repaint.
60          setMinimumSize( d );
61      }
62  
63      /**
64       * @param g
65       * @param x
66       * @param y
67       * @return actual width
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      * (non-Javadoc)
107      * 
108      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
109      */
110     @Override
111     protected void paintComponent( Graphics g ) {
112         super.paintComponent( g );
113         drawAtLocation( g, 0, 0 );
114     }
115 }