1 package npw; 2 //This makes a caterpillar, you given the option to pick the background color. 3 //The shapes within the caterpillar and randomized in two different ways. 4 //0-0 Caterpillar eyes look like this... 5 import java.awt.Color; 6 import java.util.Random; 7 import java.util.Scanner; 8 import javax.swing.JOptionPane; 9 import javax.swing.SwingUtilities; 10 import painter.SPainter; 11 import shapes.SRectangle; 12 13 public class Invention2 { 14 15 private void paintTheImage() { 16 17 18 String giveColor = getS("What color should the background be? red, green or blue? if none of these make Black..."); 19 SPainter pokemon = new SPainter("caterpillar",800,800); 20 SRectangle totoro = new SRectangle(250,200); 21 int rv = (int) (Math.random()*256); 22 int gv =(int) (Math.random()*256); 23 int bv = (int) (Math.random()*256); 24 if (giveColor.equalsIgnoreCase("red")) { 25 pokemon.setColor(Color.RED); 26 totoro.setHeight(800); 27 totoro.setWidth(800); 28 pokemon.paint(totoro); 29 totoro.setHeight(250); 30 totoro.setWidth(200); 31 } else if (giveColor.equalsIgnoreCase("green")){ 32 pokemon.setColor(Color.GREEN); 33 totoro.setHeight(800); 34 totoro.setWidth(800); 35 pokemon.paint(totoro); 36 totoro.setHeight(250); 37 totoro.setWidth(200); 38 } else if (giveColor.equalsIgnoreCase("blue")) { 39 pokemon.setColor(Color.BLUE); 40 totoro.setHeight(800); 41 totoro.setWidth(800); 42 pokemon.paint(totoro); 43 totoro.setHeight(250); 44 totoro.setWidth(200); 45 } else { 46 pokemon.setColor(Color.BLACK); 47 totoro.setHeight(800); 48 totoro.setWidth(800); 49 pokemon.paint(totoro); 50 totoro.setHeight(250); 51 totoro.setWidth(200); 52 } 53 54 Color random1 = new Color(rv,gv,bv); 55 connector(pokemon,totoro); 56 loop1(pokemon,totoro,random1); 57 eyes(pokemon,totoro,random1); 58 face(pokemon,totoro); 59 60 61 62 63 } 64 65 private String getS(String prompt) { 66 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 67 Scanner color = new Scanner(nss); 68 return color.nextLine(); 69 } 70 71 private void face(SPainter pokemon, SRectangle totoro) { 72 pokemon.moveToCenter(); 73 pokemon.mrt(295); 74 pokemon.mbk(30); 75 totoro.setWidth(50); 76 totoro.setHeight(50); 77 pokemon.setColor(Color.black); 78 pokemon.paint(totoro); 79 80 } 81 82 private void eyes(SPainter pokemon, SRectangle totoro, Color random1) { 83 pokemon.moveToCenter(); 84 pokemon.mrt(240); 85 pokemon.setColor(random1); 86 pokemon.paint(totoro); 87 pokemon.mrt(110); 88 pokemon.paint(totoro); 89 totoro.setHeight((int) totoro.height()-10); 90 totoro.setWidth((int) totoro.width()-10); 91 pokemon.setColor(Color.black); 92 pokemon.paint(totoro); 93 pokemon.mlt(110); 94 pokemon.paint(totoro); 95 96 } 97 98 private void connector(SPainter pokemon, SRectangle totoro) { 99 pokemon.setColor(Color.cyan); 100 totoro.setHeight(40); 101 totoro.setWidth(650); 102 pokemon.paint(totoro); 103 totoro.setWidth(200); 104 totoro.setHeight(250); 105 } 106 107 108 private void loop1(SPainter pokemon, SRectangle totoro, Color random1) { 109 pokemon.mrt(295); 110 pokemon.setColor(randomColor()); 111 pokemon.paint(totoro); 112 pokemon.setColor(Color.black); 113 pokemon.draw(totoro); 114 int num = 5; 115 int i = 1; 116 while (i <= num) { 117 pokemon.mlt((totoro.width()-4)); 118 totoro.setHeight((int) (totoro.height()-35)); 119 totoro.setWidth((int) (totoro.width()-35)); 120 pokemon.setColor(random1); 121 pokemon.paint(totoro); 122 pokemon.setColor(Color.black); 123 pokemon.draw(totoro); 124 i = i + 1; 125 if (i <= num) { 126 pokemon.mlt((totoro.width()-4)); 127 totoro.setHeight((int) (totoro.height()-35)); 128 totoro.setWidth((int) (totoro.width()-35)); 129 pokemon.setColor(randomColor()); 130 pokemon.paint(totoro); 131 pokemon.setColor(Color.black); 132 pokemon.draw(totoro); 133 } 134 i = i + 1; 135 136 } 137 138 } 139 140 private Color randomColor() { 141 Random randomGenerator = new Random(); 142 int r = randomGenerator.nextInt(256); 143 int g = randomGenerator.nextInt(256); 144 int b = randomGenerator.nextInt(256); 145 return new Color(r,b,g); 146 } 147 148 public Invention2(){ 149 paintTheImage(); 150 } 151 public static void main(String[] args){ 152 SwingUtilities.invokeLater(new Runnable() { 153 @Override 154 public void run() { 155 new Invention2(); 156 } 157 }); 158 } 159 }