1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 3 */ 4 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 import shapes.SSquare; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Random; 14 import java.util.Scanner; 15 16 public class Invention2 { 17 private void paintTheImage() { 18 SPainter painter = new SPainter("Invention 2", 600, 600); 19 SCircle dot = new SCircle(200); 20 SSquare square = new SSquare(400); 21 22 String command = getString("Square or target?"); 23 int number = getNumber("Enter the number of targets inside of a target, or squares inside of a square."); 24 int i = 0; 25 26 while( i < number ) { 27 28 Random rgen = new Random(); 29 int rn = rgen.nextInt(); 30 //first square 31 if (command.equalsIgnoreCase("target")) { 32 painter.paint(dot); 33 34 painter.setColor(randomColor()); 35 36 //Second square 37 dot.s3(); 38 dot.x2(); 39 painter.setColor(randomColor()); 40 painter.paint(dot); 41 42 //Third square 43 dot.s2(); 44 painter.paint(dot); 45 painter.setColor(randomColor()); 46 47 } else { 48 { 49 //First Circle 50 painter.paint(square); 51 painter.setColor(randomColor()); 52 53 //Second Circle 54 square.s3(); 55 square.x2(); 56 painter.setColor(randomColor()); 57 painter.paint(square); 58 59 //Third Circle 60 square.s2(); 61 painter.setColor(randomColor()); 62 painter.paint(square); 63 } 64 i = i + 1; 65 } 66 } 67 68 } 69 70 private static int getNumber(String prompt) { 71 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 72 Scanner scanner = new Scanner(nss); 73 return scanner.nextInt(); 74 } 75 76 private static String getString(String prompt) { 77 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 78 Scanner scanner = new Scanner(nss); 79 return scanner.next(); 80 } 81 private static Color randomColor(){ 82 Random rgen = new Random(); 83 int r = rgen.nextInt(256); 84 int g = rgen.nextInt(256); 85 int b = rgen.nextInt(256); 86 return new Color(r, g, b); 87 } 88 89 //REQUIRED INFRASTRUCTURE 90 91 public Invention2() { 92 paintTheImage(); 93 } 94 95 public static void main(String[] args) { 96 SwingUtilities.invokeLater(new Runnable() { 97 public void run() { 98 new Invention2(); 99 } 100 }); 101 } 102 }