home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Clipping / Areas.java next >
Encoding:
Java Source  |  2002-09-06  |  7.4 KB  |  217 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Areas.java    1.28 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Clipping;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.geom.Area;
  45. import java.awt.geom.Ellipse2D;
  46. import java.awt.geom.GeneralPath;
  47. import javax.swing.*;
  48. import java2d.ControlsSurface;
  49. import java2d.CustomControls;
  50.  
  51.  
  52. /**
  53.  * The Areas class demonstrates the CAG (Constructive Area Geometry) 
  54.  * operations: Add(union), Subtract, Intersect, and ExclusiveOR.
  55.  */
  56. public class Areas extends ControlsSurface {
  57.  
  58.     protected String areaType = "nop";
  59.  
  60.  
  61.     public Areas() {
  62.         setBackground(Color.white);
  63.         setControls(new Component[] { new DemoControls(this) });
  64.     }
  65.  
  66.  
  67.     public void render(int w, int h, Graphics2D g2) {
  68.         GeneralPath p1 = new GeneralPath();
  69.         p1.moveTo( w * .25f, 0.0f);
  70.         p1.lineTo( w * .75f, h * .5f);
  71.         p1.lineTo( w * .25f, (float) h);
  72.         p1.lineTo( 0.0f, h * .5f);
  73.         p1.closePath();
  74.  
  75.         GeneralPath p2 = new GeneralPath();
  76.         p2.moveTo( w * .75f, 0.0f);
  77.         p2.lineTo( (float) w, h * .5f);
  78.         p2.lineTo( w * .75f, (float) h);
  79.         p2.lineTo( w * .25f, h * .5f);
  80.         p2.closePath();
  81.  
  82.  
  83.         Area area = new Area(p1);
  84.         g2.setColor(Color.yellow);
  85.         if (areaType.equals("nop")) {
  86.             g2.fill(p1);
  87.             g2.fill(p2);
  88.             g2.setColor(Color.red);
  89.             g2.draw(p1);
  90.             g2.draw(p2);
  91.             return;
  92.         } else if (areaType.equals("add")) {
  93.         area.add(new Area(p2));
  94.         } else if (areaType.equals("sub")) {
  95.         area.subtract(new Area(p2));
  96.         } else if (areaType.equals("xor")) {
  97.         area.exclusiveOr(new Area(p2));
  98.         } else if (areaType.equals("int")) {
  99.         area.intersect(new Area(p2));
  100.         } else if (areaType.equals("pear")) {
  101.  
  102.             double sx = w/100;
  103.             double sy = h/140;
  104.             g2.scale(sx, sy);
  105.             double x = w/sx/2;
  106.             double y = h/sy/2;
  107.  
  108.             // Creates the first leaf by filling the intersection of two Area 
  109.             // objects created from an ellipse.
  110.             Ellipse2D leaf = new Ellipse2D.Double(x-16, y-29, 15.0, 15.0);
  111.             Area leaf1 = new Area(leaf);
  112.             leaf.setFrame(x-14, y-47, 30.0, 30.0);
  113.             Area leaf2 = new Area(leaf); 
  114.             leaf1.intersect(leaf2);   
  115.             g2.setColor(Color.green);
  116.             g2.fill(leaf1);   
  117.  
  118.             // Creates the second leaf.
  119.             leaf.setFrame(x+1, y-29, 15.0, 15.0);
  120.             leaf1 = new Area(leaf);
  121.             leaf2.intersect(leaf1);
  122.             g2.fill(leaf2);
  123.  
  124.             // Creates the stem by filling the Area resulting from the 
  125.             // subtraction of two Area objects created from an ellipse.
  126.             Ellipse2D stem = new Ellipse2D.Double(x, y-42, 40.0, 40.0);
  127.             Area st1 = new Area(stem);
  128.             stem.setFrame(x+3, y-47, 50.0, 50.0);
  129.             st1.subtract(new Area(stem));
  130.             g2.setColor(Color.black);
  131.             g2.fill(st1);
  132.  
  133.             // Creates the pear itself by filling the Area resulting from the 
  134.             // union of two Area objects created by two different ellipses.
  135.             Ellipse2D circle = new Ellipse2D.Double(x-25, y, 50.0, 50.0);
  136.             Ellipse2D oval = new Ellipse2D.Double(x-19, y-20, 40.0, 70.0);
  137.             Area circ = new Area(circle);
  138.             circ.add(new Area(oval));
  139.  
  140.             g2.setColor(Color.yellow);
  141.             g2.fill(circ);
  142.             return;
  143.         }
  144.         
  145.         g2.fill(area);
  146.         g2.setColor(Color.red);
  147.         g2.draw(area);
  148.     }
  149.  
  150.  
  151.     public static void main(String argv[]) {
  152.         createDemoFrame(new Areas());
  153.     }
  154.  
  155.  
  156.  
  157.     static class DemoControls extends CustomControls implements ActionListener {
  158.  
  159.         Areas demo;
  160.         JToolBar toolbar;
  161.         JComboBox combo;
  162.  
  163.         public DemoControls(Areas demo) {
  164.             super(demo.name);
  165.             this.demo = demo;
  166.             setBackground(Color.gray);
  167.             add(toolbar = new JToolBar());
  168.             toolbar.setFloatable(false);
  169.             addTool("nop", "no area operation", true);
  170.             addTool("add", "add", false);
  171.             addTool("sub", "subtract", false);
  172.             addTool("xor", "exclusiveOr", false);
  173.             addTool("int", "intersection", false);
  174.             addTool("pear", "pear", false);
  175.         }
  176.  
  177.  
  178.         public void addTool(String str, String tooltip, boolean state) {
  179.             JButton b = (JButton) toolbar.add(new JButton(str));
  180.             b.setBackground(state ? Color.green : Color.lightGray);
  181.             b.setToolTipText(tooltip);
  182.             b.setSelected(state);
  183.             b.addActionListener(this);
  184.         }
  185.  
  186.  
  187.         public void actionPerformed(ActionEvent e) {
  188.             for (int i = 0; i < toolbar.getComponentCount(); i++) {
  189.                 JButton b = (JButton) toolbar.getComponentAtIndex(i);
  190.                 b.setBackground(Color.lightGray);
  191.             }
  192.             JButton b = (JButton) e.getSource();
  193.             b.setBackground(Color.green);
  194.             demo.areaType = b.getText();
  195.             demo.repaint();
  196.         }
  197.  
  198.         public Dimension getPreferredSize() {
  199.             return new Dimension(200,37);
  200.         }
  201.  
  202.         public void run() {
  203.             try { thread.sleep(1111); } catch (Exception e) { return; }
  204.             Thread me = Thread.currentThread();
  205.             while (thread == me) {
  206.                 for (int i = 0; i < toolbar.getComponentCount(); i++) {
  207.                     ((JButton) toolbar.getComponentAtIndex(i)).doClick();
  208.                     try {
  209.                         thread.sleep(4444);
  210.                     } catch (InterruptedException e) { return; }
  211.                 }
  212.             }
  213.             thread = null;
  214.         }
  215.     } // End DemoControls
  216. } // End Areas
  217.