home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Clipping / Intersection.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.7 KB  |  242 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.  * @(#)Intersection.java    1.24 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.*;
  45. import java.awt.font.FontRenderContext;
  46. import java.awt.font.TextLayout;
  47. import javax.swing.*;
  48. import java2d.AnimatingControlsSurface;
  49. import java2d.CustomControls;
  50.  
  51.  
  52.  
  53. /**
  54.  * Animated intersection clipping of lines, an image and a textured rectangle.
  55.  */
  56. public class Intersection extends AnimatingControlsSurface {
  57.  
  58.     private static final int HEIGHTDECREASE = 0;
  59.     private static final int HEIGHTINCREASE = 1;
  60.     private static final int WIDTHDECREASE = 2;
  61.     private static final int WIDTHINCREASE = 3;
  62.  
  63.     private int xx, yy, ww, hh;
  64.     private int direction = HEIGHTDECREASE;
  65.     private int angdeg;
  66.     private Shape textshape;
  67.     private double sw, sh;
  68.     private GeneralPath ovals;
  69.     private Rectangle2D rectshape;
  70.     private DemoControls controls;
  71.     protected boolean doIntersection = true;
  72.     protected boolean doOvals = true;
  73.     protected boolean doText;
  74.     protected boolean threeSixty;
  75.  
  76.  
  77.     public Intersection() {
  78.         setBackground(Color.white);
  79.         setControls(new Component[] { new DemoControls(this) });
  80.     }
  81.  
  82.  
  83.     public void reset(int w, int h) {
  84.         xx = yy = 0;
  85.         ww = w-1; hh = h;
  86.         direction = HEIGHTDECREASE;
  87.         angdeg = 0;
  88.         FontRenderContext frc = new FontRenderContext(null, true, false);
  89.         Font f = new Font("serif",Font.BOLD,32);
  90.         TextLayout tl = new TextLayout("J2D", f, frc);
  91.         sw = tl.getBounds().getWidth();
  92.         sh = tl.getBounds().getHeight();
  93.         int size = Math.min(w, h);
  94.         double sx = (size-40)/sw;
  95.         double sy = (size-100)/sh;
  96.         AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
  97.         textshape = tl.getOutline(Tx);
  98.         rectshape = textshape.getBounds();
  99.         sw = rectshape.getWidth();
  100.         sh = rectshape.getHeight();
  101.         ovals = new GeneralPath();
  102.         ovals.append(new Ellipse2D.Double(10, 10, 20, 20), false);
  103.         ovals.append(new Ellipse2D.Double(w-30, 10, 20, 20), false);
  104.         ovals.append(new Ellipse2D.Double(10, h-30, 20, 20), false);
  105.         ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false);
  106.     }
  107.  
  108.  
  109.     public void step(int w, int h) {
  110.         if (direction == HEIGHTDECREASE) {
  111.             yy+=2; hh-=4;
  112.             if (yy >= h/2) {
  113.                 direction = HEIGHTINCREASE;
  114.             }
  115.         } else if (direction == HEIGHTINCREASE) {
  116.             yy-=2; hh+=4;
  117.             if (yy <= 0) {
  118.                 direction = WIDTHDECREASE;
  119.                 hh = h-1; yy = 0;
  120.             }
  121.         }
  122.         if (direction == WIDTHDECREASE) {
  123.             xx+=2; ww-=4;
  124.             if (xx >= w/2) {
  125.                 direction = WIDTHINCREASE;
  126.             }
  127.         } else if (direction == WIDTHINCREASE) {
  128.             xx-=2; ww+=4;
  129.             if (xx <= 0) {
  130.                 direction = HEIGHTDECREASE;
  131.                 ww = w-1; xx = 0;
  132.             }
  133.         }
  134.         if ((angdeg += 5) == 360) { 
  135.             angdeg = 0;
  136.             threeSixty = true;
  137.         }
  138.     }
  139.  
  140.  
  141.     public void render(int w, int h, Graphics2D g2) {
  142.  
  143.         Rectangle rect = new Rectangle(xx, yy, ww, hh);
  144.        
  145.         AffineTransform Tx = new AffineTransform();
  146.         Tx.rotate(Math.toRadians(angdeg),w/2,h/2);
  147.         Tx.translate(w/2-sw/2, sh+(h-sh)/2);
  148.  
  149.         GeneralPath path = new GeneralPath();
  150.         if (doOvals) {
  151.             path.append(ovals, false);
  152.         } 
  153.         if (doText) {
  154.             path.append(Tx.createTransformedShape(textshape), false);
  155.         } else {
  156.             path.append(Tx.createTransformedShape(rectshape), false);
  157.         }
  158.  
  159.         if (doIntersection) {
  160.             g2.clip(rect);
  161.             g2.clip(path);
  162.         }
  163.  
  164.         g2.setColor(Color.green);
  165.         g2.fill(rect);
  166.  
  167.         g2.setClip(new Rectangle(0, 0, w, h));
  168.  
  169.         g2.setColor(Color.lightGray);
  170.         g2.draw(rect);
  171.         g2.setColor(Color.black);
  172.         g2.draw(path);
  173.     }
  174.  
  175.  
  176.     public static void main(String argv[]) {
  177.         createDemoFrame(new Intersection());
  178.     }
  179.  
  180.  
  181.     static class DemoControls extends CustomControls implements ActionListener {
  182.  
  183.         Intersection demo;
  184.         JToolBar toolbar;
  185.  
  186.         public DemoControls(Intersection demo) {
  187.             super(demo.name);
  188.             this.demo = demo;
  189.             setBackground(Color.gray);
  190.             add(toolbar = new JToolBar());
  191.             toolbar.setFloatable(false);
  192.             addTool("Intersect", true);
  193.             addTool("Text", false);
  194.             addTool("Ovals", true);
  195.         }
  196.  
  197.  
  198.         public void addTool(String str, boolean state) {
  199.             JButton b = (JButton) toolbar.add(new JButton(str));
  200.             b.setBackground(state ? Color.green : Color.lightGray);
  201.             b.setSelected(state);
  202.             b.addActionListener(this);
  203.         }
  204.  
  205.  
  206.         public void actionPerformed(ActionEvent e) {
  207.             JButton b = (JButton) e.getSource();
  208.             b.setSelected(!b.isSelected());
  209.             b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  210.             if (b.getText().equals("Intersect")) {
  211.                 demo.doIntersection = b.isSelected();
  212.             } else if (b.getText().equals("Ovals")) {
  213.                 demo.doOvals = b.isSelected();
  214.             } else if (b.getText().equals("Text")) {
  215.                 demo.doText = b.isSelected();
  216.             }
  217.             if (demo.animating.thread == null) {
  218.                 demo.repaint();
  219.             }
  220.         }
  221.  
  222.         public Dimension getPreferredSize() {
  223.             return new Dimension(200,37);
  224.         }
  225.  
  226.  
  227.         public void run() {
  228.             Thread me = Thread.currentThread();
  229.             while (thread == me) {
  230.                 if (demo.threeSixty) {
  231.                     ((JButton) toolbar.getComponentAtIndex(1)).doClick();
  232.                     demo.threeSixty = false;
  233.                 }
  234.                 try {
  235.                     thread.sleep(500);
  236.                 } catch (InterruptedException e) { return; }
  237.             }
  238.             thread = null;
  239.         }
  240.     } // End DemoControls
  241. } // End Intersection
  242.