1 /* 2 * This program creates an image that I coded myself. 3 */ 4 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SSquare; 9 import shapes.SCircle; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Scanner; 14 15 public class Invention1 { 16 17 public Invention1() { 18 paintTheImage(); 19 } 20 21 private void paintTheImage() { 22 // User inputs values. 23 int x = getNumber("length of square side and double the diameter of circle"); 24 int n = getNumber("how many circles and squares would you like?"); 25 26 // Creates shapes. 27 SSquare square = new SSquare(x); 28 SCircle circle = new SCircle(x / 2); 29 30 // Creates tab that image will be displayed in 31 SPainter painter = new SPainter("Invention 1", x * 15, x * 15); 32 33 // paints the image 34 paintShapes(painter, square, circle, n, x); 35 } 36 37 private void paintShapes(SPainter painter, SSquare square, SCircle circle, int n, int x) { 38 int i = 0; 39 while ( i < n) { 40 chooseColor(painter); 41 painter.draw(square); 42 painter.draw(circle); 43 square.x2(); 44 circle.x2(); 45 i = i + 1; 46 } 47 48 } 49 50 private void chooseColor(SPainter painter) { 51 String command = JOptionPane.showInputDialog(null, "What color shapes would you like? (red, green, blue, purple, orange, yellow, pink, or gray)"); 52 if (command.equalsIgnoreCase("blue")) { 53 painter.setColor(Color.BLUE); 54 } else if (command.equalsIgnoreCase("red")) { 55 painter.setColor(Color.RED); 56 } else if (command.equalsIgnoreCase("green")) { 57 painter.setColor(Color.GREEN); 58 } else if (command.equalsIgnoreCase("purple")) { 59 painter.setColor(Color.MAGENTA); 60 } else if (command.equalsIgnoreCase("orange")) { 61 painter.setColor(Color.ORANGE); 62 } else if (command.equalsIgnoreCase("yellow")) { 63 painter.setColor(Color.YELLOW); 64 } else if (command.equalsIgnoreCase("pink")) { 65 painter.setColor(Color.PINK); 66 } else if (command.equalsIgnoreCase("gray")) { 67 painter.setColor(Color.GRAY); 68 } else { 69 painter.setColor(Color.BLACK); 70 } 71 } 72 73 private static int getNumber (String prompt){ 74 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 75 Scanner scanner = new Scanner(nss); 76 return scanner.nextInt(); 77 } 78 79 // Every program needs a main method 80 public static void main (String[] args) { 81 SwingUtilities.invokeLater(new Runnable() { 82 public void run() { new Invention1(); } 83 }); 84 } 85 } 86