/************************************************************************** Synopsis: Animation involving circular blocks Author: Kannan Balasubramanian Last Updated Date: 11/24/2007 **************************************************************************/ import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RadialGradientPaint; import java.awt.MultipleGradientPaint; import java.awt.MultipleGradientPaint.CycleMethod; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.RenderingHints; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import java.util.Vector; import java.util.Random; import java.awt.geom.*; /* * Documented: * Press key 'c' or 'C' to toggle circle colors * Press 'left/right' arrow to increase/decrease circle count * Press 'up/down' arrow to increase/decrease circle radius * Press key 'p' to pause/resume animation * Press 'Esc' key to exit * * Undocumented: * Press key 'a' or 'A' to toggle antialiasing of the entire screen * Press key '3' to draw circles in 3D shapes * Press key '2' to draw circles in 2D shapes */ public class StarField extends JPanel implements ActionListener { private Font font0 = new Font("Courier", Font.PLAIN, 20); private Font font1 = new Font("Courier", Font.PLAIN, 12); private Font font2 = new Font("Courier", Font.PLAIN, 16); private Vector circleVector = new Vector(); public static final Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); private boolean bColor = false; private boolean bPause = false; private static final String NO_OF_CIRCLES = "No. of circles: "; public StarField() { setBackground(Color.black); setFocusable(true); addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { if (!bPause && !Circle.is3D() && (e.getKeyChar() == 'c' || e.getKeyChar() == 'C')) { bColor = !bColor; for (int i=0; i= 15) { removeCircles(10); } } else if (e.getKeyCode() == KeyEvent.VK_UP) { Circle circle = null; for (int i=0; i= MIN_RADIUS) { radius -= r; } return radius; } public void setOffset(int sx, int sy) { cx += sx; cy += sy; } public int getDirection(int x, int y, int sx, int sy, int sw, int sh) { int midx = sx + sw/2; int midy = sy + sh/2; int v = rand.nextInt(10) % 3; if (x <= midx && y <= midy) { //1st Quadrant if (v == 0) { direction = 3; } else if (v == 1) { direction = 4; } else if (v == 2) { direction = 5; } } else if (x > midx && y <= midy) { //2nd Quadrant if (v == 0) { direction = 2; } else if (v == 1) { direction = 5; } else if (v == 2) { direction = 6; } } else if (x <= midx && y > midy) { //4th Quadrant if (v == 0) { direction = 1; } else if (v == 1) { direction = 4; } else if (v == 2) { direction = 7; } } else if (x > midx && y > midy) { //3rd Quadrant if (v == 0) { direction = 0; } else if (v == 1) { direction = 6; } else if (v == 2) { direction = 7; } } return direction; } public int getDirection() { return direction; } public boolean resetCircle() { int midwidth = StarField.screensize.width/2; int sx = midwidth/4; int sy = StarField.screensize.height/4; int wx = midwidth/2; int wy = StarField.screensize.height/2; if ( !( cx <= 0 || cx >= midwidth || cy <= 0 || cy >= StarField.screensize.height) ) { return false; } cx = sx + (int)(Math.random() * wx); cy = sy + (int)(Math.random() * wy); int val = (int)(Math.random() * 10); getDirection(cx, cy, sx, sy, wx, wy); if (radius != -1) { return true; } if (val <= 5) { setRadius(5); } else { setRadius(7); } return true; } public static void toggleAntialiasing() { bAntialias = !bAntialias; } }