FileDocCategorySizeDatePackage
FadeAnim.javaAPI DocExample16327Wed Aug 08 15:13:48 BST 2001demos.Composite

FadeAnim

public class FadeAnim extends AnimatingControlsSurface
Animation of compositing shapes, text and images fading in and out.

Fields Summary
private static TexturePaint
texture
private static BasicStroke
bs
private static Font[]
fonts
private static String[]
strings
private static String[]
imgs
private static Paint[]
paints
private Vector
vector
private int
numShapes
private int
numStrings
private int
numImages
Constructors Summary
public FadeAnim()



      
        setBackground(Color.black);
        setStrings(2);
        setImages(3);
        setShapes(8);
        setControls(new Component[] { new DemoControls(this) });
        setConstraints(new String[] { BorderLayout.EAST });
    
Methods Summary
public static voidmain(java.lang.String[] argv)

        createDemoFrame(new FadeAnim());
    
public voidrender(int w, int h, java.awt.Graphics2D g2)

        for (int i = 0; i < vector.size(); i++) {
            ObjectData od = (ObjectData) vector.get(i);
            AlphaComposite ac = AlphaComposite.getInstance(
                                   AlphaComposite.SRC_OVER, od.alpha);
            g2.setComposite(ac);
            g2.setPaint(od.paint);
            g2.translate(od.x, od.y);

            if (od.object instanceof Image) {
                g2.drawImage((Image) od.object, 0, 0, this);
            } else if (od.object instanceof TextData) {
                g2.setFont(((TextData) od.object).font);
                g2.drawString(((TextData) od.object).string, 0, 0);
            } else if (od.object instanceof QuadCurve2D 
                    || od.object instanceof CubicCurve2D) 
            {
                g2.setStroke(bs);
                g2.draw((Shape) od.object);
            } else if (od.object instanceof Shape) {
                g2.fill((Shape) od.object);
            }
            g2.translate(-od.x, -od.y);
        }
    
public voidreset(int w, int h)

        for (int i = 0; i < vector.size(); i++) {
            ((ObjectData) vector.get(i)).reset(w, h);
        }
    
public voidsetImages(int num)


        if (num < numImages) {
            Vector v = new Vector(vector.size());
            for (int i = 0; i < vector.size(); i++) {
                if (((ObjectData) vector.get(i)).object instanceof Image) {
                    v.addElement(vector.get(i));
                }
            }
            vector.removeAll(v);
            v.setSize(num);
            vector.addAll(v);
        } else {
            Dimension d = getSize();
            for (int i = numImages; i < num; i++) {
                Object obj = getImage(imgs[i % imgs.length]);
                if (imgs[i % imgs.length].equals("jumptojavastrip.png")) {
                    int iw = ((Image) obj).getWidth(null);
                    int ih = ((Image) obj).getHeight(null);
                    BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
                    bimg.createGraphics().drawImage((Image) obj, 0, 0, null);
                    obj = bimg;
                }
                ObjectData od = new ObjectData(obj, Color.black);
                od.reset(d.width, d.height);
                vector.addElement(od);
            }
        }
        numImages = num;
    
public voidsetShapes(int num)


        if (num < numShapes) {
            Vector v = new Vector(vector.size());
            for (int i = 0; i < vector.size(); i++) {
                if (((ObjectData) vector.get(i)).object instanceof Shape) {
                    v.addElement(vector.get(i));
                }
            }
            vector.removeAll(v);
            v.setSize(num);
            vector.addAll(v);
        } else {
            Dimension d = getSize();
            for (int i = numShapes; i < num; i++) {
                Object obj = null;
                switch (i % 7) {
                    case 0 : obj = new GeneralPath(); break;
                    case 1 : obj = new Rectangle2D.Double(); break;
                    case 2 : obj = new Ellipse2D.Double(); break;
                    case 3 : obj = new Arc2D.Double(); break;
                    case 4 : obj = new RoundRectangle2D.Double(); break;
                    case 5 : obj = new CubicCurve2D.Double(); break;
                    case 6 : obj = new QuadCurve2D.Double(); break;
                }
                ObjectData od = new ObjectData(obj, paints[i%paints.length]);
                od.reset(d.width, d.height);
                vector.addElement(od);
            }
        } 
        numShapes = num;
    
public voidsetStrings(int num)


        if (num < numStrings) {
            Vector v = new Vector(vector.size());
            for (int i = 0; i < vector.size(); i++) {
                if (((ObjectData) vector.get(i)).object instanceof TextData) {
                    v.addElement(vector.get(i));
                }
            }
            vector.removeAll(v);
            v.setSize(num);
            vector.addAll(v);
        } else {
            Dimension d = getSize();
            for (int i = numStrings; i < num; i++) {
                int j = i % fonts.length;
                int k = i % strings.length;
                Object obj = new TextData(strings[k], fonts[j], this); 
                ObjectData od = new ObjectData(obj, paints[i%paints.length]);
                od.reset(d.width, d.height);
                vector.addElement(od);
            }
        }
        numStrings = num;
    
public voidstep(int w, int h)

        for (int i = 0; i < vector.size(); i++) {
            ((ObjectData) vector.get(i)).step(w, h);
        }