1 31 32 package org.antlr.works.visualization.graphics; 33 34 import org.antlr.xjlib.appkit.gview.base.Vector2D; 35 import org.antlr.xjlib.appkit.gview.shape.SLinkArc; 36 37 import java.awt.*; 38 import java.awt.font.TextLayout ; 39 import java.awt.geom.QuadCurve2D ; 40 41 public class GEngineGraphics extends GEngine { 42 43 SLinkArc link_arc = new SLinkArc(); 44 45 public GEngineGraphics() { 46 } 47 48 public Graphics2D getG2D() { 49 return context.getGraphics2D(); 50 } 51 52 public void setColor(Color color) { 53 getG2D().setColor(color); 54 } 55 56 public Color getColor() { 57 return getG2D().getColor(); 58 } 59 60 public void setLineWidth(float width) { 61 getG2D().setStroke(new BasicStroke(width)); 62 } 63 64 public float getStringPixelWidth(Font font, String s) { 65 getG2D().setFont(font); 66 TextLayout layout = new TextLayout (s, getG2D().getFont(), getG2D().getFontRenderContext()); 67 return (float)layout.getBounds().getWidth(); 68 } 69 70 public void drawLine(float x0, float y0, float x1, float y1) { 71 getG2D().drawLine((int)x0, (int)y0, (int)x1, (int)y1); 72 } 73 74 public void drawArc(float x, float y, float w, float h, int a0, int a1) { 75 getG2D().drawArc((int)x, (int)y, (int)w, (int)h, a0, a1); 76 } 77 78 public void drawCircle(float x, float y, float r) { 79 getG2D().drawArc((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r), 0, 360); 80 } 81 82 public void drawRect(float x, float y, float dx, float dy) { 83 getG2D().drawRect((int)x, (int)y, (int)dx, (int)dy); 84 } 85 86 public void drawRoundRect(float x, float y, float dx, float dy, float arc_dx, float arc_dy) { 87 getG2D().drawRoundRect((int)x, (int)y, (int)dx, (int)dy, (int)arc_dx, (int)arc_dy); 88 } 89 90 public void drawOval(float x, float y, float dx, float dy) { 91 getG2D().drawOval((int)x, (int)y, (int)dx, (int)dy); 92 } 93 94 public void fillRect(float x, float y, float dx, float dy) { 95 getG2D().fillRect((int)x, (int)y, (int)dx, (int)dy); 96 } 97 98 public void fillOval(float x, float y, float dx, float dy) { 99 getG2D().fillOval((int)x, (int)y, (int)dx, (int)dy); 100 } 101 102 public void fillCircle(float x, float y, float r) { 103 getG2D().fillArc((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r), 0, 360); 104 } 105 106 public void drawRightArrow(float ox, float oy, float w, float h) { 107 int[] x = new int[] { (int)(ox-w), (int)ox, (int)(ox-w) }; 108 int[] y = new int[] { (int)(oy-h), (int)oy, (int)(oy+h) }; 109 110 getG2D().drawPolygon(x, y, 3); 111 getG2D().fillPolygon(x, y, 3); 112 } 113 114 public void drawUpArrow(float ox, float oy, float w, float h) { 115 int[] x = new int[] { (int)(ox-h), (int)ox, (int)(ox+h) }; 116 int[] y = new int[] { (int)(oy+w), (int)oy, (int)(oy+w) }; 117 118 getG2D().drawPolygon(x, y, 3); 119 getG2D().fillPolygon(x, y, 3); 120 } 121 122 public void drawDownArrow(float ox, float oy, float w, float h) { 123 int[] x = new int[] { (int)(ox-h), (int)ox, (int)(ox+h) }; 124 int[] y = new int[] { (int)(oy-w), (int)oy, (int)(oy-w) }; 125 126 getG2D().drawPolygon(x, y, 3); 127 getG2D().fillPolygon(x, y, 3); 128 } 129 130 public void drawString(Font font, String s, float x, float y, int align) { 131 getG2D().setFont(font); 132 TextLayout layout = new TextLayout (s, font, getG2D().getFontRenderContext()); 133 float tx = Float.MIN_VALUE; 134 float ty = Float.MIN_VALUE; 135 switch(align) { 136 case GContext.ALIGN_CENTER: 137 tx = (float)(x-layout.getBounds().getWidth()*0.5); 138 ty = (float)(y+layout.getBounds().getHeight()*0.5); 139 break; 140 case GContext.ALIGN_CENTER_UP: 141 tx = (float)(x-layout.getBounds().getWidth()*0.5); 142 ty = y; 143 break; 144 case GContext.ALIGN_RIGHT: 145 tx = (float)(x-layout.getBounds().getWidth()); 146 ty = (float)(y+layout.getBounds().getHeight()*0.5); 147 break; 148 } 149 layout.draw(getG2D(), tx, ty-1); 150 } 151 152 public void drawSpline(float x0, float y0, float x1, float y1, float startOffset, float endOffset, float flateness, boolean arrow) { 153 link_arc.setStart(x0, y0); 154 link_arc.setEnd(x1, y1); 155 link_arc.setStartTangentOffset(startOffset); 156 link_arc.setEndTangentOffset(endOffset); 157 link_arc.setFlateness(flateness); 158 link_arc.setArrowVisible(arrow); 159 link_arc.update(); 160 link_arc.setColor(getG2D().getColor()); 161 link_arc.draw(getG2D()); 162 } 163 164 public void drawArcConnector(float x0, float y0, float x1, float y1, 165 float start_offset, float end_offset, 166 float ctrl_offset, float arc_offset, 167 boolean arrow) 168 { 169 if(x0>x1) { 170 ctrl_offset *= -1; 171 arc_offset *= -1; 172 } 173 174 float cx = x1-ctrl_offset; 175 float ax0 = cx-arc_offset; 176 177 Vector2D a = new Vector2D(cx, y0); 178 Vector2D b = new Vector2D(x1, y1); 179 Vector2D z = a.add(b.sub(a).setLength(Math.abs(arc_offset))); 180 181 drawLine(x0+start_offset, y0, ax0, y0); 182 drawSpline((float)z.x, (float)z.y, x1, y1, 0, end_offset, 0, arrow); 183 184 QuadCurve2D.Float quad = new QuadCurve2D.Float (); 185 quad.setCurve(ax0, y0, cx, y0, (float)z.x, (float)z.y); 186 getG2D().draw(quad); 187 } 188 189 190 } 191 | Popular Tags |