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

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)Caps.java 1.15 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.event.*;
  33. import java.awt.geom.Line2D;
  34. import java.awt.font.TextLayout;
  35. import java.awt.font.FontRenderContext;
  36. import DemoSurface;
  37. import DemoPanel;
  38. /**
  39.  * Shows the three different styles of stroke ending.
  40.  */
  41. public class Caps extends DemoSurface {
  42.     private static int cap[] = { BasicStroke.CAP_BUTT,
  43.         BasicStroke.CAP_ROUND, BasicStroke.CAP_SQUARE };
  44.     private static String desc[] = { "Butt Cap", "Round Cap", "Square Cap" };
  45.     public Caps() {
  46.         setBackground(Color.white);
  47.     }
  48.     public void drawDemo(int w, int h, Graphics2D g2) {
  49.         FontRenderContext frc = g2.getFontRenderContext();
  50.         Font font = g2.getFont();
  51.         g2.setColor(Color.black);
  52.         for (int i=0; i < 3; i++) {
  53.             g2.setStroke(new BasicStroke(15, cap[i], BasicStroke.JOIN_MITER));
  54.             g2.draw(new Line2D.Float(w/4,(i+1)*h/4,w-w/4,(i+1)*h/4));
  55.             TextLayout tl = new TextLayout(desc[i], font, frc);
  56.             tl.draw(g2,(float)(w/2-tl.getBounds().getWidth()/2),(i+1)*h/4-10);
  57.         }
  58.     }
  59.     public static void main(String s[]) {
  60.         Frame f = new Frame("Java2D Demo - Caps");
  61.         f.addWindowListener(new WindowAdapter() {
  62.             public void windowClosing(WindowEvent e) {System.exit(0);}
  63.         });
  64.         f.add("Center", new DemoPanel(new Caps()));
  65.         f.pack();
  66.         f.setSize(new Dimension(400,300));
  67.         f.show();
  68.     }
  69. }