/* * @(#)Stars.java 1.10 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.*; import java.lang.Math; import java.util.Vector; import J2DCanvas; /** * Rendering single width & height rectangles in different colors. */ public class Stars extends J2DCanvas { private Color colors[] = { Color.red, Color.green, Color.white }; private int cn; private Vector vector = new Vector(); public Stars() { setBackground(Color.black); clearCanvas = false; runnable = true; } public void drawDemo(Graphics2D g2) { if (vector.size() == 300) { g2.setColor(Color.black); g2.setComposite(AlphaComposite.SrcOver); g2.fill((Rectangle2D) vector.firstElement()); vector.removeElementAt(0); } g2.setColor(colors[cn%3]); vector.addElement(new Rectangle2D.Double(w*Math.random(), h*Math.random(), 2,2)); g2.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_OVER, (float) Math.random())); g2.fill((Rectangle2D) vector.lastElement()); if (cn++ == 300) cn = 0; } public static void main(String s[]) { final Stars demo = new Stars(); 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 - Stars"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }