Revision 19319 trunk/extensions/extRasterTools-SE/src/org/gvsig/raster/beans/canvas/layers/MinMaxLines.java

View differences:

MinMaxLines.java
21 21
import java.awt.Color;
22 22
import java.awt.Cursor;
23 23
import java.awt.Graphics;
24
import java.awt.Point;
25 24
import java.awt.event.MouseEvent;
26
import java.awt.event.MouseListener;
27
import java.awt.event.MouseMotionListener;
28
import java.awt.geom.Point2D;
29 25

  
30 26
import org.gvsig.raster.beans.canvas.DrawableElement;
31 27
import org.gvsig.raster.beans.canvas.GCanvas;
32

  
33 28
/**
34 29
 * Representa dos l?neas rectas que se?alizan el m?ximo y el m?nimo.
35 30
 *
36 31
 * 14-oct-2007
37 32
 * @author Nacho Brodin (nachobrodin@gmail.com)
38 33
 */
39
public class MinMaxLines extends DrawableElement implements MouseListener, MouseMotionListener {
34
public class MinMaxLines extends DrawableElement {
35
	private int     smallLine = 5;
40 36

  
41
	private Point2D minP1 = null;
42
	private Point2D minP2 = null;
43
	private Point2D maxP1 = null;
44
	private Point2D maxP2 = null;
45
	private int     smallLine = 5;
46
	private int     minDistance = 2;
47
	private int     maxDistance = 2;
37
	/**
38
	 * Representa el borde de separacion para los limites 
39
	 */
40
	private int border = 2;
41
	
42
	private double     minPos = 0.0D;
43
	private double     maxPos = 1.0D;
44

  
48 45
	private boolean minSelected = false;
49 46
	private boolean maxSelected = false;
50 47
	/**
......
56 53
	}
57 54
	
58 55
	/*
59
	 *  (non-Javadoc)
60
	 * @see org.gvsig.gui.beans.canvas.DrawableElement#firstDraw()
56
	 * (non-Javadoc)
57
	 * @see org.gvsig.raster.beans.canvas.DrawableElement#firstDrawActions()
61 58
	 */
62 59
	public void firstDrawActions() {
63
		//El primer dibujado asigna valores a los puntos inicial y final.
64
		minP1 = new Point(canvas.getCanvasMinX(), canvas.getCanvasMinY());
65
		minP2 = new Point(canvas.getCanvasMinX(), canvas.getCanvasMaxY());
66
		maxP1 = new Point(canvas.getCanvasMaxX(), canvas.getCanvasMinY());
67
		maxP2 = new Point(canvas.getCanvasMaxX(), canvas.getCanvasMaxY());
68 60
	}
69 61
	
62
	private int valueToPixel(double value) {
63
		return (int) Math.round(canvas.getCanvasMinX() + border + ((canvas.getCanvasMaxX() - canvas.getCanvasMinX() - (border * 2)) * value));
64
	}
65

  
66
	private double pixelToValue(int pixel) {
67
		return ((double) (pixel - canvas.getCanvasMinX() - border) / (double) (canvas.getCanvasMaxX() - canvas.getCanvasMinX() - (border * 2)));
68
	}
69
	
70 70
	/**
71 71
	 * Dibujado de las l?neas y cuadros sobre el canvas
72 72
	 */
73 73
	public void paint(Graphics g) {
74 74
		g.setColor(color);
75 75
		
76
		int initY = (int)minP1.getY();
77
		while((initY + smallLine) < minP2.getY()) {
78
			int xDist = (int)minP1.getX() + minDistance; 
76
		int initY = (int) canvas.getCanvasMinY();
77
		while((initY + smallLine) < canvas.getCanvasMaxY()) {
78
			int xDist = valueToPixel(minPos); 
79 79
			g.drawLine(xDist, initY, xDist, initY + smallLine);
80 80
			initY += (smallLine * 2);
81 81
		}
82 82
		
83
		initY = (int)maxP1.getY();
84
		while((initY + smallLine) < maxP2.getY()) {
85
			int xDist = (int)maxP1.getX() - maxDistance;
83
		initY = (int) canvas.getCanvasMinY();
84
		while((initY + smallLine) < canvas.getCanvasMaxY()) {
85
			int xDist = valueToPixel(maxPos);
86 86
			g.drawLine(xDist, initY, xDist, initY + smallLine);
87 87
			initY += (smallLine * 2);
88 88
		}
......
95 95
	 */
96 96
	public void setCanvas(GCanvas canvas) {
97 97
		super.setCanvas(canvas);
98
		canvas.addMouseListener(this);
99
		canvas.addMouseMotionListener(this);
100 98
	}
101 99
	
102 100
	/**
103
	 * Selecciona el eje m?nimo o m?ximo si es rat?n est? sobre ?l.
104
	 */
105
	public void mouseClicked(MouseEvent e) {
106
	}
107

  
108
	/**
109 101
	 * Se captura el punto a arrastrar
110 102
	 */
111
	public void mousePressed(MouseEvent e) {
112
		if(e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
113
			if(e.getX() >= (canvas.getCanvasMinX() + minDistance) && e.getX() <= (canvas.getCanvasMinX() + minDistance + 8) ) 
103
	public boolean mousePressed(MouseEvent e) {
104
		if (e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
105
			if (e.getX() >= (valueToPixel(minPos) - 3) && e.getX() <= (valueToPixel(minPos) + 3)) {
114 106
				minSelected = true;
115
			if(e.getX() >= (canvas.getCanvasMaxX() - maxDistance) && e.getX() <= (canvas.getCanvasMaxX() - maxDistance + 8) ) 
107
				return false;
108
			}
109
			if (e.getX() >= (valueToPixel(maxPos) - 3) && e.getX() <= (valueToPixel(maxPos) + 3)) {
116 110
				maxSelected = true;
111
				return false;
112
			}
117 113
		}
118
			
114
		return true;
119 115
	}
120 116

  
121 117
	/**
122 118
	 * Inicializamos el punto arrastrado a un valor fuera del array
123 119
	 */
124
	public void mouseReleased(MouseEvent e) {
120
	public boolean mouseReleased(MouseEvent e) {
125 121
		minSelected = false;
126 122
		maxSelected = false;
127
		if(actionsManager != null)
128
			actionsManager.end(new double[]{getMinDistance(), getMaxDistance()});
123
		if (actionsManager != null)
124
			actionsManager.end(new double[] { getMinDistance(), getMaxDistance() });
125
		return true;
129 126
	}
130 127

  
131 128
	/**
132 129
	 * Cuando se ha pinchado un punto y se arrastra se define aqu? su comportamiento.
133 130
	 */
134
	public void mouseDragged(MouseEvent e) {
135
		if(minSelected)
136
			minDistance = e.getX() - 4;
137
		if(maxSelected)
138
			maxDistance = canvas.getCanvasMaxX() - e.getX();
139
		canvas.repaint();
131
	public boolean mouseDragged(MouseEvent e) {
132
		if (minSelected) {
133
			minPos = pixelToValue(e.getX());
134
			if (minPos < 0)
135
				minPos = 0;
136
			if (minPos > 1.0)
137
				minPos = 1.0;
138
			if (minPos > maxPos)
139
				maxPos = minPos;
140
			canvas.repaint();
141
			return false;
142
		}
143
		if (maxSelected) {
144
			maxPos = pixelToValue(e.getX());
145
			if (maxPos < 0)
146
				maxPos = 0;
147
			if (maxPos > 1.0)
148
				maxPos = 1.0;
149
			if (maxPos < minPos)
150
				minPos = maxPos;
151
			canvas.repaint();
152
			return false;
153
		}
154
		return true;
140 155
	}
141 156

  
142
	public void mouseMoved(MouseEvent e) {
143
		if(e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
144
			if( e.getX() >= (canvas.getCanvasMinX() + minDistance) && e.getX() <= (canvas.getCanvasMinX() + minDistance + 3) ||
145
				e.getX() >= (canvas.getCanvasMaxX() - maxDistance) && e.getX() <= (canvas.getCanvasMaxX() - maxDistance + 3))
146
				canvas.setCursor(new Cursor(Cursor.HAND_CURSOR));
147
			else
148
				canvas.setCursor(new Cursor(GCanvas.DEFAULT_CURSOR));
157
	/*
158
	 * (non-Javadoc)
159
	 * @see org.gvsig.raster.beans.canvas.DrawableElement#mouseMoved(java.awt.event.MouseEvent)
160
	 */
161
	public boolean mouseMoved(MouseEvent e) {
162
		if (e.getY() > canvas.getCanvasMinY() && e.getY() < canvas.getCanvasMaxY()) {
163
			if (e.getX() >= (valueToPixel(minPos) - 3) &&
164
					e.getX() <= (valueToPixel(minPos) + 3)) {
165
				canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
166
				return false;
167
			}
168
			if (e.getX() >= (valueToPixel(maxPos) - 3) &&
169
					e.getX() <= (valueToPixel(maxPos) + 3)) {
170
				canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
171
				return false;
172
			}
149 173
		}
174
		return true;
150 175
	}
151 176
	
152
	public void mouseEntered(MouseEvent e) {
153
	}
154

  
155
	public void mouseExited(MouseEvent e) {
156
	}
157
	
158 177
	/**
159 178
	 * Obtiene la distancia de la l?nea del m?nimo al inicio del histograma. La 
160 179
	 * distancia la devuelve en tanto por cien del tama?o del gr?fico
......
162 181
	 * l?nea del m?nimo.
163 182
	 */
164 183
	public double getMinDistance() {
165
		return ((canvas.getCanvasMinX() + minDistance) * 100 ) / canvas.getCanvasWidth();
184
		return 0;
185
//		return ((canvas.getCanvasMinX() + minDistance) * 100 ) / canvas.getCanvasWidth();
166 186
	}
167 187
	
168 188
	/**
......
172 192
	 * l?nea del m?ximo.
173 193
	 */
174 194
	public double getMaxDistance() {
175
		return ((canvas.getCanvasMaxX() - maxDistance) * 100 ) / canvas.getCanvasWidth();
195
		return 0;
196
//		return ((canvas.getCanvasMaxX() - maxDistance) * 100 ) / canvas.getCanvasWidth();
176 197
	}
177 198
	
178 199
	public void setMax(double max) {

Also available in: Unified diff