1 19 package jcckit.renderer; 20 21 import java.awt.BasicStroke ; 22 import java.awt.Color ; 23 import java.awt.Graphics2D ; 24 import java.awt.Shape ; 25 import java.awt.font.TextLayout ; 26 import java.awt.geom.AffineTransform ; 27 import java.awt.geom.Ellipse2D ; 28 import java.awt.geom.GeneralPath ; 29 import java.awt.geom.Rectangle2D ; 30 31 import jcckit.graphic.BasicGraphicalElement; 32 import jcckit.graphic.ClippingRectangle; 33 import jcckit.graphic.ClippingShape; 34 import jcckit.graphic.FillAttributes; 35 import jcckit.graphic.FontStyle; 36 import jcckit.graphic.GraphPoint; 37 import jcckit.graphic.GraphicAttributes; 38 import jcckit.graphic.GraphicalComposite; 39 import jcckit.graphic.GraphicalCompositeRenderer; 40 import jcckit.graphic.LineAttributes; 41 import jcckit.graphic.Oval; 42 import jcckit.graphic.OvalRenderer; 43 import jcckit.graphic.Polygon; 44 import jcckit.graphic.PolygonRenderer; 45 import jcckit.graphic.Rectangle; 46 import jcckit.graphic.RectangleRenderer; 47 import jcckit.graphic.Text; 48 import jcckit.graphic.TextAttributes; 49 import jcckit.graphic.TextRenderer; 50 51 63 public class Graphics2DRenderer implements GraphicalCompositeRenderer, 64 PolygonRenderer, OvalRenderer, 65 TextRenderer, RectangleRenderer { 66 private static final int FS = 1; 67 private static final String DEFAULT_FONT_NAME = "SansSerif"; 68 private static final FontStyle DEFAULT_FONT_STYLE = FontStyle.NORMAL; 69 private static final int DEFAULT_FONT_SIZE = 12; 70 71 private Color _defaultColor; 72 private Graphics2D _graphics; 73 74 81 public Graphics2DRenderer init(Graphics2D graphics) { 82 _graphics = graphics; 83 _defaultColor = graphics.getColor(); return this; 85 } 86 87 93 public void startRendering(GraphicalComposite composite) { 94 ClippingShape shape = composite.getClippingShape(); 95 if (shape != null) { 96 ClippingRectangle rect = shape.getBoundingBox(); 97 _graphics.clip(new Rectangle2D.Double (rect.getMinX(), rect.getMinY(), 98 rect.getMaxX() - rect.getMinX(), rect.getMaxY() - rect.getMinY())); 99 } 100 } 101 102 107 public void finishRendering(GraphicalComposite composite) { 108 _graphics.setClip(null); 109 } 110 111 112 public void render(Polygon polygon) { 113 int numberOfPoints = polygon.getNumberOfPoints(); 114 if (numberOfPoints > 0) { 115 Color currentColor = _graphics.getColor(); 116 GeneralPath p 117 = new GeneralPath (GeneralPath.WIND_EVEN_ODD, numberOfPoints); 118 p.moveTo((float) polygon.getPoint(0).getX(), 119 (float) polygon.getPoint(0).getY()); 120 for (int i = 1; i < numberOfPoints; i++) { 121 p.lineTo((float) polygon.getPoint(i).getX(), 122 (float) polygon.getPoint(i).getY()); 123 } 124 if (polygon.isClosed()) { 125 p.closePath(); 126 } 127 drawShape(p, polygon, currentColor); 128 } 129 } 130 131 134 public void render(Rectangle rectangle) { 135 Color currentColor = _graphics.getColor(); 136 GraphPoint center = rectangle.getCenter(); 137 double width = rectangle.getWidth(); 138 double height = rectangle.getHeight(); 139 Rectangle2D rect = new Rectangle2D.Double (center.getX() - 0.5 * width, 140 center.getY() - 0.5 * height, 141 width, height); 142 drawShape(rect, rectangle, currentColor); 143 } 144 145 148 public void render(Oval oval) { 149 Color currentColor = _graphics.getColor(); 150 GraphPoint center = oval.getCenter(); 151 double width = oval.getWidth(); 152 double height = oval.getHeight(); 153 Ellipse2D ellipse = new Ellipse2D.Double (center.getX() - 0.5 * width, 154 center.getY() - 0.5 * height, 155 width, height); 156 drawShape(ellipse, oval, currentColor); 157 } 158 159 private void drawShape(Shape shape, BasicGraphicalElement element, 160 Color backupColor) { 161 GraphicAttributes attributes = element.getGraphicAttributes(); 162 Color fillColor = null; 163 if (element.isClosed() && attributes instanceof FillAttributes) { 164 fillColor = ((FillAttributes) attributes).getFillColor(); 165 } 166 if (fillColor != null) { 167 _graphics.setColor(fillColor); 168 _graphics.fill(shape); 169 } 170 Color lineColor = _defaultColor; 171 if (attributes instanceof LineAttributes) { 172 LineAttributes la = (LineAttributes) attributes; 173 BasicStroke stroke = new BasicStroke ((float) la.getLineThickness()); 174 double[] linePattern = la.getLinePattern(); 175 if (linePattern != null) { 176 float[] dash = new float[linePattern.length]; 177 for (int i = 0; i < dash.length; i++) { 178 dash[i] = (float) la.getLinePattern()[i]; 179 } 180 stroke = new BasicStroke (stroke.getLineWidth(), BasicStroke.CAP_BUTT, 181 BasicStroke.JOIN_MITER, 10f, dash, 0f); 182 } 183 _graphics.setStroke(stroke); 184 if (la.getLineColor() != null || fillColor != null) { 185 lineColor = la.getLineColor(); 186 } 187 } 188 if (lineColor != null) { 189 _graphics.setColor(lineColor); 190 _graphics.draw(shape); 191 } 192 _graphics.setColor(backupColor); 193 } 194 195 206 public void render(Text text) { 207 final GraphicAttributes ga = text.getGraphicAttributes(); 208 if (ga instanceof TextAttributes) { 209 final TextAttributes ta = (TextAttributes) ga; 210 final Color currentColor = _graphics.getColor(); 211 Color fontColor = ta.getTextColor(); 212 if (fontColor == null) { 213 fontColor = _defaultColor; 214 } 215 _graphics.setColor(fontColor); 216 217 final double scale = _graphics.getTransform().getScaleX(); 218 final String str = text.getText(); 219 final TextLayout layout 220 = new TextLayout (str.length() == 0 ? " " : str, GraphicsRenderer.createFont(ta, 0), 222 _graphics.getFontRenderContext()); 223 double fs = ta.getFontSize(); 224 fs = fs == 0 ? 1 / scale : fs / DEFAULT_FONT_SIZE; 225 final Shape ts 226 = layout.getOutline(new AffineTransform (fs, 0, 0, -fs, 0, 0)); 227 final double x = -0.5 * ta.getHorizontalAnchor().getFactor() 228 * ts.getBounds2D().getWidth(); 229 final double y = -0.5 * ta.getVerticalAnchor().getFactor() 230 * ts.getBounds2D().getHeight(); 231 final AffineTransform transformation 232 = AffineTransform.getTranslateInstance(text.getPosition().getX(), 233 text.getPosition().getY()); 234 transformation.rotate(ta.getOrientationAngle() * Math.PI / 180); 235 transformation.translate(x, y); 236 _graphics.fill(transformation.createTransformedShape(ts)); 237 _graphics.setColor(currentColor); 238 } 239 } 240 } 241
| Popular Tags
|