1 package NPW; 2 3 import java.awt.*; 4 import java.util.Random; 5 import java.util.Scanner; 6 import javax.swing.JOptionPane; 7 import javax.swing.SwingUtilities; 8 import painter.SPainter; 9 import shapes.SCircle; 10 11 public class Number4 { 12 // The solution to the graphical problem. 13 String whatcolor =getString("What Color man? "); 14 Color color = getcolor(whatcolor); 15 private void paintTheImage() { 16 // Get the input information from the user. 17 18 int nrOfRows = getNumber("rows"); 19 int nrOfColumns = getNumber("columns"); 20 int sizeOfCirc = getNumber("Circle radius length"); 21 22 // Establish the painter. 23 int height = nrOfRows * sizeOfCirc; 24 int width = nrOfColumns * sizeOfCirc; 25 SPainter miro = new SPainter("Simple Dots",width+50,height+50); 26 miro.setBrushWidth(4); 27 SCircle circ = new SCircle(sizeOfCirc); 28 29 // Paint the circles. 30 paintCircles(miro,circ,nrOfRows,nrOfColumns); 31 } 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 private static String getString(String prompt) { 40 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 41 Scanner scanner = new Scanner(nss); 42 return scanner.next(); 43 } 44 45 private void paintCircles(SPainter miro, SCircle circ, 46 int nrOfRows, int nrOfColumns) { 47 // Position the painter to paint the rectangle of squares. 48 miro.mlt(((nrOfColumns-1)/2.0) * circ.radius()); 49 miro.mbk(((nrOfRows-1)/2.0) * circ.radius()); 50 51 // Paint the rectangle of squares. 52 int i = 1; 53 while ( i <= nrOfRows) { 54 paintOneRow(miro,nrOfColumns, circ); 55 miro.mfd(circ.radius()); 56 i = i + 1; 57 } 58 59 // Make the method invariant with respect to painter position. 60 miro.mrt(((nrOfColumns-1)/2.0) * circ.radius()); 61 miro.mfd(((nrOfRows-1)/2.0) * circ.radius()); 62 } 63 64 private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circ) { 65 int i = 1; 66 while ( i <= nrOfColumns ) { 67 paintOnedot(miro, circ); 68 miro.mrt(circ.radius()); 69 i = i + 1; 70 } 71 miro.mlt(nrOfColumns* circ.radius()); 72 } 73 // made 74 private void paintOnedot(SPainter miro, SCircle circ) { 75 circ.s2(); 76 miro.setColor(color); 77 miro.paint(circ); 78 miro.draw(circ); 79 circ.x2(); 80 } 81 82 private static Color getcolor(String x) { 83 if (x.equalsIgnoreCase("red")) { 84 return Color.red; 85 } 86 if (x.equalsIgnoreCase("blue")) { 87 return Color.blue; 88 } 89 if (x.equalsIgnoreCase("green")) { 90 return Color.green; 91 } else{ 92 return Color.black; 93 } 94 95 } 96 97 98 99 public Number4(){ 100 paintTheImage(); 101 } 102 103 public static void main(String[] args){ 104 SwingUtilities.invokeLater(new Runnable() { 105 @Override 106 public void run() { 107 new Number4(); 108 } 109 }); 110 } 111 }