1 /* 2 * 3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package java2d.demos.Lines; 33 34 35 import static java.awt.Color.BLACK; 36 import static java.awt.Color.WHITE; 37 import java.awt.BasicStroke; 38 import java.awt.Font; 39 import java.awt.Graphics2D; 40 import java.awt.Shape; 41 import java.awt.font.FontRenderContext; 42 import java.awt.font.TextLayout; 43 import java.awt.geom.Arc2D; 44 import java.awt.geom.CubicCurve2D; 45 import java.awt.geom.Ellipse2D; 46 import java.awt.geom.QuadCurve2D; 47 import java.awt.geom.Rectangle2D; 48 import java.awt.geom.RoundRectangle2D; 49 import java2d.Surface; 50 51 52 /** 53 * Various shapes stroked with a dashing pattern. 54 */ 55 @SuppressWarnings("serial") 56 public class Dash extends Surface { 57 58 public Dash() { 59 setBackground(WHITE); 60 } 61 62 @Override 63 public void render(int w, int h, Graphics2D g2) { 64 FontRenderContext frc = g2.getFontRenderContext(); 65 Font font = g2.getFont(); 66 TextLayout tl = new TextLayout("Dashes", font, frc); 67 float sw = (float) tl.getBounds().getWidth(); 68 float sh = tl.getAscent() + tl.getDescent(); 69 g2.setColor(BLACK); 70 tl.draw(g2, (w / 2 - sw / 2), sh + 5); 71 72 BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND, 73 BasicStroke.JOIN_ROUND, 0, new float[] { 0, 6, 0, 6 }, 0); 74 g2.setStroke(dotted); 75 g2.drawRect(3, 3, w - 6, h - 6); 76 77 int x = 0; 78 int y = h - 34; 79 BasicStroke bs[] = new BasicStroke[6]; 80 81 float j = 1.1f; 82 for (int i = 0; i < bs.length; i++, j += 1.0f) { 83 float dash[] = { j }; 84 BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, 85 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f); 86 g2.setStroke(b); 87 g2.drawLine(20, y, w - 20, y); 88 bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, 89 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f); 90 y += 5; 91 } 92 93 Shape shape = null; 94 y = 0; 95 for (int i = 0; i < 6; i++) { 96 x = (i == 0 || i == 3) ? (w / 3 - w / 5) / 2 : x + w / 3; 97 y = (i <= 2) ? (int) sh + h / 12 : h / 2; 98 99 g2.setStroke(bs[i]); 100 g2.translate(x, y); 101 switch (i) { 102 case 0: 103 shape = new Arc2D.Float(0.0f, 0.0f, w / 5, h / 4, 45, 270, 104 Arc2D.PIE); 105 break; 106 case 1: 107 shape = new Ellipse2D.Float(0.0f, 0.0f, w / 5, h / 4); 108 break; 109 case 2: 110 shape = new RoundRectangle2D.Float(0.0f, 0.0f, w / 5, h / 4, 111 10.0f, 10.0f); 112 break; 113 case 3: 114 shape = new Rectangle2D.Float(0.0f, 0.0f, w / 5, h / 4); 115 break; 116 case 4: 117 shape = new QuadCurve2D.Float(0.0f, 0.0f, w / 10, h / 2, w 118 / 5, 0.0f); 119 break; 120 case 5: 121 shape = new CubicCurve2D.Float(0.0f, 0.0f, w / 15, h / 2, w 122 / 10, h / 4, w / 5, 0.0f); 123 break; 124 } 125 126 g2.draw(shape); 127 g2.translate(-x, -y); 128 } 129 } 130 131 public static void main(String argv[]) { 132 createDemoFrame(new Dash()); 133 } 134 }