1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 6 import javax.swing.*; 7 import javax.swing.plaf.ColorChooserUI; 8 import java.awt.*; 9 import java.util.Random; 10 import java.util.Scanner; 11 12 public class SimpleDots { 13 public static void main(String[] args) { 14 SwingUtilities.invokeLater(SimpleDots::new); 15 } 16 17 public SimpleDots() { 18 paintTheImage(); 19 } 20 21 private String colorChoice = getString ("Red, Blue or Green?"); 22 23 private void paintTheImage() { 24 // Grab the input information 25 int width = getNumber("width"); 26 int height = getNumber("height"); 27 int dotSpacing = getNumber("dot spacing"); 28 29 // Establish the painter 30 SPainter painter = new SPainter("Abstract Gradient", width, height); 31 SCircle dot = new SCircle(5); 32 33 // Move the painter to the upper left corner to get ready to paint the gradient 34 painter.mfd(height / 2); 35 painter.tl(90); 36 painter.mfd(width / 2); 37 painter.tl(90); 38 39 // Paint it! 40 paintGradient(painter, dot, height, width, dotSpacing); 41 } 42 43 private static int getNumber(String prompt) { 44 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 45 Scanner scanner = new Scanner(nss); 46 return scanner.nextInt(); 47 } 48 49 private static String getString(String prompt) { 50 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 51 Scanner scanner = new Scanner(nss); 52 return scanner.next(); 53 } 54 55 private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpacing) { 56 int cols = 0; 57 // Calculate the number of columns. 58 int nrOfCols = (width / dotSpacing - 1); 59 60 while (cols < nrOfCols) { 61 nextCol(painter, dotSpacing); 62 paintColumn(painter, dot, height, dotSpacing); 63 cols = cols + 1; 64 } 65 } 66 67 private void paintOneDot(SPainter painter, SCircle dot) { 68 chooseColors(painter); 69 painter.paint(dot); 70 71 } 72 73 private void randomColor(SPainter painter) { 74 Random colorful = new Random(); 75 int r = colorful.nextInt(255); 76 int g = colorful.nextInt(255); 77 int b = colorful.nextInt(255); 78 painter.setColor(new Color(r, g, b)); 79 80 } 81 82 // Dots are spaced tighter together near the bottom of the canvas.-CHANGE 83 private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpacing) { 84 int travel = 0; 85 int totalDistanceTraveled = 0; 86 87 // Paint a row with equal distance between dots. 88 while (totalDistanceTraveled < height - (dot.radius() * 3)) { 89 //travel = randomDistance((height - totalDistanceTraveled) / 4); 90 travel = dotSpacing; 91 totalDistanceTraveled = totalDistanceTraveled + travel; 92 painter.mfd(travel); 93 paintOneDot(painter, dot); 94 } 95 96 // Make the method invariant with respect to painter position. 97 painter.mbk(totalDistanceTraveled); 98 } 99 100 // Moves the painter to the next column. 101 private void nextCol(SPainter painter, int colWidth) { 102 painter.tl(90); 103 painter.mfd(colWidth); 104 painter.tr(90); 105 } 106 107 108 private void chooseColors(SPainter painter) { 109 110 111 if (colorChoice.equalsIgnoreCase("red")) { 112 painter.setColor(Color.RED); 113 } else if (colorChoice.equalsIgnoreCase("blue")) { 114 painter.setColor(Color.BLUE); 115 } else if (colorChoice.equalsIgnoreCase("green")) { 116 painter.setColor(Color.GREEN); 117 } else { 118 painter.setColor(Color.BLACK); 119 } 120 } 121 122 } 123