Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / layers / MinMaxLines.java @ 19296

History | View | Annotate | Download (5.72 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.Color;
22
import java.awt.Cursor;
23
import java.awt.Graphics;
24
import java.awt.Point;
25
import java.awt.event.MouseEvent;
26
import java.awt.event.MouseListener;
27
import java.awt.event.MouseMotionListener;
28
import java.awt.geom.Point2D;
29

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

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

    
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;
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.gui.beans.canvas.DrawableElement#firstDraw()
61
         */
62
        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
        }
69
        
70
        /**
71
         * Dibujado de las l?neas y cuadros sobre el canvas
72
         */
73
        public void paint(Graphics g) {
74
                g.setColor(color);
75
                
76
                int initY = (int)minP1.getY();
77
                while((initY + smallLine) < minP2.getY()) {
78
                        int xDist = (int)minP1.getX() + minDistance; 
79
                        g.drawLine(xDist, initY, xDist, initY + smallLine);
80
                        initY += (smallLine * 2);
81
                }
82
                
83
                initY = (int)maxP1.getY();
84
                while((initY + smallLine) < maxP2.getY()) {
85
                        int xDist = (int)maxP1.getX() - maxDistance;
86
                        g.drawLine(xDist, initY, xDist, initY + smallLine);
87
                        initY += (smallLine * 2);
88
                }
89
        }
90

    
91
        /**
92
         * Asigna el objeto JComponent donde se pintan los elementos. Inicializa los puntos
93
         * de inicio y fin de l?nea y asigna los listener
94
         * @param canvas
95
         */
96
        public void setCanvas(GCanvas canvas) {
97
                super.setCanvas(canvas);
98
                canvas.addMouseListener(this);
99
                canvas.addMouseMotionListener(this);
100
        }
101
        
102
        /**
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
         * Se captura el punto a arrastrar
110
         */
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) ) 
114
                                minSelected = true;
115
                        if(e.getX() >= (canvas.getCanvasMaxX() - maxDistance) && e.getX() <= (canvas.getCanvasMaxX() - maxDistance + 8) ) 
116
                                maxSelected = true;
117
                }
118
                        
119
        }
120

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

    
131
        /**
132
         * Cuando se ha pinchado un punto y se arrastra se define aqu? su comportamiento.
133
         */
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();
140
        }
141

    
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));
149
                }
150
        }
151
        
152
        public void mouseEntered(MouseEvent e) {
153
        }
154

    
155
        public void mouseExited(MouseEvent e) {
156
        }
157
        
158
        /**
159
         * Obtiene la distancia de la l?nea del m?nimo al inicio del histograma. La 
160
         * distancia la devuelve en tanto por cien del tama?o del gr?fico
161
         * @return Porcentaje de distancia entre el punto inicial del gr?fico hasta la
162
         * l?nea del m?nimo.
163
         */
164
        public double getMinDistance() {
165
                return ((canvas.getCanvasMinX() + minDistance) * 100 ) / canvas.getCanvasWidth();
166
        }
167
        
168
        /**
169
         * Obtiene la distancia de la l?nea del m?ximo al inicio del histograma. La 
170
         * distancia la devuelve en tanto por cien del tama?o del gr?fico
171
         * @return Porcentaje de distancia entre el punto inicial del gr?fico hasta la
172
         * l?nea del m?ximo.
173
         */
174
        public double getMaxDistance() {
175
                return ((canvas.getCanvasMaxX() - maxDistance) * 100 ) / canvas.getCanvasWidth();
176
        }
177
        
178
        public void setMax(double max) {
179
                //maxDistance = max
180
        }
181
        
182
        public void setMin(double min) {
183
                double value = (min *  canvas.getCanvasWidth()) / 100;
184
                
185
        }
186
}