1 19 package jcckit; 20 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.awt.Image ; 26 import java.awt.RenderingHints ; 27 import java.awt.geom.AffineTransform ; 28 import java.awt.geom.NoninvertibleTransformException ; 29 30 import jcckit.graphic.GraphPoint; 31 import jcckit.graphic.Renderer; 32 import jcckit.renderer.Graphics2DRenderer; 33 import jcckit.util.ConfigParameters; 34 import jcckit.util.Factory; 35 36 42 public class Graphics2DPlotCanvas extends GraphicsPlotCanvas2 { 43 44 public static final String ANTI_ALIASINGD_KEY = "antiAliasing"; 45 46 private static final AffineTransform IDENTITY = new AffineTransform (); 47 48 53 protected class Graphics2DPainter extends GraphicsPainter { 54 public Graphics2DPainter(Component component) { 55 super(component); 56 } 57 58 62 protected void prepare(Graphics g) { 63 ((Graphics2D ) g).setTransform(IDENTITY); 64 } 65 66 70 protected Renderer createRenderer(Graphics g) { 71 ((Graphics2D ) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, 72 _antiAliasing ? RenderingHints.VALUE_ANTIALIAS_ON 73 : RenderingHints.VALUE_ANTIALIAS_OFF); 74 ((Graphics2D ) g).setTransform(_transformation); 75 return ((Graphics2DRenderer) Factory.create(_renderer)) 76 .init((Graphics2D ) g); 77 } 78 79 protected void calculateTransformation(Dimension size) { 80 double pWidth = getPaper().getMaxX() - getPaper().getMinX(); 81 double pHeight = getPaper().getMaxY() - getPaper().getMinY(); 82 double scale = Math.min(size.width / pWidth, size.height / pHeight); 83 double x0 = 0.5 * getHorizontalAnchor().getFactor() 84 * (size.width - scale * pWidth) + scale * getPaper().getMinX(); 85 double y0 = 0.5 * getVerticalAnchor().getFactor() * (scale * pHeight 86 - size.height) 87 + size.height + scale * getPaper().getMinY(); 88 _transformation = new AffineTransform (scale, 0, 0, -scale, x0, y0); 89 } 90 } 91 92 protected class Graphics2DCanvas extends GraphicsCanvas { 93 public Graphics2DCanvas() { 94 super(); 95 _painter = new Graphics2DPainter(this); 96 } 97 } 98 99 protected class Graphics2DJPanel extends GraphicsJPanel { 100 public Graphics2DJPanel() { 101 super(); 102 _painter = new Graphics2DPainter(this); 103 } 104 } 105 106 protected String _renderer = "jcckit.renderer.Graphics2DRenderer"; 107 private AffineTransform _transformation; 108 private final boolean _antiAliasing; 109 110 125 public Graphics2DPlotCanvas(ConfigParameters config) { 126 super(config); 127 _antiAliasing = config.getBoolean(ANTI_ALIASINGD_KEY, true); 128 setRenderer("jcckit.renderer.Graphics2DRenderer"); 129 } 130 131 135 protected void createGraphicsCanvas() { 136 _canvas = new Graphics2DCanvas(); 137 } 138 139 143 protected void createGraphicsJPanel() { 144 _jPanel = new Graphics2DJPanel(); 145 } 146 147 152 public void draw2DInto(Image image) 153 { 154 _jPanel.setSize(image.getWidth(null), image.getHeight(null)); 155 _jPanel.update(image.getGraphics()); 156 } 157 158 159 164 public GraphPoint mapCursorPosition(int x, int y) { 165 double[] coordinates = new double[] {(double) x, (double) y, 0, 0}; 166 try { 167 _transformation.inverseTransform(coordinates, 0, coordinates, 2, 1); 168 } catch (NoninvertibleTransformException e) {} 169 return new GraphPoint(coordinates[2], coordinates[3]); 170 } 171 172 173 174 182 public static void main(String [] args) throws Exception { 183 run(args, "jcckit.Graphics2DPlotCanvas"); 184 } 185 } 186
| Popular Tags
|