/* * @(#)Dash.java 1.13 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Lines; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.font.TextLayout; import J2DCanvas; /** * Various shapes stroked with a dashing pattern. */ public class Dash extends J2DCanvas { public Dash() { setBackground(Color.white); } public void drawDemo(Graphics2D g2) { TextLayout tl = new TextLayout("Line Dashes", g2.getFont(), g2.getFontRenderContext()); float sw = (float) tl.getBounds().getWidth(); float sh = (float) tl.getAscent() + tl.getDescent(); g2.setColor(Color.black); tl.draw(g2, (float) (w/2-sw/2), sh); float dash1[] = { w / 50.0f }; BasicStroke bs = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); g2.setStroke(bs); Line2D line = new Line2D.Float(5.0f, h-20.0f, w-5.0f, h-20.0f); g2.draw(line); float dash2[] = { w / 25.0f }; bs = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f); g2.setStroke(bs); GeneralPath p = new GeneralPath(); p.moveTo(5, h-10); p.lineTo(w-5, h-10); g2.draw(p); g2.translate(-w/8, h/6); Shape shape = null; int x2 = 0; int y2 = 0; for (int i=0; i < 6; i++) { if (i == 3) { x2 = -w/4*3 + w/4; y2 = h/3; } else { x2 = w/4; y2 = 0; } g2.translate(x2, y2); switch (i) { case 0 : shape = new Arc2D.Float(0.0f, 0.0f, w/5, h/4, 45, 270, Arc2D.PIE); break; case 1 : shape = new Ellipse2D.Float(0.0f, 0.0f, w/5, h/4); break; case 2 : shape = new RoundRectangle2D.Float(0.0f, 0.0f, w/5, h/4, 10.0f, 10.0f); break; case 3 : shape = new Rectangle2D.Float(0.0f, 0.0f, w/5, h/4); break; case 4 : shape = new QuadCurve2D.Float(0.0f,0.0f,w/10, h/2,w/5,0.0f); break; case 5 : shape = new CubicCurve2D.Float(0.0f,0.0f,w/15,h/2, w/10,h/4,w/5,0.0f); break; } g2.draw(shape); } g2.setTransform(new AffineTransform()); } public static void main(String argv[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java2D Demo - Dash"); f.addWindowListener(l); f.add("Center", new Dash()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }