1 /* 2 * Program to paint a user-specified number of concentric squares in a 700x700 square with random colors. 3 */ 4 package npw; 5 import java.awt.*; 6 import java.util.Random; 7 import java.util.Scanner; 8 import javax.swing.*; 9 import painter.SPainter; 10 import shapes.SSquare; 11 12 public class Stella { 13 14 private void paintTheImage() { 15 String nss = JOptionPane.showInputDialog(null, "number of concentric squares"); 16 Scanner scanner = new Scanner(nss); 17 int conSquares = scanner.nextInt(); 18 SPainter painter = new SPainter("Stella", 800, 800); 19 double squareSide = 700; 20 SSquare square = new SSquare(squareSide); 21 Random rgen = new Random(); 22 int r1 = rgen.nextInt(256); 23 int g1 = rgen.nextInt(256); 24 int b1 = rgen.nextInt(256); 25 int r2 = rgen.nextInt(256); 26 int g2 = rgen.nextInt(256); 27 int b2 = rgen.nextInt(256); 28 painter.setColor(Color.getHSBColor(r1,g1,b1)); 29 painter.paint(square); 30 int i = 0; 31 while (i < conSquares) { 32 if (i % 2 == 0) { 33 painter.setColor(Color.getHSBColor(r2,g2,b2)); 34 } else if (i % 2 == 1) { 35 painter.setColor(Color.getHSBColor(r1,g1,b1)); 36 } 37 squareSide = squareSide - (square.side() / conSquares); 38 SSquare newSquare = new SSquare(squareSide); 39 painter.paint(newSquare); 40 i = i + 1; 41 } 42 } 43 44 public Stella() { 45 paintTheImage(); 46 } 47 48 public static void main(String[] args) { 49 SwingUtilities.invokeLater(new Runnable() { 50 public void run() { 51 new Stella(); 52 } 53 }); 54 } 55 } 56 57