/* * @(#)Intersection.java 1.18 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Clipping; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import J2DCanvas; /** * Animated intersection clipping of lines, an image and a textured rectangle. */ public class Intersection extends J2DCanvas { private Image img; private int rectW, rectH, x1, y1, x2, y2; private final int IN = 0; private final int OUT = 1; private int direction = IN; private int fillType = 0; private TexturePaint tp; private TextLayout tl = new TextLayout("Intersection Clipping", font, new FontRenderContext(null, false, false)); public Intersection() { setBackground(Color.white); img = getImage("images/clouds.jpg"); Font f = new Font("Dialog", Font.ITALIC, 12); TextLayout tl = new TextLayout("Java2D", f, new FontRenderContext(null, false, false)); int sw = (int) tl.getBounds().getWidth(); int sh = (int) (tl.getAscent() + tl.getDescent()); BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.setBackground(Color.gray); big.setColor(Color.cyan); tl.draw(big, 0, (float) tl.getAscent()); Rectangle r = new Rectangle(0,0,sw,sh); tp = new TexturePaint(bi, r); runnable = true; } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { rectW = w/3; rectH = h/3; x1 = w/6; y1 = h/6; x2 = x1+rectW; y2 = y1+rectH; } g2.setColor(Color.black); tl.draw(g2, (int) (w/2-tl.getBounds().getWidth()/2), (int) tl.getBounds().getHeight()); if (direction == IN) { ++x1; ++y1; --x2; --y2; if (x1 >= x2 || y1 >= y2) direction = OUT; } if (direction == OUT) { --x1; --y1; ++x2; ++y2; if (x1 <= w/6) { direction = IN; fillType++; } } Rectangle rect1 = new Rectangle(x1, y1, rectW, rectH); Rectangle rect2 = new Rectangle(x2, y2, rectW, rectH); g2.setClip(rect1); g2.clip(rect2); switch (fillType%3) { case 0 : g2.setColor(Color.gray); g2.fill(rect1); g2.setColor(Color.yellow); for (int j = y1; j < y1+rectH; j+=5) g2.drawLine(0,j,w,j); break; case 1 : g2.drawImage(img, x1, y1, rectW, rectH, this); break; case 2 : g2.setPaint(tp); g2.fill(rect1); } g2.setClip(new Rectangle(0,0,w,h)); g2.setColor(Color.black); g2.draw(rect1); g2.draw(rect2); } public static void main(String s[]) { final Intersection demo = new Intersection(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - Intersection"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }