1 import java.applet.Applet ; 2 import java.awt.BorderLayout ; 3 import java.awt.Canvas ; 4 import java.awt.Color ; 5 import java.awt.event.MouseAdapter ; 6 import java.awt.event.MouseEvent ; 7 import java.awt.event.MouseListener ; 8 import java.awt.event.MouseMotionAdapter ; 9 import java.awt.event.MouseMotionListener ; 10 import java.util.Properties ; 11 12 import jcckit.GraphicsPlotCanvas; 13 import jcckit.data.DataCurve; 14 import jcckit.data.DataPlot; 15 import jcckit.data.DataPoint; 16 import jcckit.graphic.GraphPoint; 17 import jcckit.graphic.GraphicalElement; 18 import jcckit.graphic.Rectangle; 19 import jcckit.graphic.ShapeAttributes; 20 import jcckit.plot.CartesianCoordinateSystem; 21 import jcckit.util.ConfigParameters; 22 import jcckit.util.PropertiesBasedConfigData; 23 24 32 public class ZoomExample extends Applet 33 { 34 private static double X_MIN_DEFAULT = -1.1; 35 private static double X_MAX_DEFAULT = 1.1; 36 private static double Y_MIN_DEFAULT = -1.1; 37 private static double Y_MAX_DEFAULT = 1.1; 38 39 private static final int NUMBER_OF_ITERATIONS = 6; 40 private static final double H = 1 / (2 * Math.sqrt(3)); 41 42 private static final ShapeAttributes MARKER_ATTRIBUTES 43 = new ShapeAttributes(null, Color.gray, 0, null); 44 45 private GraphicsPlotCanvas _plotCanvas; 46 private GraphPoint _anchor; 47 48 private MouseListener _mouseListener = new MouseAdapter () 49 { 50 public void mouseClicked(MouseEvent event) 51 { 52 if (event.getClickCount() > 1) 53 { 54 resetViewWindow(); 55 } 56 } 57 58 public void mousePressed(MouseEvent event) 59 { 60 _anchor = getPosition(event); 61 drawMarker(_anchor); 62 } 63 64 public void mouseReleased(MouseEvent event) 65 { 66 changeViewingWindow(getPosition(event)); 67 } 68 }; 69 70 private MouseMotionListener _mouseMotionListener = new MouseMotionAdapter () 71 { 72 public void mouseDragged(MouseEvent event) 73 { 74 drawMarker(getPosition(event)); 75 } 76 }; 77 78 public void init() { 79 createPlotCanvas(); 80 _plotCanvas.connect(createData()); 81 82 setLayout(new BorderLayout ()); 83 Canvas graphicsCanvas = _plotCanvas.getGraphicsCanvas(); 84 graphicsCanvas.addMouseListener(_mouseListener); 85 graphicsCanvas.addMouseMotionListener(_mouseMotionListener); 86 add(graphicsCanvas, BorderLayout.CENTER); 87 88 resetViewWindow(); 89 } 90 91 private void createPlotCanvas() { 92 Properties props = new Properties (); 93 props.put("background", "0xffffff"); 94 props.put("paper", "0 0 1 1"); 95 props.put("plot/legendVisible", "false"); 96 _plotCanvas = new GraphicsPlotCanvas( 97 new ConfigParameters(new PropertiesBasedConfigData(props))); 98 } 99 100 private void resetViewWindow() 101 { 102 setCoordinateSystem(X_MIN_DEFAULT, Y_MIN_DEFAULT, 103 X_MAX_DEFAULT, Y_MAX_DEFAULT); 104 } 105 106 private void setCoordinateSystem(double xMin, double yMin, 107 double xMax, double yMax) 108 { 109 Properties props = new Properties (); 110 props.put("xAxis/minimum", Double.toString(xMin)); 111 props.put("xAxis/maximum", Double.toString(xMax)); 112 props.put("xAxis/ticLabelFormat", "%1.3f"); 113 props.put("xAxis/axisLabel", ""); 114 props.put("yAxis/minimum", Double.toString(yMin)); 115 props.put("yAxis/maximum", Double.toString(yMax)); 116 props.put("yAxis/ticLabelFormat", "%1.3f"); 117 props.put("yAxis/axisLabel", ""); 118 props.put("yAxis/axisLength", "0.8"); 119 CartesianCoordinateSystem cs = new CartesianCoordinateSystem( 120 new ConfigParameters(new PropertiesBasedConfigData(props))); 121 _plotCanvas.getPlot().setCoordinateSystem(cs); 122 } 123 124 private DataPlot createData() 125 { 126 DataPlot dataPlot = new DataPlot(); 127 DataCurve dataCurve = new DataCurve(""); 128 DataPoint a = new DataPoint(-3 * H, 0.5); 129 DataPoint b = new DataPoint(3 * H, 0.5); 130 DataPoint c = new DataPoint(0, -1); 131 makeKochCurve(dataCurve, a, b, NUMBER_OF_ITERATIONS); 132 makeKochCurve(dataCurve, b, c, NUMBER_OF_ITERATIONS); 133 makeKochCurve(dataCurve, c, a, NUMBER_OF_ITERATIONS); 134 dataPlot.addElement(dataCurve); 135 return dataPlot; 136 } 137 138 private void makeKochCurve(DataCurve curve, DataPoint p1, DataPoint p2, 139 int numberOfIterations) 140 { 141 if (numberOfIterations == 0) 142 { 143 curve.addElement(p1); 144 curve.addElement(p2); 145 } else 146 { 147 numberOfIterations--; 148 double x1 = p1.getX(); 149 double y1 = p1.getY(); 150 double dx = p2.getX() - x1; 151 double dy = p2.getY() - y1; 152 DataPoint a = new DataPoint(x1 + dx / 3, y1 + dy / 3); 153 DataPoint b = new DataPoint(x1 + dx / 2 - H * dy, y1 + dy / 2 + H * dx); 154 DataPoint c = new DataPoint(x1 + 2 * dx / 3, y1 + 2 * dy / 3); 155 makeKochCurve(curve, p1, a, numberOfIterations); 156 makeKochCurve(curve, a, b, numberOfIterations); 157 makeKochCurve(curve, b, c, numberOfIterations); 158 makeKochCurve(curve, c, p2, numberOfIterations); 159 } 160 } 161 162 private GraphPoint getPosition(MouseEvent event) 163 { 164 return _plotCanvas.mapCursorPosition(event.getX(), event.getY()); 165 } 166 167 private void drawMarker(GraphPoint point) 168 { 169 double x0 = _anchor.getX(); 170 double y0 = _anchor.getY(); 171 double x1 = point.getX(); 172 double y1 = point.getY(); 173 GraphPoint center = new GraphPoint(0.5 * (x0 + x1), 0.5 * (y0 + y1)); 174 setMarker(new Rectangle(center, Math.abs(x1 - x0), Math.abs(y1 - y0), 175 MARKER_ATTRIBUTES)); 176 } 177 178 private void setMarker(GraphicalElement marker) 179 { 180 _plotCanvas.setMarker(marker); 181 _plotCanvas.getGraphicsCanvas().repaint(); 182 } 183 184 private void changeViewingWindow(GraphPoint point) 185 { 186 DataPoint p0 = _plotCanvas.getPlot().transform(_anchor); 187 DataPoint p1 = _plotCanvas.getPlot().transform(point); 188 double xMin = p0.getX(); 189 double xMax = p1.getX(); 190 if (xMin > xMax) 191 { 192 xMin = p1.getX(); 193 xMax = p0.getX(); 194 } 195 double yMin = p0.getY(); 196 double yMax = p1.getY(); 197 if (yMin > yMax) 198 { 199 yMin = p1.getY(); 200 yMax = p0.getY(); 201 } 202 setMarker(null); 203 if (xMin != xMax && yMin != yMax) 204 { 205 setCoordinateSystem(xMin, yMin, xMax, yMax); 206 } 207 } 208 } 209 | Popular Tags |