Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / canvas / layer / DefaultInfoLayer.java @ 2443

History | View | Annotate | Download (5.86 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.swing.impl.canvas.layer;
23

    
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.RenderingHints;
29
import java.awt.geom.Rectangle2D;
30
import java.util.ArrayList;
31

    
32
import org.gvsig.raster.swing.gcanvas.DrawableElement;
33
import org.gvsig.raster.swing.gcanvas.InfoLayer;
34
/**
35
 * Capa para GCanvas que muestra el maximo y minimo de una banda
36
 * 
37
 * @version 04/03/2008
38
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
39
 */
40
public class DefaultInfoLayer extends InfoLayer {
41
        private Color        color       = null;
42
        private double       min         = 0.0;
43
        private double       max         = 1.0;
44
        private String       statusRight = null;
45
        private String       statusLeft  = null;
46
        
47
        /**
48
         * Creacion de un InfoLayer especificando el color
49
         * @param c
50
         */
51
        public DefaultInfoLayer(Color c) {
52
                color = c;
53
        }
54

    
55
        /**
56
         * Definir el borde que va a tener el componente
57
         */
58
        public void firstActions() {
59
                canvas.addBorder(0, 15, 0, 15);
60
        }
61
        
62
        /**
63
         * Establecer los valores maximo y minimo para dicha capa
64
         * @param min
65
         * @param max
66
         */
67
        public void setLimits(double min, double max) {
68
                this.min = min;
69
                this.max = max;
70
        }
71

    
72
        /*
73
         * (non-Javadoc)
74
         * @see org.gvsig.raster.beans.canvas.DrawableElement#paint(java.awt.Graphics)
75
         */
76
        protected void paint(Graphics g) {
77
                
78
                Graphics2D graphics2 = (Graphics2D) g;
79

    
80
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
81
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
82
                graphics2.setRenderingHints(hints);
83

    
84
                graphics2.setColor(color);
85
                graphics2.setFont(new java.awt.Font("Times", 1, 12));
86
                
87
                String min2 = clipDecimals(min, 1) + "";
88
                String max2 = clipDecimals(max, 1) + "";
89

    
90
                Rectangle2D rectangle2D = graphics2.getFontMetrics().getStringBounds(max2, g);
91
                
92
                graphics2.drawString(min2, canvas.getCanvasMinX(), canvas.getCanvasMinX() + 12);
93
                graphics2.drawString(max2, 
94
                                (int) (((Component)canvas).getWidth() - rectangle2D.getWidth() - canvas.getCanvasMinX()), 
95
                                canvas.getCanvasMinX() + 12);
96
                
97
                if (statusRight != null) {
98
                        rectangle2D = graphics2.getFontMetrics().getStringBounds(statusRight, g);
99
                        graphics2.drawString(statusRight, 
100
                                        (int) (((Component)canvas).getWidth() - rectangle2D.getWidth() - canvas.getCanvasMinX()), 
101
                                        (int) ((Component)canvas).getHeight() - canvas.getCanvasMinX());
102
                }
103

    
104
                if (statusLeft != null) {
105
                        graphics2.drawString(statusLeft, 
106
                                        canvas.getCanvasMinX(), 
107
                                        (int) ((Component)canvas).getHeight() - canvas.getCanvasMinX());
108
                }
109
                
110
                ArrayList<DrawableElement> list = canvas.getDrawableElements(DefaultMinMaxLines.class);
111
                if (list.size() > 0) {
112
                        DefaultMinMaxLines minMaxLines = (DefaultMinMaxLines) list.get(0);
113

    
114
                        double minP = min + (minMaxLines.getMinDistance() * (max - min));
115
                        double maxP = min + (minMaxLines.getMaxDistance() * (max - min));
116
                        min2 = clipDecimals(minP, 1) + "";
117
                        max2 = clipDecimals(maxP, 1) + "";
118

    
119
                        list = canvas.getDrawableElements(DefaultGraphicHistogram.class);
120
                        if (list.size() > 0) {
121
                                DefaultGraphicHistogram histogram = (DefaultGraphicHistogram) list.get(0);
122
                                double[] ds = histogram.getHistogramValues();
123
                                double total = 0.0D;
124
                                double totalmin = 0.0D;
125
                                double totalmax = 0.0D;
126
                                for (int i = 0; i < ds.length; i++) {
127
                                        double value = min + (((double) (i * (max - min))) / (double) (ds.length - 1.0D));
128
                                        total += ds[i];
129
                                        if (minP > value)
130
                                                totalmin += ds[i];
131
                                        if (maxP < value)
132
                                                totalmax += ds[i];
133
                                }
134

    
135
                                totalmin = (100.0D * totalmin) / total;
136
                                totalmax = (100.0D * totalmax) / total;
137

    
138
                                min2 = min2 + " (" + clipDecimals(totalmin, 2) + "%)";
139
                                max2 = max2 + " (" + clipDecimals(totalmax, 2) + "%)";
140
                        }
141

    
142
                        rectangle2D = graphics2.getFontMetrics().getStringBounds(max2, g);
143

    
144
                        if (statusLeft == null)
145
                                graphics2.drawString(min2, 
146
                                                canvas.getCanvasMinX(), 
147
                                                (int) ((Component)canvas).getHeight() - canvas.getCanvasMinX());
148

    
149
                        if (statusRight == null)
150
                                graphics2.drawString(max2, 
151
                                                (int) (((Component)canvas).getWidth() - rectangle2D.getWidth() - canvas.getCanvasMinX()), 
152
                                                (int) ((Component)canvas).getHeight() - canvas.getCanvasMinX());
153
                }
154
        }
155
        
156
        /**
157
         * @return the min
158
         */
159
        public double getMin() {
160
                return min;
161
        }
162

    
163
        /**
164
         * @return the max
165
         */
166
        public double getMax() {
167
                return max;
168
        }
169
        
170
        /**
171
         * @param status the status to set
172
         */
173
        public void setStatusRight(String statusRight) {
174
                this.statusRight = statusRight;
175
        }
176
        
177
        /**
178
         * @param statusLeft the statusLeft to set
179
         */
180
        public void setStatusLeft(String statusLeft) {
181
                this.statusLeft = statusLeft;
182
        }
183
        
184
        public void firstDrawActions() {}
185
        
186
        public double clipDecimals(double num, int n) {
187
                long m = (long) Math.pow(10, n);
188
                long aux = Math.round(num * m);
189
                return (double) aux / (double) m;
190
        }
191
}