/* * @(#)BezierAnim.java 1.17 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.Mix; import java.awt.*; import java.awt.event.*; import java.awt.geom.GeneralPath; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.util.Vector; import java.net.URL; import J2DCanvas; /** * Animated Bezier Curve with the README.txt file scrolling up. */ public class BezierAnim extends J2DCanvas { private static final int NUMPTS = 6; private float animpts[] = new float[NUMPTS * 2]; private float deltas[] = new float[NUMPTS * 2]; private float staticpts[] = { 50.0f, 0.0f, 150.0f, 0.0f, 200.0f, 75.0f, 150.0f, 150.0f, 50.0f, 150.0f, 0.0f, 75.0f, }; private BufferedReader reader; private int nStrs; private int strheight; private Vector vector; private Font fonts[] = { new Font("Times New Roman", Font.PLAIN, 12), new Font("Times New Roman", Font.ITALIC, 12), new Font("Times New Roman", Font.BOLD, 12) }; private int fn = 0; private BasicStroke solid = new BasicStroke(10.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND); private AlphaComposite ac = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.7f); public BezierAnim() { setBackground(Color.white); runnable = true; } public void animate(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 void getFile() { String srcFile = "../../README.txt"; URL url = BezierAnim.class.getResource(srcFile); if (url == null) { System.out.println("File doesn't exist: " + srcFile); return; } File file = new File(url.getFile()); try { if ((reader = new BufferedReader(new FileReader(file))) != null) getLine(); } catch (Exception e) { e.printStackTrace(); reader = null; } fn = 0; } public String getLine() { String str = null; try { if ((str = reader.readLine()) != null) { if (str.length() == 0) str = " "; vector.addElement(new TextLayout(str,fonts[fn++%fonts.length],new FontRenderContext(null, false, false))); } } catch (Exception e) { e.printStackTrace(); reader = null; } return str; } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { 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]; } } strheight = g2.getFontMetrics(fonts[0]).getHeight(); nStrs = h/strheight; vector = new Vector(nStrs); } if (vector != null && vector.size() == 0) getFile(); if (reader != null) { String s = getLine(); if (s == null || vector.size() == nStrs && vector.size() != 0) vector.removeElementAt(0); int yy = (s == null) ? 0 : h - vector.size() * strheight-15; g2.setComposite(AlphaComposite.SrcOver); g2.setColor(Color.lightGray); for (int i = 0; i < vector.size(); i++) ((TextLayout)vector.get(i)).draw(g2, 1f,(float) (yy += strheight)); } GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO); float[] ctrlpts; for (int i = 0; i < animpts.length; i += 2) { animate(animpts, deltas, i + 0, w); animate(animpts, deltas, i + 1, h); } ctrlpts = animpts; int len = ctrlpts.length; gp.reset(); 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; 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.setComposite(ac); g2.setColor(Color.blue); g2.setStroke(solid); g2.draw(gp); g2.setColor(Color.green); g2.fill(gp); } public static void main(String s[]) { final BezierAnim demo = new BezierAnim(); 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 - BezierAnim"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,400)); f.show(); demo.start(); } }