1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import shapes.SRectangle; 6 import shapes.SSquare; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.util.Random; 11 import java.util.Scanner; 12 13 public class Invention2 { 14 15 public static void main(String[] args) { 16 SwingUtilities.invokeLater(new Runnable() { 17 public void run() { 18 new Invention2(); 19 } 20 }); 21 } 22 23 public Invention2() { 24 paintTheImage(); 25 } 26 27 private void paintTheImage() { 28 // Grab the input information 29 int width = 1000; 30 int height = 1000; 31 int colWidth = 15; 32 // Establish the painter 33 SPainter painter = new SPainter("Invention 2", width, height); 34 SRectangle box = new SRectangle(10, 15); 35 36 37 // Move the painter to the upper left corner to get ready to paint the gradient 38 painter.mfd(height / 2); 39 painter.tl(90); 40 painter.mfd(width / 2 - 10); 41 painter.tl(90); 42 43 // Paint it! 44 paintGradient(painter, box, height, width, colWidth); 45 } 46 47 private void paintGradient(SPainter painter, SRectangle box, int height, int width, int colWidth) { 48 int cols = 0; 49 // Calculate the number of columns. We want to fill the screen but we don't want 50 //any of the dots only half on the canvas, so we subtract some space. 51 int nrOfCols = (width / colWidth) - 2; 52 53 while (cols < nrOfCols) { 54 nextCol(painter, colWidth); 55 paintColumn(painter, box, height); 56 cols = cols + 1; 57 } 58 } 59 60 private void paintOneDot(SPainter painter, SRectangle box) { 61 randomColor(painter); 62 painter.paint(box); 63 64 } 65 66 67 // Dots are spaced tighter together near the bottom of the canvas. 68 private void paintColumn(SPainter painter, SRectangle box, int height) { 69 int travel = 0; 70 int totalDistanceTraveled = 0; 71 72 // Paint a row with decreasing distance between dots. 73 while (totalDistanceTraveled < height - (box.diagonal() * 3)) { 74 travel = randomDistance((height - totalDistanceTraveled) / 4); 75 totalDistanceTraveled = totalDistanceTraveled + travel; 76 painter.mfd(travel); 77 paintOneDot(painter, box); 78 } 79 80 // Make the method invariant with respect to painter position. 81 painter.mbk(totalDistanceTraveled); 82 } 83 84 // Moves the painter to the next column. 85 private void nextCol(SPainter painter, int colWidth) { 86 painter.tl(90); 87 painter.mfd(colWidth); 88 painter.tr(90); 89 } 90 91 private int randomDistance(int maxDistance) { 92 Random rgen = new Random(); 93 return rgen.nextInt(maxDistance); 94 } 95 96 private void randomColor(SPainter painter) { 97 Random rgen = new Random(); 98 int rn = rgen.nextInt(6); 99 if (rn == 0) { 100 painter.setColor(new Color(0, 0, 0, 255)); 101 } else if (rn == 1) { 102 painter.setColor(new Color(255, 244, 0)); 103 } else if (rn == 2) { 104 painter.setColor(new Color(255, 5, 0)); 105 } else if (rn == 3) { 106 painter.setColor(new Color(3, 0, 210)); 107 } else if (rn == 4) { 108 painter.setColor(new Color(206, 203, 50)); 109 } else { 110 painter.setColor(new Color(171, 171, 171)); 111 } 112 } 113 } 114