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