1 /* 2 * Program to paint a rectangle, centered in the canvas, made up of red, green, blue, or black 3 * colored circles that are spaced evenly. 4 */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SCircle; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Scanner; 14 15 public class Number4 { 16 // The solution to the graphical problem. 17 private static void paintTheImage() { 18 // Get the input information fron the user. 19 int nrOfRows = getNumber("rows"); 20 int nrOfColumns = getNumber("columns"); 21 int sizeOfSquare = getNumber("circle radius length"); 22 // Establish the painter. 23 int height = nrOfRows * sizeOfSquare; 24 int width = nrOfColumns * sizeOfSquare; 25 Color color = setColor(); 26 SPainter miro = new SPainter("Number 1", (width * 2) + 50, (height * 2) + 50); 27 miro.setBrushWidth(4); 28 SCircle circle = new SCircle(sizeOfSquare); 29 miro.setColor(color); 30 // Paint the rectangles. 31 paintRectangle(miro, circle, nrOfRows, nrOfColumns); 32 } 33 34 private static int getNumber(String prompt) { 35 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 36 Scanner scanner = new Scanner(nss); 37 return scanner.nextInt(); 38 } 39 40 private static void paintRectangle(SPainter miro, SCircle circle, 41 int nrOfRows, int nrOfColumns) { 42 // Position the painter to paint the rectangle of circles. 43 miro.mlt(((nrOfColumns - 1) / 2.0) * circle.radius() * 2); //changed to diameter shifted left 44 miro.mbk(((nrOfRows - 1) / 2.0) * circle.radius() * 2); //changed to diameter shifted it down 45 // Paint the rectangle of circles. 46 int i = 1; 47 while (i <= nrOfRows) { 48 paintOneRow(miro, nrOfColumns, circle); 49 miro.mfd(circle.diameter()); 50 i = i + 1; 51 } 52 // Make the method invariant with respect to painter position. 53 miro.mrt(((nrOfColumns - 1) / 2.0) * circle.radius()); 54 miro.mfd(((nrOfRows - 1) / 2.0) * circle.radius()); 55 } 56 57 private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) { 58 int i = 1; 59 while (i <= nrOfColumns) { 60 paintOneCircle(miro, circle); 61 miro.mrt(circle.diameter()); //moved over too much when * 1.5 62 i = i + 1; 63 } 64 miro.mlt(nrOfColumns * circle.diameter()); 65 } 66 67 private static void paintOneCircle(SPainter miro, SCircle circle) { 68 circle.shrink(circle.radius() / 2); 69 miro.paint(circle); 70 circle.expand(circle.radius()); 71 } 72 73 private static Color setColor() { 74 String nss = JOptionPane.showInputDialog(null, "Color?"); 75 Scanner scanner = new Scanner(nss); 76 // String command = JOptionPane.showInputDialog(null, "Color?"); 77 String command = scanner.nextLine(); 78 if (command == null) { 79 command = "exit"; 80 } //user clicked on Canceln 81 if (command.equalsIgnoreCase("blue")) { 82 return (Color.BLUE); 83 } else if (command.equalsIgnoreCase("red")) { 84 return (Color.RED); 85 } else if (command.equalsIgnoreCase("green")) { 86 return (Color.GREEN); 87 } else { 88 return(Color.BLACK); 89 } 90 } 91 92 93 public Number4() { 94 paintTheImage(); 95 } 96 97 public static void main(String[] args) { 98 SwingUtilities.invokeLater(new Runnable() { 99 @Override 100 public void run() { 101 new Number4(); 102 } 103 }); 104 } 105 }