root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / layers / MinMaxLines.java @ 19354
History | View | Annotate | Download (5.88 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.beans.canvas.layers; |
20 |
|
21 |
import java.awt.BasicStroke; |
22 |
import java.awt.Color; |
23 |
import java.awt.Cursor; |
24 |
import java.awt.Graphics; |
25 |
import java.awt.Graphics2D; |
26 |
import java.awt.Stroke; |
27 |
import java.awt.event.MouseEvent; |
28 |
|
29 |
import org.gvsig.raster.beans.canvas.DrawableElement; |
30 |
import org.gvsig.raster.beans.canvas.GCanvas; |
31 |
/**
|
32 |
* Representa dos l?neas rectas que se?alizan el m?ximo y el m?nimo.
|
33 |
*
|
34 |
* 14-oct-2007
|
35 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
36 |
*/
|
37 |
public class MinMaxLines extends DrawableElement { |
38 |
private int smallLine = 5; |
39 |
|
40 |
/**
|
41 |
* Representa el borde de separacion para los limites
|
42 |
*/
|
43 |
private int border = 2; |
44 |
|
45 |
private double minPos = 0.0D; |
46 |
private double maxPos = 1.0D; |
47 |
|
48 |
private boolean minSelected = false; |
49 |
private boolean maxSelected = false; |
50 |
/**
|
51 |
* Constructor. Asigna el color
|
52 |
* @param c
|
53 |
*/
|
54 |
public MinMaxLines(Color c) { |
55 |
setColor(c); |
56 |
} |
57 |
|
58 |
/*
|
59 |
* (non-Javadoc)
|
60 |
* @see org.gvsig.raster.beans.canvas.DrawableElement#firstDrawActions()
|
61 |
*/
|
62 |
public void firstDrawActions() { |
63 |
} |
64 |
|
65 |
private int valueToPixel(double value) { |
66 |
return (int) Math.round(canvas.getCanvasMinX() + border + ((canvas.getCanvasMaxX() - canvas.getCanvasMinX() - (border * 2)) * value)); |
67 |
} |
68 |
|
69 |
private double pixelToValue(int pixel) { |
70 |
return ((double) (pixel - canvas.getCanvasMinX() - border) / (double) (canvas.getCanvasMaxX() - canvas.getCanvasMinX() - (border * 2))); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Dibujado de las l?neas y cuadros sobre el canvas
|
75 |
*/
|
76 |
public void paint(Graphics g) { |
77 |
g.setColor(color); |
78 |
int y = (int) canvas.getCanvasMinY(); |
79 |
int x1 = valueToPixel(minPos);
|
80 |
int x2 = valueToPixel(maxPos);
|
81 |
Graphics2D g2 = (Graphics2D) g; |
82 |
float dash1[] = {10.0f}; |
83 |
BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); |
84 |
Stroke stroke2 = g2.getStroke();
|
85 |
g2.setStroke(stroke); |
86 |
|
87 |
g2.drawLine(x1, y, x1, canvas.getCanvasMaxY()); |
88 |
g2.drawLine(x2, y, x2, canvas.getCanvasMaxY()); |
89 |
|
90 |
g2.setStroke(stroke2); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Asigna el objeto JComponent donde se pintan los elementos. Inicializa los puntos
|
95 |
* de inicio y fin de l?nea y asigna los listener
|
96 |
* @param canvas
|
97 |
*/
|
98 |
public void setCanvas(GCanvas canvas) { |
99 |
super.setCanvas(canvas);
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Se captura el punto a arrastrar
|
104 |
*/
|
105 |
public boolean mousePressed(MouseEvent e) { |
106 |
if (e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
|
107 |
if (e.getX() >= (valueToPixel(minPos) - 3) && e.getX() <= (valueToPixel(minPos) + 3)) { |
108 |
minSelected = true;
|
109 |
return false; |
110 |
} |
111 |
if (e.getX() >= (valueToPixel(maxPos) - 3) && e.getX() <= (valueToPixel(maxPos) + 3)) { |
112 |
maxSelected = true;
|
113 |
return false; |
114 |
} |
115 |
} |
116 |
return true; |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Inicializamos el punto arrastrado a un valor fuera del array
|
121 |
*/
|
122 |
public boolean mouseReleased(MouseEvent e) { |
123 |
minSelected = false;
|
124 |
maxSelected = false;
|
125 |
if (actionsManager != null) |
126 |
actionsManager.end(new double[] { getMinDistance(), getMaxDistance() }); |
127 |
return true; |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Cuando se ha pinchado un punto y se arrastra se define aqu? su comportamiento.
|
132 |
*/
|
133 |
public boolean mouseDragged(MouseEvent e) { |
134 |
if (minSelected) {
|
135 |
minPos = pixelToValue(e.getX()); |
136 |
if (minPos < 0) |
137 |
minPos = 0;
|
138 |
if (minPos > 1.0) |
139 |
minPos = 1.0;
|
140 |
if (minPos > maxPos)
|
141 |
maxPos = minPos; |
142 |
canvas.repaint(); |
143 |
return false; |
144 |
} |
145 |
if (maxSelected) {
|
146 |
maxPos = pixelToValue(e.getX()); |
147 |
if (maxPos < 0) |
148 |
maxPos = 0;
|
149 |
if (maxPos > 1.0) |
150 |
maxPos = 1.0;
|
151 |
if (maxPos < minPos)
|
152 |
minPos = maxPos; |
153 |
canvas.repaint(); |
154 |
return false; |
155 |
} |
156 |
return true; |
157 |
} |
158 |
|
159 |
/*
|
160 |
* (non-Javadoc)
|
161 |
* @see org.gvsig.raster.beans.canvas.DrawableElement#mouseMoved(java.awt.event.MouseEvent)
|
162 |
*/
|
163 |
public boolean mouseMoved(MouseEvent e) { |
164 |
if (e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
|
165 |
if (e.getX() >= (valueToPixel(minPos) - 3) && |
166 |
e.getX() <= (valueToPixel(minPos) + 3)) {
|
167 |
canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); |
168 |
return false; |
169 |
} |
170 |
if (e.getX() >= (valueToPixel(maxPos) - 3) && |
171 |
e.getX() <= (valueToPixel(maxPos) + 3)) {
|
172 |
canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); |
173 |
return false; |
174 |
} |
175 |
} |
176 |
return true; |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Obtiene la distancia de la l?nea del m?nimo al inicio del histograma. La
|
181 |
* distancia la devuelve en tanto por cien del tama?o del gr?fico
|
182 |
* @return Porcentaje de distancia entre el punto inicial del gr?fico hasta la
|
183 |
* l?nea del m?nimo.
|
184 |
*/
|
185 |
public double getMinDistance() { |
186 |
return 0; |
187 |
// return ((canvas.getCanvasMinX() + minDistance) * 100 ) / canvas.getCanvasWidth();
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Obtiene la distancia de la l?nea del m?ximo al inicio del histograma. La
|
192 |
* distancia la devuelve en tanto por cien del tama?o del gr?fico
|
193 |
* @return Porcentaje de distancia entre el punto inicial del gr?fico hasta la
|
194 |
* l?nea del m?ximo.
|
195 |
*/
|
196 |
public double getMaxDistance() { |
197 |
return 0; |
198 |
// return ((canvas.getCanvasMaxX() - maxDistance) * 100 ) / canvas.getCanvasWidth();
|
199 |
} |
200 |
|
201 |
public void setMax(double max) { |
202 |
//maxDistance = max
|
203 |
} |
204 |
|
205 |
public void setMin(double min) { |
206 |
double value = (min * canvas.getCanvasWidth()) / 100; |
207 |
|
208 |
} |
209 |
} |