Intersection.java :  » Swing » java2d » Java-2D-Examples » demos » Clipping » Java Open Source

Java Open Source
1. Apache Common
2. Aspect Oriented
3. Barcode
4. Byte Code
5. Cache
6. Chart
7. Collections Libraries
8. Content Management Systems CMS
9. Crawlers
10. CSV File
11. Database Connection Pools
12. Database Engines
13. Database JDBC Drivers
14. Database Persistence Frameworks
15. Development
16. Development Build Systems
17. Development Code Analyzers
18. Development Code Beautifiers Formatters
19. Development Code Coverage Tools
20. Development IDEs
21. Development Java Source Obfuscators
22. Development JavaDoc
23. Development Text Editor
24. Email Clients
25. Enterprise Service Bus
26. Game Graphic
27. HTML Parsers
28. Installers
29. Inversion of Control Container
30. Issue Tracking
31. J2EE EJB Servers
32. J2EE Frameworks
33. JMS
34. JMX Tools
35. Job Schedulers
36. Logging Tools
37. Network Clients
38. Network Clients Chat
39. Network Servers
40. Open Source System Bloggers
41. Open Source System Financial ERP CRM
42. Open Source System Forum
43. Open Source System Groupware
44. Parser Generators
45. PDF Libraries
46. Portals
47. Profilers
48. Project Management
49. Report
50. RSS RDF
51. Rule Engines
52. Science Library
53. Scripting Languages
54. Search Engines
55. Security
56. Source Control Tools
57. SQL Clients Interface
58. Swing
59. Template Engines
60. Testing Tools
61. UML Modeling
62. Web Development JSP Tag Libraries
63. Web Frameword Ajax
64. Web Frameworks
65. Web Mail Clients
66. Web Servers
67. Web Services Tools
68. Web Testing Tools
69. Wiki Engines
70. Windows Parm
71. Workflow Engines
72. XML
73. XML UI Toolkits
74. Zip Tar
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Open Source » Swing » java2d 
Intersection.java :  » Swing » java2d » Java-2D-Examples » demos » Clipping » Java Open Source
/*
 * @(#)Intersection.java  1.32 06/08/29
 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @(#)Intersection.java  1.32 06/08/29
 */

package java2d.demos.Clipping;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import javax.swing.*;
import java2d.AnimatingControlsSurface;
import java2d.CustomControls;

import static java.awt.Color.*;



/**
 * Animated intersection clipping of lines, an image and a textured rectangle.
 */
public class Intersection extends AnimatingControlsSurface {

    private static final int HEIGHT_DECREASE = 0;
    private static final int HEIGHT_INCREASE = 1;
    private static final int  WIDTH_DECREASE = 2;
    private static final int  WIDTH_INCREASE = 3;

    private int xx, yy, ww, hh;
    private int direction = HEIGHT_DECREASE;
    private int angdeg;
    private Shape textshape;
    private double sw, sh;
    private GeneralPath ovals;
    private Rectangle2D rectshape;
    private DemoControls controls;
    protected boolean doIntersection = true;
    protected boolean doOvals = true;
    protected boolean doText;
    protected boolean threeSixty;


    public Intersection() {
        setBackground(WHITE);
        setControls(new Component[] { new DemoControls(this) });
    }


    public void reset(int w, int h) {
        xx = yy = 0;
        ww = w-1; hh = h;
        direction = HEIGHT_DECREASE;
        angdeg = 0;
        FontRenderContext frc = new FontRenderContext(null, true, false);
        Font f = new Font("serif",Font.BOLD,32);
        TextLayout tl = new TextLayout("J2D", f, frc);
        sw = tl.getBounds().getWidth();
        sh = tl.getBounds().getHeight();
        int size = Math.min(w, h);
        double sx = (size-40)/sw;
        double sy = (size-100)/sh;
        AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
        textshape = tl.getOutline(Tx);
        rectshape = textshape.getBounds();
        sw = rectshape.getWidth();
        sh = rectshape.getHeight();
        ovals = new GeneralPath();
        ovals.append(new Ellipse2D.Double(  10,   102020)false);
        ovals.append(new Ellipse2D.Double(w-30,   102020)false);
        ovals.append(new Ellipse2D.Double(  10, h-302020)false);
        ovals.append(new Ellipse2D.Double(w-30, h-302020)false);
    }


    public void step(int w, int h) {
        if (direction == HEIGHT_DECREASE) {
            yy+=2; hh-=4;
            if (yy >= h/2) {
                direction = HEIGHT_INCREASE;
            }
        else if (direction == HEIGHT_INCREASE) {
            yy-=2; hh+=4;
            if (yy <= 0) {
                direction = WIDTH_DECREASE;
                hh = h-1; yy = 0;
            }
        }
        if (direction == WIDTH_DECREASE) {
            xx+=2; ww-=4;
            if (xx >= w/2) {
                direction = WIDTH_INCREASE;
            }
        else if (direction == WIDTH_INCREASE) {
            xx-=2; ww+=4;
            if (xx <= 0) {
                direction = HEIGHT_DECREASE;
                ww = w-1; xx = 0;
            }
        }
        if ((angdeg += 5== 360) { 
            angdeg = 0;
            threeSixty = true;
        }
    }


    public void render(int w, int h, Graphics2D g2) {

        Rectangle rect = new Rectangle(xx, yy, ww, hh);
       
        AffineTransform Tx = new AffineTransform();
        Tx.rotate(Math.toRadians(angdeg),w/2,h/2);
        Tx.translate(w/2-sw/2, sh+(h-sh)/2);

        GeneralPath path = new GeneralPath();
        if (doOvals) {
            path.append(ovals, false);
        
        if (doText) {
            path.append(Tx.createTransformedShape(textshape)false);
        else {
            path.append(Tx.createTransformedShape(rectshape)false);
        }

        if (doIntersection) {
            g2.clip(rect);
            g2.clip(path);
        }

        g2.setColor(GREEN);
        g2.fill(rect);

        g2.setClip(new Rectangle(00, w, h));

        g2.setColor(LIGHT_GRAY);
        g2.draw(rect);
        g2.setColor(BLACK);
        g2.draw(path);
    }


    public static void main(String argv[]) {
        createDemoFrame(new Intersection());
    }


    static class DemoControls extends CustomControls implements ActionListener {

        Intersection demo;
        JToolBar toolbar;

        public DemoControls(Intersection demo) {
            super(demo.name);
            this.demo = demo;
            add(toolbar = new JToolBar());
            toolbar.setFloatable(false);
            addTool("Intersect"true );
            addTool("Text",      false);
            addTool("Ovals",     true );
        }


        public void addTool(String str, boolean state) {
            JToggleButton b = (JToggleButtontoolbar.add(new JToggleButton(str));
            b.setFocusPainted(false);
            b.setSelected(state);
            b.addActionListener(this);
            int width = b.getPreferredSize().width;
            Dimension prefSize = new Dimension(width, 21);
            b.setPreferredSize(prefSize);
            b.setMaximumSize(  prefSize);
            b.setMinimumSize(  prefSize);
        }


        public void actionPerformed(ActionEvent e) {
            JToggleButton b = (JToggleButtone.getSource();
            if (b.getText().equals("Intersect")) {
                demo.doIntersection = b.isSelected();
            else if (b.getText().equals("Ovals")) {
                demo.doOvals = b.isSelected();
            else if (b.getText().equals("Text")) {
                demo.doText = b.isSelected();
            }
            if (demo.animating.thread == null) {
                demo.repaint();
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(200,40);
        }


        public void run() {
            Thread me = Thread.currentThread();
            while (thread == me) {
                if (demo.threeSixty) {
                    ((AbstractButtontoolbar.getComponentAtIndex(1)).doClick();
                    demo.threeSixty = false;
                }
                try {
                    thread.sleep(500);
                catch (InterruptedException e) { return}
            }
            thread = null;
        }
    // End DemoControls
// End Intersection
 Clipping
 Mix
Name Size Time
Areas.java8 k   2007-08-08
ClipAnim.java10 k   2007-08-08
Intersection.java9 k   2007-08-08
Text.java8 k   2007-08-08
w_ww._j__a__v__a___2__s_.c___o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.