// file: TestGSpark.java // date: 3/23/2014 // author: Dr. Van Cleave // // This program will test the GSpark class by creating a fountain of GSparks // in the middle of the window. The number of sparks in the fountain is // based on the slider value. // After the GSparks are created and added to the window, a loop will cause them // to move until they (1) exit out the top of the window, (2) fadeout, or (3) // the max number of moves for the loop is reached package testing; import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; import graphics.*; public class TestGSpark extends SliderProgram { public void init() { setSize(600, 600); super.init(); setRange(25, 100); // number of sparks in the fountain } public void run() { // slider indicates number of sparks to create int numSparks = getN(); // fill the window with a black background for contrast double winW = getWidth(); double winH = getHeight(); GRect background = new GRect(0.0, 0.0, winW, winH); background.setFilled(true); add(background); // create the desired number of sparks in the center of the window, // with random size, colors, velocities, and fadeout times double centerX = winW / 2.0; double centerY = winH / 2.0; for (int count = 1; count <= numSparks; count++) { // determine random velocity double deltax = rgen.nextDouble(lowDx, highDx); double deltay = rgen.nextDouble(lowDy, highDy); // make the spark act more spark-like, beyond a certain threshhold // for movement in the y-direction (speeding upward), reduce the // movement in the x direction if (deltay < cutOffDy) { deltax *= scaleDx; } // determine random size, fadeout time, and color int diam = rgen.nextInt(lowerRadius, upperRadius) * 2; int life = rgen.nextInt(lowerTime, upperTime); Color light = rgen.nextColor(); // create and add the GSpark GSpark sparky = new GSpark(centerX, centerY, diam, light, deltax, deltay, getGCanvas(), life); add(sparky); } // end of creating fountain of GSparks // Move all sparks in the window max times for (int makeMove = 1; makeMove <= maxMoves; makeMove++) { // For every object in our window... for (int j = 0; j < getElementCount(); j++) { // Get the jth object GObject gObject = getElement(j); // Make sure it is a GSpark if (gObject instanceof GSpark) { // Since we have a GSpark, cast it as such and move it. ((GSpark) gObject).move(); } pause(sleepTime); } // end of inspecting window elements } // end of making GSParks move loop // let the user know the fountain loop has been exhausted GLabel bye = new GLabel("THE END.", centerX, centerY); bye.setColor(Color.green); add(bye); } // end of run() method // Constant Declaration Section // how long to pause between moves, and the number of moves in this test private static final int sleepTime = 5; private static final int maxMoves = 250; // Ranges for particle attributes // Min and max size -- diameter between 4 and 10 private static final int lowerRadius = 2; private static final int upperRadius = 5; // Min moves until removed from window private static final int lowerTime = 10; private static final int upperTime = 250; // Min and max dx and dy values private static final double lowDx = -20.0; private static final double highDx = 20.0; private static final double lowDy = -100.0; private static final double highDy = -10.0; // max value for Dy that requires a decrease in Dx to make it act // spark-like, and the scale factor for changing Dx private static final double cutOffDy = -75; private static final double scaleDx = 0.65; // rgen allows for random direction, velocity, and color private RandomGenerator rgen = RandomGenerator.getInstance(); public static void main(String args[]) { (new TestGSpark()).start(); } // end of main() method } // end of TestGSpark program class