43
43
44
44
package org .baderlab .csplugins .enrichmentmap .view .legend ;
45
45
46
+ import java .awt .BorderLayout ;
46
47
import java .awt .Color ;
47
48
import java .awt .Dimension ;
48
- import java .awt .Font ;
49
49
import java .awt .GradientPaint ;
50
50
import java .awt .Graphics ;
51
51
import java .awt .Graphics2D ;
52
52
import java .awt .RenderingHints ;
53
53
import java .awt .geom .Point2D ;
54
54
import java .awt .geom .Rectangle2D ;
55
55
56
+ import javax .swing .BorderFactory ;
57
+ import javax .swing .JLabel ;
56
58
import javax .swing .JPanel ;
57
59
import javax .swing .UIManager ;
58
60
59
- import org .cytoscape .util .swing .LookAndFeelUtil ;
60
-
61
- /**
62
- * Created by
63
- * User: risserlin
64
- * Date: Feb 5, 2009
65
- * Time: 3:55:52 PM
66
- * <p>
67
- * enrichment map legend panel
68
- */
61
+ import org .baderlab .csplugins .enrichmentmap .view .util .SwingUtil ;
69
62
70
63
@ SuppressWarnings ("serial" )
71
64
public class ColorLegendPanel extends JPanel {
72
65
73
66
private static final int WIDTH = 150 ;
74
67
private static final int HEIGHT = 36 ;
75
-
76
- private final Color minColor ;
77
- private final Color maxColor ;
78
- private final String phenotype1 ;
79
- private final String phenotype2 ;
68
+
69
+ private final Color minColor ;
70
+ private final Color maxColor ;
71
+
72
+ private final boolean legacy ;
80
73
81
74
public ColorLegendPanel (Color minColor , Color maxColor , String phenotype1 , String phenotype2 ) {
82
- this .minColor = minColor ;
83
- this .maxColor = maxColor ;
84
- this .phenotype1 = phenotype1 ;
85
- this .phenotype2 = phenotype2 ;
86
-
87
- setPreferredSize (new Dimension (WIDTH , HEIGHT ));
88
- setOpaque (false );
89
- }
90
-
91
- @ Override
92
- public void paint (Graphics g ) {
93
- final int w = getWidth ();
94
- final int h = getHeight ();
95
-
96
- if (w <= 0 || h <= 0 )
97
- return ;
75
+ this (minColor , maxColor , phenotype1 , phenotype2 , true );
76
+ }
77
+
78
+ public ColorLegendPanel (Color minColor , Color maxColor , String phenotype1 , String phenotype2 , boolean legacy ) {
79
+ this .legacy = legacy ;
80
+ this .minColor = minColor ;
81
+ this .maxColor = maxColor ;
82
+
83
+ setPreferredSize (new Dimension (WIDTH , HEIGHT ));
84
+ setOpaque (false );
85
+
86
+ JPanel gradientRectangle = new GradientRectanglePanel ();
87
+ JPanel gradientParentPanel = new JPanel (new BorderLayout ());
88
+ gradientParentPanel .add (gradientRectangle , BorderLayout .CENTER );
89
+ gradientParentPanel .setBorder (BorderFactory .createLineBorder (UIManager .getColor ("Separator.foreground" )));
98
90
99
- Graphics2D g2d = (Graphics2D ) g ;
100
- g2d .setRenderingHint (RenderingHints .KEY_ANTIALIASING , RenderingHints .VALUE_ANTIALIAS_ON );
101
- g2d .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING , RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
91
+ JLabel minLabel = new JLabel (phenotype1 );
92
+ JLabel maxLabel = new JLabel (phenotype2 );
93
+ SwingUtil .makeSmall (minLabel , maxLabel );
94
+ JPanel labelPanel = new JPanel (new BorderLayout ());
95
+ labelPanel .add (minLabel , BorderLayout .WEST );
96
+ labelPanel .add (maxLabel , BorderLayout .EAST );
97
+ labelPanel .setOpaque (false );
98
+
99
+ setLayout (new BorderLayout ());
100
+ add (gradientParentPanel , BorderLayout .CENTER );
101
+ add (labelPanel , BorderLayout .SOUTH );
102
+ }
103
+
104
+ private class GradientRectanglePanel extends JPanel {
105
+ @ Override
106
+ public void paint (Graphics g ) {
107
+ if (getWidth () <= 0 || getHeight () <= 0 )
108
+ return ;
109
+
110
+ Graphics2D g2d = (Graphics2D ) g ;
111
+ g2d .setRenderingHint (RenderingHints .KEY_ANTIALIASING , RenderingHints .VALUE_ANTIALIAS_ON );
112
+ g2d .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING , RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
113
+
114
+ if (legacy )
115
+ paintLegacyOneDatasetGradient (g2d );
116
+ else
117
+ paintNormalGradient (g2d );
118
+ }
119
+
120
+ private void paintNormalGradient (Graphics2D g2d ) {
121
+ final int w = getWidth ();
122
+ final int h = getHeight ();
123
+
124
+ Point2D .Float p1 = new Point2D .Float (0f , 0f ); // Gradient line start
125
+ Point2D .Float p2 = new Point2D .Float (w , 0f ); // Gradient line end
126
+
127
+ GradientPaint g1 = new GradientPaint (p1 , minColor , p2 , maxColor , false ); // Acyclic gradient
128
+ Rectangle2D .Float rect1 = new Rectangle2D .Float (p1 .x , p1 .y , w , h );
129
+
130
+ g2d .setPaint (g1 );
131
+ g2d .fill (rect1 );
132
+ }
102
133
103
- float ww = w / 5.f ;
104
- float hPad = ww / 2.f ; // To center the legend horizontally
105
-
106
- Point2D .Float p1 = new Point2D .Float (hPad , 0.f ); //Gradient line start
107
- Point2D .Float p2 = new Point2D .Float (hPad + ww , 0.f ); //Gradient line end
108
-
109
- //empty white box
110
- Point2D .Float p3 = new Point2D .Float (hPad + ww , 0.f ); //Gradient line start
111
-
112
- Point2D .Float p5 = new Point2D .Float (hPad + 2 * ww , 0.f ); //Gradient line start
113
-
114
- Point2D .Float p7 = new Point2D .Float (hPad + 3 * ww , 0.f ); //Gradient line start
115
- Point2D .Float p8 = new Point2D .Float (hPad + 4 * ww , 0.f ); //Gradient line end
116
-
117
- float w1 = 30 ;
118
- float w2 = 30 ;
119
- float hh = h / 2 ;
120
-
121
- // Need to create two gradients, one one for the max and one for the min
122
- GradientPaint g1 = new GradientPaint (p1 , minColor , p2 , Color .WHITE , false ); //Acyclic gradient
123
- GradientPaint g2 = new GradientPaint (p7 , Color .WHITE , p8 , maxColor , false ); //Acyclic gradient
124
- Rectangle2D .Float rect1 = new Rectangle2D .Float (p1 .x , p1 .y , w1 , hh );
125
- Rectangle2D .Float rect2 = new Rectangle2D .Float (p3 .x , p3 .y , w2 , hh );
126
- Rectangle2D .Float rect3 = new Rectangle2D .Float (p5 .x , p5 .y , w2 , hh );
127
- Rectangle2D .Float rect4 = new Rectangle2D .Float (p7 .x , p7 .y , w1 , hh );
128
-
129
- g2d .setFont (getLabelFont ());
130
- float tyOffset = hh + h / 3.f ; // Text y offset
131
-
132
- if (minColor != Color .WHITE ) {
134
+ private void paintLegacyOneDatasetGradient (Graphics2D g2d ) {
135
+ final int w = getWidth ();
136
+ final int h = getHeight ();
137
+ float ww = w / 5f ;
138
+
139
+ // first box
140
+ Point2D .Float p1 = new Point2D .Float (0f , 0f ); // Gradient line start
141
+ Point2D .Float p2 = new Point2D .Float (ww , 0f ); // Gradient line end
142
+
143
+ // empty white box
144
+ Point2D .Float p3 = new Point2D .Float (ww * 4 , 0f ); // Gradient line start
145
+ Point2D .Float p4 = new Point2D .Float (ww * 5 , 0f ); // Gradient line start
146
+
147
+ GradientPaint g1 = new GradientPaint (p1 , minColor , p2 , Color .WHITE , false ); // Acyclic gradient
148
+ GradientPaint g2 = new GradientPaint (p3 , Color .WHITE , p4 , maxColor , false ); // Acyclic gradient
149
+
150
+ Rectangle2D .Float rect1 = new Rectangle2D .Float (p1 .x , p1 .y , ww , h );
151
+ Rectangle2D .Float rect2 = new Rectangle2D .Float (p2 .x , p2 .y , ww *3 , h );
152
+ Rectangle2D .Float rect3 = new Rectangle2D .Float (p3 .x , p3 .y , ww , h );
153
+
133
154
g2d .setPaint (g1 );
134
155
g2d .fill (rect1 );
135
156
g2d .setPaint (Color .WHITE );
136
-
137
- // make a white block
138
- g2d .setPaint (Color .WHITE );
139
157
g2d .fill (rect2 );
140
-
141
- g2d .setPaint (getLabelForeground ());
142
- g2d .drawString (phenotype1 , p1 .x , p1 .y + tyOffset );
143
- } else {
144
- g2d .setPaint (getLabelForeground ());
145
- g2d .drawString (phenotype1 , p5 .x , p5 .y + tyOffset );
158
+ g2d .setPaint (g2 );
159
+ g2d .fill (rect3 );
160
+
146
161
}
147
-
148
- // Make a white block
149
- g2d .setPaint (Color .WHITE );
150
- g2d .fill (rect3 );
151
-
152
- g2d .setPaint (g2 );
153
- g2d .fill (rect4 );
154
-
155
- // Border
156
- g2d .setPaint (UIManager .getColor ("Separator.foreground" ));
157
- g2d .draw (new Rectangle2D .Float (p1 .x , p1 .y , (2 * w1 + 2 * w2 ), hh ));
158
-
159
- g2d .setPaint (getLabelForeground ());
160
- g2d .drawString (phenotype2 , p7 .x , p7 .y + tyOffset );
161
162
}
162
163
163
- private static Font getLabelFont () {
164
- return UIManager .getFont ("Label.font" ).deriveFont (LookAndFeelUtil .getSmallFontSize ());
165
- }
166
-
167
- private static Color getLabelForeground () {
168
- return UIManager .getColor ("Label.foreground" );
169
- }
170
164
}
0 commit comments