1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Random; 9 import java.util.Scanner; 10 11 public class SimpleDots { 12 13 public static void main(String[] args) { 14 SwingUtilities.invokeLater(SimpleDots::new); 15 } 16 17 public SimpleDots() { 18 paintTheImage(); 19 } 20 21 private void paintTheImage() { 22 // Grab the input information 23 int width = getNumber("width"); 24 int height = getNumber("height"); 25 int dotSpace = getNumber("horizontal space between dots"); 26 String color = getColor("color"); 27 // Establish the painter 28 SPainter painter = new SPainter("Abstract Gradient", width, height); 29 SCircle dot = new SCircle(5); 30 31 // Move the painter to the upper left corner to get ready to paint the gradient 32 painter.mfd(height / 2.0); 33 painter.tl(); 34 painter.mfd(width / 2.0); 35 painter.tl(); 36 37 // Paint it! 38 paintGradient(painter, dot, height, width, dotSpace, color); 39 } 40 41 private String getColor(String color) { 42 String nss = JOptionPane.showInputDialog(null, color + "?"); 43 Scanner scanner = new Scanner(nss); 44 return scanner.next(); 45 46 47 } 48 49 50 private static int getNumber(String prompt) { 51 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 52 Scanner scanner = new Scanner(nss); 53 return scanner.nextInt(); 54 } 55 56 private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpace, String color) { 57 // Calculate the number of columns. We want to fill the screen, but don't want any columns half on the canvas. 58 // A column takes up the horizontal space of a dot's diameter plus the space between it and a neighbor. 59 double colWidth = dot.diameter() + dotSpace; 60 // We don't want a column all the way on the edge on the right side, so subtract 1. 61 int nrOfCols = (int) Math.floor((width / colWidth)) - 1; 62 63 int cols = 0; 64 while (cols < nrOfCols) { 65 nextCol(painter, dot, dotSpace); 66 paintColumn(painter, dot, height, dotSpace, color); 67 cols = cols + 1; 68 } 69 } 70 71 private void paintOneDot(SPainter painter, SCircle dot, String color) { 72 painter.setColor(fixedColor(color)); 73 painter.paint(dot); 74 75 } 76 77 private Color fixedColor(String color) { 78 if (color.equalsIgnoreCase("red")) { 79 return Color.red; 80 } else if (color.equalsIgnoreCase("blue")) { 81 return Color.blue; 82 } else if (color.equalsIgnoreCase("green")) { 83 return Color.green; 84 } else { 85 return Color.black; 86 } 87 88 } 89 90 // Dots are spaced more tightly together near the bottom of the canvas. 91 private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpace, String color) { 92 int totalDistanceTraveled = 0; 93 94 // Paint a column with decreasing distance between dots. 95 while (totalDistanceTraveled < height - (dot.radius() * 3)) { 96 int travel = (int) (dot.diameter() + dotSpace); 97 totalDistanceTraveled = totalDistanceTraveled + travel; 98 painter.mfd(travel); 99 paintOneDot(painter, dot, color); 100 } 101 102 // Make the method invariant with respect to painter position. 103 painter.mbk(totalDistanceTraveled); 104 } 105 106 // Moves the painter to the next column. 107 private void nextCol(SPainter painter, SCircle dot, int dotSpace) { 108 painter.tl(); 109 painter.mfd(dot.diameter() + dotSpace); 110 painter.tr(); 111 } 112 }