package npw; import painter.SPainter; import shapes.SCircle; import shapes.SSquare; import javax.swing.*; import java.awt.*; import java.util.Scanner; public class Invention1 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Invention1(); } }); } public Invention1() { paintTheImage(); } private void paintTheImage() { //Grab input information. int width = getNumber("width"); int height = getNumber("height"); int squareSpacing = getNumber("spacing"); String squareColor = getColor("color"); //Establish the painter. SPainter painter = new SPainter("Invention1", width, height); SSquare square = new SSquare(20); SCircle circle = new SCircle(200); painter.setColor(Color.PINK); painter.paint(circle); //Move the painter to the upper left to get ready to paint the gradient. painter.mfd(height / 2); painter.tl(90); painter.mfd(width / 2 - 10); painter.tl(90); //Paint it! paintGradient(painter, square,circle , height, width, squareSpacing, squareColor); } private static int getNumber(String prompt) { String nss = JOptionPane.showInputDialog(null, prompt + "?"); Scanner scanner = new Scanner(nss); return scanner.nextInt(); } private static String getColor(String prompt) { String squareColor = JOptionPane.showInputDialog(null, prompt + "?"); Scanner colorScanner = new Scanner(squareColor); return colorScanner.next(); } private void paintGradient(SPainter painter, SSquare square,SCircle circle, int height, int width, int squareSpacing, String squareColor) { int cols = 0; /*Calculate the number of columns. We want to fill the screen, but we don't want any dots only half on the canvas, so we subtract some space.*/ int nrOfCols = (width / squareSpacing) - 2; while (cols < nrOfCols) { nextCol(painter, squareSpacing); paintColumn(painter, square, circle, height, squareSpacing, squareColor); cols = cols + 1; } } private void paintOneDot(SPainter painter, SSquare dot, String squareColor) { if (squareColor.equalsIgnoreCase("Blue")) { painter.setColor(Color.BLUE); } else if (squareColor.equalsIgnoreCase("Red")) { painter.setColor(Color.RED); } else if (squareColor.equalsIgnoreCase("Green")) { painter.setColor(Color.GREEN); } else { painter.setColor(Color.BLACK); } painter.paint(dot); } //Dots are spaced evenly. private void paintColumn(SPainter painter, SSquare square,SCircle circle, int height, int squareSpacing, String squareColor) { int travel = 0; int totalDistanceTraveled = 0; //Paint a row with the same distance between dots, with no dots being cutoff. while (totalDistanceTraveled < height - (square.side() * 3)) { travel = squareSpacing; totalDistanceTraveled = totalDistanceTraveled + travel; painter.mfd(travel); paintOneDot(painter, square, squareColor); } //Make the method invariant with respect to painter position. painter.mbk(totalDistanceTraveled); } //Moves the painter to the next column. private void nextCol(SPainter painter, int dotSpacing) { painter.tl(90); painter.mfd(dotSpacing); painter.tr(90); } }