FileDocCategorySizeDatePackage
Intersection.javaAPI DocExample7686Wed Aug 08 15:13:48 BST 2001demos.Clipping

Intersection

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

Fields Summary
private static final int
HEIGHTDECREASE
private static final int
HEIGHTINCREASE
private static final int
WIDTHDECREASE
private static final int
WIDTHINCREASE
private int
xx
private int
yy
private int
ww
private int
hh
private int
direction
private int
angdeg
private Shape
textshape
private double
sw
private double
sh
private GeneralPath
ovals
private Rectangle2D
rectshape
private DemoControls
controls
protected boolean
doIntersection
protected boolean
doOvals
protected boolean
doText
protected boolean
threeSixty
Constructors Summary
public Intersection()



      
        setBackground(Color.white);
        setControls(new Component[] { new DemoControls(this) });
    
Methods Summary
public static voidmain(java.lang.String[] argv)

        createDemoFrame(new Intersection());
    
public voidrender(int w, int h, java.awt.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(Color.green);
        g2.fill(rect);

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

        g2.setColor(Color.lightGray);
        g2.draw(rect);
        g2.setColor(Color.black);
        g2.draw(path);
    
public voidreset(int w, int h)

        xx = yy = 0;
        ww = w-1; hh = h;
        direction = HEIGHTDECREASE;
        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, 10, 20, 20), false);
        ovals.append(new Ellipse2D.Double(w-30, 10, 20, 20), false);
        ovals.append(new Ellipse2D.Double(10, h-30, 20, 20), false);
        ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false);
    
public voidstep(int w, int h)

        if (direction == HEIGHTDECREASE) {
            yy+=2; hh-=4;
            if (yy >= h/2) {
                direction = HEIGHTINCREASE;
            }
        } else if (direction == HEIGHTINCREASE) {
            yy-=2; hh+=4;
            if (yy <= 0) {
                direction = WIDTHDECREASE;
                hh = h-1; yy = 0;
            }
        }
        if (direction == WIDTHDECREASE) {
            xx+=2; ww-=4;
            if (xx >= w/2) {
                direction = WIDTHINCREASE;
            }
        } else if (direction == WIDTHINCREASE) {
            xx-=2; ww+=4;
            if (xx <= 0) {
                direction = HEIGHTDECREASE;
                ww = w-1; xx = 0;
            }
        }
        if ((angdeg += 5) == 360) { 
            angdeg = 0;
            threeSixty = true;
        }