1 /* This program asks for a number of concentric squares. Two randomly chosen colors will be used for the image. 2 */ 3 4 5 package npw; 6 import java.awt.Color; 7 import java.util.Scanner; 8 import javax.swing.*; 9 import java.util.Random; 10 import painter.SPainter; 11 import shapes.SCircle; 12 import shapes.SSquare; 13 14 public class Stella { 15 private void paintTheImage() { 16 int nrOfSquares = getNrOfSquares("how many concentric squares"); 17 SPainter painter = new SPainter("Stella", 800, 800); 18 int squareSide = 700; 19 paintTheSquares(squareSide,nrOfSquares,painter); 20 21 } 22 23 private void paintTheSquares(int squareSide, int nrOfSquares, SPainter painter) { 24 int i = squareSide; 25 Color color1 = randomColor(); 26 Color color2 = randomColor(); 27 while (i>0){ 28 painter.setColor(color1); 29 SSquare square = new SSquare(i); 30 painter.paint(square); 31 i=i-(squareSide/nrOfSquares); 32 painter.setColor(color2); 33 SSquare square1 =new SSquare(i); 34 painter.paint(square1); 35 i= i-(squareSide/nrOfSquares); 36 } 37 } 38 39 40 41 42 private static Color randomColor() { 43 Random rgen = new Random(); 44 int r = rgen.nextInt(256); 45 int g = rgen.nextInt(256); 46 int b = rgen.nextInt(256); 47 return new Color ( r, g , b); 48 49 50 } 51 52 private static int getNrOfSquares(String prompt) { 53 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 54 Scanner scanner = new Scanner(nss); 55 return scanner.nextInt(); 56 } 57 58 59 public Stella() { 60 paintTheImage(); 61 } 62 63 public static void main(String[] args) { 64 SwingUtilities.invokeLater(new Runnable() { 65 public void run() { 66 new Stella(); 67 } 68 }); 69 } 70 } 71