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.FontMetrics;
35  import java.awt.Graphics;
36  import java.awt.Insets;
37  import java.util.Vector;
38  
39  /***
40    * Simple polar plot widget.
41    *
42    * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
43  
44  
45  
46    * @author H. Philip Crotwell
47    */
48  public class PolarPlot extends Canvas {
49  
50     Container pappy;
51  
52     Dimension minSize;
53  
54     int width;   // circles, so height=width
55  
56     protected Vector segments = new Vector();
57     
58     double[] circleRadius;
59     
60     double outsideRadius = 6371.0;
61     
62     int centerX, centerY;
63  
64  	public static final short FULL = 0;
65  	public static final short HALF = 1;
66  	public static final short QUARTER = 2;
67  	short displayMode;
68  		
69     public PolarPlot(Container parent) {
70        pappy = parent;
71        width = 250;
72        minSize = new Dimension(width,width);
73     	setDisplayMode(FULL);
74     }
75  
76     public PolarPlot(Container parent, int width) {
77        pappy = parent;
78        this.width = width;
79        minSize = new Dimension(width,width);
80     	setDisplayMode(FULL);
81  
82     }
83  
84  	public void setCircles(double[] circles) {
85  		circleRadius = new double[circles.length];
86  		for (int i=0;i<circles.length;i++) {
87  			circleRadius[i] = circles[i];
88  		}
89  	}
90  	
91  	public void setOutsideRadius(double r) {
92  		outsideRadius = r;
93  	}
94  	
95  	public void appendSegment(TimeDist[] td) {
96  		segments.addElement(td);
97  	}
98  	
99  	public void clearSegments() {
100 		segments.removeAllElements();
101 	}
102 	
103 	public void setDisplayMode(short mode) {
104 		displayMode = mode;
105 		switch (displayMode) {
106 			case FULL:
107 				centerX = width/2;
108 				centerY = width/2;
109 				break;
110 			case HALF:
111 				centerX = width/2;
112 				centerY = width;
113 				break;
114 			case QUARTER:
115 				centerX = 0;
116 				centerY = width;
117 				break;
118 		}
119 	}
120 	
121    public Dimension preferredSize() {
122       return minimumSize();
123    }
124 
125    public synchronized Dimension minimumSize() {
126       return minSize;
127    }
128    
129    public Insets insets() {
130       return new Insets(5,5,5,5);
131    }
132  
133    public void paint(Graphics g) {
134       FontMetrics fontMetrics = g.getFontMetrics();
135 
136       g.drawRect(0, 0, width-1, width-1);
137 		plotCircles(g);
138       plotData(g);
139    }
140 
141 	protected void plotCircles(Graphics g) {
142 		int radius;
143 		if (circleRadius != null) {
144 			switch (displayMode) {
145 				case FULL:
146 					for (int i=0; i<circleRadius.length; i++) {
147 						radius = (int)Math.rint(circleRadius[i]/outsideRadius*width/2);
148 						g.drawOval(width/2-radius, width/2-radius,
149 							 2*radius, 2*radius);
150 					}
151 					break;
152 				case HALF:
153 					for (int i=0; i<circleRadius.length; i++) {
154 						radius = (int)Math.rint(circleRadius[i]/outsideRadius*width/2);
155 						g.drawArc(width/2-radius, width-radius,
156 							2*radius, 2*radius, 0, 180);
157 					}
158 					break;
159 				case QUARTER:
160 					for (int i=0; i<circleRadius.length; i++) {
161 						radius = (int)Math.rint(circleRadius[i]/outsideRadius*width);
162 						g.drawArc(-1*radius, width-radius,
163 							2*radius, 2*radius, 0, 90);
164 					}
165 					break;
166 			}
167 		}
168 	}
169 
170    protected void plotData(Graphics g) {
171 		int numSegments = segments.size();
172 		double scale = centerY/outsideRadius;
173       for (int segNum=0;segNum<numSegments;segNum++) {
174          g.setColor(colorForSegment(segNum));
175          TimeDist[] data = (TimeDist[])segments.elementAt(segNum);
176          for (int i=0;i<data.length-1;i++) {
177             g.drawLine(centerX - (int)Math.rint(
178             	(outsideRadius-data[i].depth)*Math.cos(data[i].dist+Math.PI/2)*scale), 
179                centerY - (int)Math.rint(
180             	(outsideRadius-data[i].depth)*Math.sin(data[i].dist+Math.PI/2)*scale),
181                centerX - (int)Math.rint(
182             	(outsideRadius-data[i+1].depth)*Math.cos(data[i+1].dist+Math.PI/2)*scale), 
183                centerY - (int)Math.rint(
184             	(outsideRadius-data[i+1].depth)*Math.sin(data[i+1].dist+Math.PI/2)*scale));
185          }
186       }
187       g.setColor(Color.black);
188    }
189 
190    public Color colorForSegment(int segNum) {
191       if (segments.size() == 1) return Color.black;
192 
193       int colorInc = (int)Math.floor(255/(segments.size()-1));
194       return new Color(segNum*colorInc,255-segNum*colorInc,(segNum % 3)*127);
195    }
196 
197 }