1 /* 2 *This program will ask for what color dots to print. 3 * There are three choices Green, Blue, or Red. 4 */ 5 package npw; 6 import java.awt.Color; 7 import java.util.Scanner; 8 import javax.swing.JOptionPane; 9 import javax.swing.SwingUtilities; 10 import painter.SPainter; 11 import shapes.SCircle; 12 13 public class Number4 { 14 //Solution to the graphical problem. 15 private void paintTheImage() { 16 //Get the input info from the user. 17 int numOfRows = getNumber("rows"); 18 int numOfColumns = getNumber("columns"); 19 int sizeOfCircle = getNumber("circle side length"); 20 String whatColor = getColor("What color dots? Options are... Green, Blue and, Red"); 21 //Establishing the painter. 22 int height = numOfRows * sizeOfCircle; 23 int width = numOfColumns * sizeOfCircle; 24 SPainter leo = new SPainter("Number 4",width+50,height+50); 25 leo.setBrushWidth(4); 26 SCircle circle = new SCircle(sizeOfCircle); 27 //paint the rectangles 28 paintCircle(leo,circle,numOfRows,numOfColumns, whatColor); 29 } 30 31 private String getColor(String prompt) { 32 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 33 Scanner color = new Scanner(nss); 34 return color.nextLine(); 35 } 36 37 private int getNumber(String prompt) { 38 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 39 Scanner scan = new Scanner(nss); 40 return scan.nextInt(); 41 } 42 private void paintCircle(SPainter leo, SCircle circle, int numOfRows, int numOfColumns, String whatColor) { 43 //Position the painter to paint the rectangle of circles. 44 leo.mlt(((numOfColumns-1)/2.0)*circle.radius()); 45 leo.mbk(((numOfRows-1)/2.0)*circle.radius()); 46 //Paint the rectangle of circles. 47 int i = 1; 48 while (i <= numOfRows) { 49 paintOneRow(leo,numOfColumns,circle, whatColor); 50 leo.mfd(circle.radius()); 51 i = i + 1; 52 } 53 //Used to make method invariant with respect to painter position. 54 leo.mrt(((numOfColumns-1)/2.0)*circle.radius()); 55 leo.mfd(((numOfColumns-1)/2.0)*circle.radius()); 56 } 57 58 private void paintOneRow(SPainter leo, int numOfColumns, SCircle circle, String whatColor) { 59 int i = 1; 60 while (i <= numOfColumns) { 61 paintOneCircle(leo,circle, whatColor); 62 leo.mrt(circle.radius()); 63 i = i + 1; 64 } 65 //invariant 66 leo.mlt(numOfColumns*circle.radius()); 67 } 68 69 private void paintOneCircle(SPainter leo, SCircle circle, String whatColor) { 70 circle.setRadius(((int) circle.radius())/4); 71 if (whatColor.equalsIgnoreCase("green")) { 72 leo.setColor(Color.GREEN); 73 } else if (whatColor.equalsIgnoreCase("red")) { 74 leo.setColor(Color.RED); 75 } else if (whatColor.equalsIgnoreCase("blue")) { 76 leo.setColor(Color.BLUE); 77 } else { 78 leo.setColor(Color.BLACK); 79 } 80 leo.paint(circle); 81 circle.setRadius(((int) circle.radius())*4); 82 83 84 85 } 86 87 public Number4(){ 88 paintTheImage(); 89 } 90 public static void main(String[] args){ 91 SwingUtilities.invokeLater(new Runnable() { 92 @Override 93 public void run() { 94 new Number4(); 95 } 96 }); 97 } 98 }