1 19 package jcckit; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Canvas ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Frame ; 26 import java.awt.Graphics ; 27 import java.awt.Image ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.util.Properties ; 31 32 import jcckit.data.DataPlot; 33 import jcckit.graphic.GraphPoint; 34 import jcckit.graphic.GraphicalElement; 35 import jcckit.graphic.Renderer; 36 import jcckit.plot.Plot; 37 import jcckit.plot.PlotCanvas; 38 import jcckit.plot.PlotEvent; 39 import jcckit.plot.PlotEventType; 40 import jcckit.renderer.GraphicsRenderer; 41 import jcckit.renderer.Transformation; 42 import jcckit.util.ConfigParameters; 43 import jcckit.util.ConfigParametersBasedConfigData; 44 import jcckit.util.Factory; 45 import jcckit.util.PropertiesBasedConfigData; 46 47 62 public class GraphicsPlotCanvas extends PlotCanvas { 63 64 public static final String BACKGROUND_KEY = "background", 65 FOREGROUND_KEY = "foreground", 66 DOUBLE_BUFFERING_KEY = "doubleBuffering"; 67 68 75 protected class GraphicsPainter { 76 private final Component _component; 77 private Image _buffer; 78 private Image _coordinateSystem; 79 private Graphics _bufferGraphics; 80 private Dimension _currentSize; 81 82 86 public GraphicsPainter(Component component) { 87 _component = component; 88 } 89 90 95 public void paint(Graphics g) 96 { 97 Dimension size = _component.getSize(); 98 if (size.width <= 0) 99 { 100 size.width = 1; 101 } 102 if (size.height <= 0) 103 { 104 size.height = 1; 105 } 106 if (_reset || !size.equals(_currentSize)) 107 { 108 init(size); 109 } 110 Plot plot = getPlot(); 112 drawCoordinateSystem(size, plot, g); 113 drawPlot(plot, _doubleBuffering ? _bufferGraphics : g); 114 if (_doubleBuffering) 115 { 116 g.drawImage(_buffer, 0, 0, null); 117 } 118 if (_marker != null) 119 { 120 _marker.renderWith(createRenderer(g)); 121 } 122 } 124 125 private void drawPlot(Plot plot, Graphics g) 126 { 127 prepare(g); 128 if (_doubleBuffering) 129 { 130 g.drawImage(_coordinateSystem, 0, 0, null); 131 } 132 Renderer renderer = createRenderer(g); 133 GraphicalElement[] curves = plot.getCurves(); 134 for (int i = 0; i < curves.length; i++) { 135 curves[i].renderWith(renderer); 136 } 137 GraphicalElement annotation = plot.getAnnotation(); 138 if (annotation != null) 139 { 140 annotation.renderWith(renderer); 141 } 142 if (plot.isLegendVisible()) { 143 plot.getLegend().renderWith(renderer); 144 } 145 } 146 147 private void init(Dimension size) 148 { 149 _reset = false; 150 _currentSize = size; 151 if (_buffer != null) 152 { 153 _buffer.flush(); 154 _bufferGraphics.dispose(); 155 } 156 if (_doubleBuffering) 157 { 158 _buffer = _component.createImage(size.width, size.height); 159 _bufferGraphics = _buffer.getGraphics(); 160 } 161 if (_coordinateSystem != null) 162 { 163 _coordinateSystem.flush(); 164 _coordinateSystem = null; 165 } 166 calculateTransformation(size); 167 } 168 169 private void drawCoordinateSystem(Dimension size, Plot plot, Graphics g) 170 { 171 if (_coordinateSystem == null || _doubleBuffering == false) 172 { 173 if (_doubleBuffering) 174 { 175 _coordinateSystem = _component.createImage(size.width, size.height); 176 g = _coordinateSystem.getGraphics(); 177 } 178 g.setColor(_component.getBackground()); 179 g.fillRect(0, 0, size.width, size.height); 180 g.setColor(_component.getForeground()); 181 plot.getCoordinateSystem().renderWith(createRenderer(g)); 182 if (_doubleBuffering) 183 { 184 g.dispose(); 185 } 186 } 187 } 188 189 193 protected void prepare(Graphics g) {} 194 195 200 protected void calculateTransformation(Dimension size) { 201 _transformation = new Transformation(size.width, size.height, 202 getPaper(), getHorizontalAnchor(), getVerticalAnchor()); 203 } 204 205 209 protected Renderer createRenderer(Graphics g) { 210 return ((GraphicsRenderer) Factory.create(_renderer)) 211 .init(g, _component, _transformation); 212 } 213 } 214 215 220 protected class GraphicsCanvas extends Canvas { 221 222 protected GraphicsPainter _painter; 223 224 228 public GraphicsCanvas() { 229 super(); 230 _painter = new GraphicsPainter(this); 231 } 232 233 234 public void handleEvent(PlotEvent event) { 235 repaint(); 236 } 237 238 239 public void update(Graphics g) { 240 paint(g); 241 } 242 243 248 public void paint(Graphics g) { 249 _painter.paint(g); 250 } 251 } 253 private Transformation _transformation; 254 private boolean _reset = true; 255 private boolean _doubleBuffering; 256 private String _renderer = "jcckit.renderer.GraphicsRenderer"; 257 258 259 protected GraphicsCanvas _canvas; 260 261 private GraphicalElement _marker; 262 263 286 public GraphicsPlotCanvas(ConfigParameters config) { 287 super(config); 288 createGraphicsCanvas(); 289 _doubleBuffering = config.getBoolean(DOUBLE_BUFFERING_KEY, true); 290 _canvas.setBackground(config.getColor(BACKGROUND_KEY, 291 _canvas.getBackground())); 292 _canvas.setForeground(config.getColor(FOREGROUND_KEY, 293 _canvas.getForeground())); 294 } 295 296 301 public void setRenderer(String className) { 302 _renderer = className; 303 } 304 305 309 public void setDoubleBuffering(boolean doubleBuffering) 310 { 311 _doubleBuffering = doubleBuffering; 312 } 313 314 320 public void drawInto(Image image) 321 { 322 _canvas.setSize(image.getWidth(null), image.getHeight(null)); 323 _canvas.paint(image.getGraphics()); 324 } 325 326 330 protected void createGraphicsCanvas() { 331 _canvas = new GraphicsCanvas(); 332 } 333 334 335 public void plotChanged(PlotEvent event) { 336 if (event.getType() == PlotEventType.COODINATE_SYSTEM_CHANGED) 337 { 338 _reset = true;; 339 } 340 _canvas.handleEvent(event); 341 } 342 343 349 public Canvas getGraphicsCanvas() { 350 return _canvas; 351 } 352 353 358 public GraphPoint mapCursorPosition(int x, int y) { 359 return _transformation.transformBack(x, y); 360 } 361 362 367 public void setMarker(GraphicalElement marker) 368 { 369 _marker = marker; 370 } 371 372 380 public static void main(String [] args) throws Exception { 381 run(args, "jcckit.GraphicsPlotCanvas"); 382 } 383 384 protected static void run(String [] args, String plotCanvas) 385 throws Exception { 386 if (args.length == 0) { 387 printUsageAndExit(plotCanvas); 388 } 389 String file = args[0]; 390 String renderer = null; 391 if (file.equals("-r")) { 392 if (args.length > 2) { 393 renderer = args[1]; 394 file = args[2]; 395 } else { 396 printUsageAndExit(plotCanvas); 397 } 398 } 399 Properties p = new Properties (); 400 p.put(Factory.CLASS_NAME_KEY, plotCanvas); 401 ConfigParameters config 402 = new ConfigParameters( 403 new ConfigParametersBasedConfigData( 404 new ConfigParameters(new PropertiesBasedConfigData(file)), 405 new ConfigParameters(new PropertiesBasedConfigData(p)))); 406 GraphicsPlotCanvas canvas = (GraphicsPlotCanvas) Factory.create(config); 407 if (renderer != null) { 408 canvas.setRenderer(renderer); 409 } 410 canvas.connect(DataPlot.create(config)); 411 412 Frame frame = new Frame ("JCCKit: " + file); 413 frame.addWindowListener(new WindowAdapter () { 414 public void windowClosing(WindowEvent event) { 415 System.exit(0); 416 } 417 } 418 ); 419 frame.add(canvas.getGraphicsCanvas(), BorderLayout.CENTER); 420 frame.setSize(666, 555); 421 frame.show(); 422 } 423 424 private static void printUsageAndExit(String plotCanvas) { 425 System.out.println("Usage: java " + plotCanvas 426 + " [-r <renderer class>] <properties file>"); 427 System.exit(0); 428 } 429 } 430
| Popular Tags
|