FileDocCategorySizeDatePackage
BezierScroller.javaAPI DocExample11802Wed Aug 08 15:13:48 BST 2001demos.Mix

BezierScroller

public class BezierScroller extends AnimatingControlsSurface
Animated Bezier Curve shape with images at the control points. README.txt file scrolling up. Composited Image fading in and out.

Fields Summary
private static String[]
appletStrs
private static final int
NUMPTS
private static Color
greenBlend
private static Font
font
private static Color
blueBlend
private static BasicStroke
bs
private static Image
hotj_img
private static BufferedImage
img
private static final int
UP
private static final int
DOWN
private float[]
animpts
private float[]
deltas
private BufferedReader
reader
private int
nStrs
private int
strH
private int
yy
private int
ix
private int
iy
private int
imgX
private Vector
vector
private Vector
appletVector
private float
alpha
private int
alphaDirection
protected boolean
doImage
protected boolean
doShape
protected boolean
doText
protected boolean
buttonToggle
Constructors Summary
public BezierScroller()



      
        setBackground(Color.white);
        doShape = doText = true;
        hotj_img = getImage("HotJava-16.gif");
        Image image = getImage("jumptojavastrip.png");
        int iw = image.getWidth(this);
        int ih = image.getHeight(this);
        img = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        img.createGraphics().drawImage(image, 0, 0, this);
        setControls(new Component[] { new DemoControls(this) });
    
Methods Summary
public voidanimate(float[] pts, float[] deltas, int index, int limit)

        float newpt = pts[index] + deltas[index];
        if (newpt <= 0) {
            newpt = -newpt;
            deltas[index] = (float) (Math.random() * 4.0 + 2.0);
        } else if (newpt >= (float) limit) {
            newpt = 2.0f * limit - newpt;
            deltas[index] = - (float) (Math.random() * 4.0 + 2.0);
        }
        pts[index] = newpt;
    
public voidgetFile()

        try {
            String fName = "README.txt";
            if ((reader = new BufferedReader(new FileReader(fName))) != null) {
                getLine();
            }
        } catch (Exception e) { reader = null; }
        if (reader == null) {
            appletVector = new Vector(100);
            for (int i = 0; i < 100; i++) {
                appletVector.addElement(appletStrs[i%appletStrs.length]);
            }
            getLine();
        }
        buttonToggle = true;
    
public java.lang.StringgetLine()

        String str = null;
        if (reader != null) {
            try {
                if ((str = reader.readLine()) != null) {
                    if (str.length() == 0) {
                        str = " ";
                    }
                    vector.addElement(str);
                }
            } catch (Exception e) { e.printStackTrace(); reader = null; }
        } else {
            if (appletVector.size() != 0) {
                vector.addElement(str = (String) appletVector.remove(0));
            }
        }
        return str;
    
public static voidmain(java.lang.String[] argv)

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


        if (doText) {
            g2.setColor(Color.lightGray);
            g2.setFont(font);
            float y = yy;
            for (int i = 0; i < vector.size(); i++) {
                g2.drawString((String)vector.get(i), 1, y += strH);
            }
        }

        if (doShape) {
            float[] ctrlpts = animpts;
            int len = ctrlpts.length;
            float prevx = ctrlpts[len - 2];
            float prevy = ctrlpts[len - 1];
            float curx = ctrlpts[0];
            float cury = ctrlpts[1];
            float midx = (curx + prevx) / 2.0f;
            float midy = (cury + prevy) / 2.0f;
            GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
            gp.moveTo(midx, midy);
            for (int i = 2; i <= ctrlpts.length; i += 2) {
                float x1 = (midx + curx) / 2.0f;
                float y1 = (midy + cury) / 2.0f;
                prevx = curx;
                prevy = cury;
                if (i < ctrlpts.length) {
                    curx = ctrlpts[i + 0];
                    cury = ctrlpts[i + 1];
                } else {
                    curx = ctrlpts[0];
                    cury = ctrlpts[1];
                }
                midx = (curx + prevx) / 2.0f;
                midy = (cury + prevy) / 2.0f;
                float x2 = (prevx + midx) / 2.0f;
                float y2 = (prevy + midy) / 2.0f;
                gp.curveTo(x1, y1, x2, y2, midx, midy);
            }
            gp.closePath();

            g2.setColor(blueBlend);
            g2.setStroke(bs);
            g2.draw(gp);
            g2.setColor(greenBlend);
            g2.fill(gp);

            PathIterator pi = gp.getPathIterator(null);
            float pts[] = new float[6];
            while ( !pi.isDone() ) {
                if (pi.currentSegment(pts) == pi.SEG_CUBICTO) {
                    g2.drawImage(hotj_img, (int) pts[0], (int) pts[1], this);
                }
                pi.next();
            }
        }

        if (doImage) {
            AlphaComposite ac = AlphaComposite.getInstance(
                                   AlphaComposite.SRC_OVER, alpha);
            g2.setComposite(ac);
            g2.drawImage(img.getSubimage(imgX,0,80,80), ix, iy, this);
        }
    
public voidreset(int w, int h)

        for (int i = 0; i < animpts.length; i += 2) {
            animpts[i + 0] = (float) (Math.random() * w);
            animpts[i + 1] = (float) (Math.random() * h);
            deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
            deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
            if (animpts[i + 0] > w / 2.0f) {
                deltas[i + 0] = -deltas[i + 0];
            }
            if (animpts[i + 1] > h / 2.0f) {
                deltas[i + 1] = -deltas[i + 1];
            }
        }
        FontMetrics fm = getFontMetrics(font);
        strH = fm.getAscent()+fm.getDescent();
        nStrs = h/strH+2;
        vector = new Vector(nStrs);
        ix = (int) (Math.random() * (w - 80));
        iy = (int) (Math.random() * (h - 80));
    
public voidstep(int w, int h)

        if (doText && vector.size() == 0) {
            getFile();
        }
        if (doText) {
            String s = getLine();
            if (s == null || vector.size() == nStrs && vector.size() != 0) {
                vector.removeElementAt(0);
            }
            yy = (s == null) ? 0 : h - vector.size() * strH;
        }
       
        for (int i = 0; i < animpts.length && doShape; i += 2) {
            animate(animpts, deltas, i + 0, w);
            animate(animpts, deltas, i + 1, h);
        }
        if (doImage && alphaDirection == UP) {
            if ((alpha += 0.025) > .99) {
                alphaDirection = DOWN;
                alpha = 1.0f;
            }
        } else if (doImage && alphaDirection == DOWN) {
            if ((alpha -= .02) < 0.01) {
                alphaDirection = UP;
                alpha = 0;
                ix = (int) (Math.random() * (w - 80));
                iy = (int) (Math.random() * (h - 80));
            }
        }
        if (doImage) {
            if ((imgX += 80) == 800) {
                imgX = 0;
            }
        }