View Javadoc

1   /*
2     The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
3     Copyright (C) 1998-2000 University of South Carolina
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    The current version can be found at 
20    <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
21  
22    Bug reports and comments should be directed to 
23    H. Philip Crotwell, crotwell@seis.sc.edu or
24    Tom Owens, owens@seis.sc.edu
25  
26  */
27  
28  package edu.sc.seis.TauP;
29  
30  import java.awt.Canvas;
31  import java.awt.Color;
32  import java.awt.Container;
33  import java.awt.Dimension;
34  import java.awt.Event;
35  import java.awt.FontMetrics;
36  import java.awt.Graphics;
37  import java.awt.Insets;
38  import java.util.Vector;
39  
40  /***
41    * Simple y versus x plot widget.
42    *
43    * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
44  
45  
46  
47    * @author H. Philip Crotwell
48    */
49  public class XYPlot extends Canvas {
50     Container pappy;
51     Dimension minSize;
52     int width, height;
53     Vector xSegments, ySegments;
54     double[] xData, yData;
55     int xOffset = 40;
56     int yOffset = 40;
57     int xTickWidth = 100;
58     int yTickWidth = 100;
59     
60     double minX = 0.0;
61     double zoomMinX = minX;
62     double maxX = 10.0;
63     double zoomMaxX = maxX;
64     double minY = 0.0;
65     double zoomMinY = minY;
66     double maxY = 10.0;
67     double zoomMaxY = maxY;
68     
69     private double xScale;
70     private double yScale;
71     
72     String title = "Title";
73     String xLabel = "X Label";
74     String yLabel = "Y Label";
75  
76     double mouseDownX, mouseDownY;
77  
78     boolean DEBUG = false;
79  
80     public XYPlot(Container parent) {
81        pappy = parent;
82        width = 600;
83        height = 600;
84        minSize = new Dimension(width,height);
85     }
86  
87     public XYPlot(Container parent, int width, int height) {
88        pappy = parent;
89        this.width = width;
90        this.height = height;
91        minSize = new Dimension(width,height);
92     }
93  
94     public Dimension preferredSize() {
95        return minimumSize();
96     }
97  
98     public synchronized Dimension minimumSize() {
99        return minSize;
100    }
101    
102    public Insets insets() {
103       return new Insets(5,5,5,5);
104    }
105  
106    public void paint(Graphics g) {
107       FontMetrics fontMetrics = g.getFontMetrics();
108 
109       xScale = (width-2.0*xOffset)/(zoomMaxX-zoomMinX);
110       yScale = (height-2.0*yOffset)/(zoomMaxY-zoomMinY);
111 
112       if (DEBUG) System.out.println("xOffset "+xOffset+" yOffset "+yOffset+
113                     " minX "+minX+" maxX "+maxX+" minY "+minY+" maxY "+
114                     maxY+" xScale "+xScale+" yScale "+yScale);
115       g.drawRect(0, 0, width-1, height-1);
116       g.drawLine(xOffset, yOffset, xOffset, height-yOffset);
117       g.drawLine(xOffset, height-yOffset, width-xOffset, height-yOffset);
118       g.drawString(title, 
119                    (width-fontMetrics.stringWidth(title))/2, 
120                    yOffset-fontMetrics.getDescent()-fontMetrics.getLeading());
121       g.drawString(xLabel, 
122                    (width-fontMetrics.stringWidth(xLabel))/2, 
123                    height-fontMetrics.getDescent()-fontMetrics.getLeading());
124 
125       for (int i=0;i*xTickWidth>=zoomMinX && i*xTickWidth<=zoomMaxX;i++) {
126          g.drawLine(xOffset + (int)(xScale*(i*xTickWidth-zoomMinX)),
127             height-yOffset,
128             xOffset + (int)(xScale*(i*xTickWidth-zoomMinX)),
129             height-(int)(3.0/4*yOffset));
130          g.drawString(""+(i*xTickWidth),
131             xOffset + (int)(xScale*(i*xTickWidth-zoomMinX)),
132             height-fontMetrics.getAscent()-fontMetrics.getLeading());
133       }
134       for (int i=0;i*yTickWidth>=zoomMinY && i*yTickWidth<=zoomMaxY;i++) {
135          g.drawLine( xOffset,
136             height - yOffset - (int)(yScale*(i*yTickWidth-zoomMinY)),
137             (int)(3.0/4*xOffset),
138             height - yOffset - (int)(yScale*(i*yTickWidth-zoomMinY)));
139          g.drawString(""+(i*yTickWidth),
140             0, height - yOffset - (int)(yScale*(i*yTickWidth-zoomMinY)));
141       }
142       plotData(g);
143    }
144 
145    public void plotData(Graphics g) {
146       if (xSegments != null && ySegments != null) {
147          if (xSegments.size() != ySegments.size()) {
148             System.out.println("xSegments.size() != ySegments.size()");
149          }
150          for (int segNum=0;segNum<xSegments.size();segNum++) {
151             g.setColor(colorForSegment(segNum)); 
152             xData=(double[])xSegments.elementAt(segNum);
153             yData=(double[])ySegments.elementAt(segNum);
154             if (xData != null) {
155                if (xData.length == yData.length && xData.length > 0) {
156                   for (int i=0;i<xData.length-1;i++)
157                      g.drawLine(xOffset + (int)(xScale*(xData[i]-zoomMinX)), 
158                          height - yOffset - (int)(yScale*(yData[i]-zoomMinY)),
159                          xOffset + (int)(xScale*(xData[i+1]-zoomMinX)),
160                          height -yOffset -(int)(yScale*(yData[i+1]-zoomMinY)));
161                }
162             } else {
163                System.out.println("null data");
164             }
165          }
166       }
167       g.setColor(Color.black);
168    }
169 
170    public Color colorForSegment(int segNum) {
171       if (xSegments.size() == 1) return Color.black;
172 
173       int colorInc = (int)Math.floor(255/(xSegments.size()-1));
174       return new Color(segNum*colorInc,255-segNum*colorInc,(segNum % 3)*127);
175    }
176 
177    public void plot() {
178       xData = new double[2];
179       yData = new double[2];
180       xData[0] = minX;
181       yData[0] = minY;
182       xData[1] = maxX;
183       yData[1] = maxY;
184       xSegments.addElement(xData);
185       ySegments.addElement(yData);
186       xData=null;
187       yData=null;
188    }
189    
190    public boolean mouseDown(Event evt, int x, int y) {
191       mouseDownX = x;
192       mouseDownY = y;
193       return true;
194    }
195    
196    public boolean mouseUp(Event evt, int mouseUpX, int mouseUpY) {
197          /* If the difference is too small then it must have been a mistake. */
198       if (Math.abs(mouseDownX-mouseUpX)+Math.abs(mouseDownY-mouseUpY) > 5) {
199          double tempDown, tempUp;
200          
201          tempDown =(mouseDownX - xOffset)/xScale + zoomMinX;
202          tempUp =(mouseUpX - xOffset)/xScale + zoomMinX;
203          if (tempDown > tempUp) {
204             zoomMinX = tempUp;
205             zoomMaxX = tempDown;
206          } else {
207             zoomMinX = tempDown;
208             zoomMaxX = tempUp;
209          }
210          
211          tempDown =(mouseDownY - yOffset)/yScale + zoomMinY;
212          tempUp =(mouseUpY - yOffset)/yScale + zoomMinY;
213          if (tempDown > tempUp) {
214             zoomMinY = tempUp;
215             zoomMaxY = tempDown;
216          } else {
217             zoomMinY = tempDown;
218             zoomMaxY = tempUp;
219          }
220          
221          repaint();
222       }
223       return true;
224    }
225 }