1 /* 2 * Program that has the user input a number and that many squares will be 3 * painted on top of each other getting increasingly smaller and the color 4 * of the squares alternate between two random colors. 5 */ 6 7 package npw; 8 9 import java.awt.Color; 10 import java.util.Random; 11 import java.util.Scanner; 12 import javax.swing.JOptionPane; 13 import javax.swing.SwingUtilities; 14 import painter.SPainter; 15 import shapes.SSquare; 16 17 public class Stella { 18 19 public static void main(String[] args) { 20 SPainter painter = new SPainter("Stella", 800, 800); 21 SSquare square = new SSquare(700); 22 int nrOfSquares = getNumber("number of squares"); 23 Color randomcolor1 = randomColor(); 24 Color randomcolor2 = randomColor(); 25 double change = (700.0 / nrOfSquares); 26 int x = 0; 27 while (x < nrOfSquares) { 28 if (x % 2 == 1) { 29 painter.setColor(randomcolor1); 30 } else { 31 painter.setColor(randomcolor2); 32 } 33 x = x + 1; 34 painter.paint(square); 35 square.shrink(change); 36 } 37 } 38 39 private static int getNumber(String prompt) { 40 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 41 Scanner scanner = new Scanner(nss); 42 return scanner.nextInt(); 43 } 44 45 private static Color randomColor() { 46 Random rgen = new Random(); 47 int r = rgen.nextInt(256); 48 int g = rgen.nextInt(256); 49 int b = rgen.nextInt(256); 50 return new Color(r,b,g); 51 } 52 } 53 54 55 56 57 58 59 60 61