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

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)WindingRule.java 1.16 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.Paths;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.GeneralPath;
  34. import DemoSurface;
  35. import DemoPanel;
  36. /**
  37.  * Rectangles filled to illustrate the GenerPath winding rule, determining
  38.  * the interior of a path.
  39.  */
  40. public class WindingRule extends DemoSurface {
  41.     public WindingRule() {
  42.         setBackground(Color.white);
  43.     }
  44.     public void drawDemo(int w, int h, Graphics2D g2) {
  45.         g2.translate(w*.2, h*.2);
  46.         GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  47.         p.moveTo(0.0f, 0.0f);
  48.         p.lineTo(w*.5f, 0.0f);
  49.         p.lineTo(w*.5f, h*.2f);
  50.         p.lineTo(0.0f, h*.2f);
  51.         p.closePath();
  52.         p.moveTo(w*.05f, h*.05f);
  53.         p.lineTo(w*.55f, h*.05f);
  54.         p.lineTo(w*.55f, h*.25f);
  55.         p.lineTo(w*.05f, h*.25f);
  56.         p.closePath();
  57.         g2.setColor(Color.lightGray);
  58.         g2.fill(p);
  59.         g2.setColor(Color.black);
  60.         g2.draw(p);
  61.         g2.drawString("NON_ZERO rule", 0, -5);
  62.         g2.translate(0.0f, h*.45);
  63.         p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
  64.         g2.setColor(Color.lightGray);
  65.         g2.fill(p);
  66.         g2.setColor(Color.black);
  67.         g2.draw(p);
  68.         g2.drawString("EVEN_ODD rule", 0, -5);
  69.     }
  70.     public static void main(String s[]) {
  71.         Frame f = new Frame("Java2D Demo - WindingRule");
  72.         f.addWindowListener(new WindowAdapter() {
  73.             public void windowClosing(WindowEvent e) {System.exit(0);}
  74.         });
  75.         f.add("Center", new DemoPanel(new WindingRule()));
  76.         f.pack();
  77.         f.setSize(new Dimension(400,300));
  78.         f.show();
  79.     }
  80. }