1 36 37 40 41 package java2d.demos.Lines; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.geom.*; 46 import java.awt.font.TextLayout ; 47 import java.awt.font.FontRenderContext ; 48 import java2d.Surface; 49 50 51 54 public class Dash extends Surface { 55 56 public Dash() { 57 setBackground(WHITE); 58 } 59 60 61 public void render(int w, int h, Graphics2D g2) { 62 FontRenderContext frc = g2.getFontRenderContext(); 63 Font font = g2.getFont(); 64 TextLayout tl = new TextLayout ("Dashes", font, frc); 65 float sw = (float) tl.getBounds().getWidth(); 66 float sh = (float) tl.getAscent() + tl.getDescent(); 67 g2.setColor(BLACK); 68 tl.draw(g2, (float) (w/2-sw/2), sh+5); 69 70 BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND, 71 BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0); 72 g2.setStroke(dotted); 73 g2.drawRect(3,3,w-6,h-6); 74 75 int x = 0; int y = h-34; 76 BasicStroke bs[] = new BasicStroke[6]; 77 78 float j = 1.1f; 79 for (int i = 0; i < bs.length; i++, j += 1.0f) { 80 float dash[] = { j }; 81 BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, 82 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f); 83 g2.setStroke(b); 84 g2.drawLine(20, y, w-20, y); 85 bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, 86 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f); 87 y += 5; 88 } 89 90 Shape shape = null; 91 y = 0; 92 for (int i = 0; i < 6; i++) { 93 x = (i == 0 || i == 3) ? (w/3-w/5)/2 : x + w/3; 94 y = (i <= 2) ? (int) sh+h/12 : h/2; 95 96 g2.setStroke(bs[i]); 97 g2.translate(x, y); 98 switch (i) { 99 case 0 : shape = new Arc2D.Float(0.0f, 0.0f, w/5, h/4, 45, 270, Arc2D.PIE); 100 break; 101 case 1 : shape = new Ellipse2D.Float(0.0f, 0.0f, w/5, h/4); 102 break; 103 case 2 : shape = new RoundRectangle2D.Float(0.0f, 0.0f, w/5, h/4, 10.0f, 10.0f); 104 break; 105 case 3 : shape = new Rectangle2D.Float(0.0f, 0.0f, w/5, h/4); 106 break; 107 case 4 : shape = new QuadCurve2D.Float(0.0f,0.0f,w/10, h/2,w/5,0.0f); 108 break; 109 case 5 : shape = new CubicCurve2D.Float(0.0f,0.0f,w/15,h/2, w/10,h/4,w/5,0.0f); 110 break; 111 } 112 113 g2.draw(shape); 114 g2.translate(-x, -y); 115 } 116 } 117 118 119 public static void main(String argv[]) { 120 createDemoFrame(new Dash()); 121 } 122 } 123 | Popular Tags |