Dash.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 5k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)Dash.java 1.19 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Lines;
  31. import java.awt.*;
  32. import java.awt.geom.*;
  33. import java.awt.event.*;
  34. import java.awt.font.TextLayout;
  35. import java.awt.font.FontRenderContext;
  36. import DemoSurface;
  37. import DemoPanel;
  38. /**
  39.  * Various shapes stroked with a dashing pattern.
  40.  */
  41. public class Dash extends DemoSurface {
  42.     public Dash() {
  43.         setBackground(Color.white);
  44.     }
  45.     public void drawDemo(int w, int h, Graphics2D g2) {
  46.         FontRenderContext frc = g2.getFontRenderContext();
  47.         Font font = g2.getFont();
  48.         TextLayout tl = new TextLayout("Dashes", font, frc);
  49.         float sw = (float) tl.getBounds().getWidth();
  50.         float sh = (float) tl.getAscent() + tl.getDescent();
  51.         g2.setColor(Color.black);
  52.         tl.draw(g2, (float) (w/2-sw/2), sh+5);
  53.         BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND, 
  54.                      BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0);
  55.         g2.setStroke(dotted);
  56.         g2.drawRect(3,3,w-6,h-6);
  57.         int x = 0; int y = h-34;
  58.         BasicStroke bs[] = new BasicStroke[6];
  59.         float j = 1.1f;
  60.         for (int i = 0; i < bs.length; i++, j += 1.0f) {
  61.             float dash[] = { j };
  62.             BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, 
  63.                                 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  64.             g2.setStroke(b);
  65.             g2.drawLine(20, y, w-20, y);
  66.             bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, 
  67.                                 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  68.             y += 5;
  69.         }
  70.         Shape shape = null;
  71.         y = 0;
  72.         for (int i = 0; i < 6; i++) {
  73.             x = (i == 0 || i == 3) ? (w/3-w/5)/2 : x + w/3;
  74.             y = (i <= 2) ? (int) sh+h/12 : h/2;
  75.             g2.setStroke(bs[i]);
  76.             g2.translate(x, y);  
  77.             switch (i) {
  78.                 case 0 : shape = new Arc2D.Float(0.0f, 0.0f, w/5, h/4, 45, 270, Arc2D.PIE);
  79.                          break;
  80.                 case 1 : shape = new Ellipse2D.Float(0.0f, 0.0f, w/5, h/4);
  81.                          break;
  82.                 case 2 : shape = new RoundRectangle2D.Float(0.0f, 0.0f, w/5, h/4, 10.0f, 10.0f);
  83.                          break;
  84.                 case 3 : shape = new Rectangle2D.Float(0.0f, 0.0f, w/5, h/4);
  85.                          break;
  86.                 case 4 : shape = new QuadCurve2D.Float(0.0f,0.0f,w/10, h/2,w/5,0.0f);
  87.                          break;
  88.                 case 5 : shape = new CubicCurve2D.Float(0.0f,0.0f,w/15,h/2, w/10,h/4,w/5,0.0f);
  89.                          break;
  90.             }
  91.             g2.draw(shape);
  92.             g2.translate(-x, -y);
  93.         }
  94.     }
  95.     public static void main(String argv[]) {
  96.         Frame f = new Frame("Java2D Demo - Dash");
  97.         f.addWindowListener(new WindowAdapter() {
  98.             public void windowClosing(WindowEvent e) {System.exit(0);}
  99.         });
  100.         f.add("Center", new DemoPanel(new Dash()));
  101.         f.pack();
  102.         f.setSize(new Dimension(400,300));
  103.         f.show();
  104.     }
  105. }