/* * A program to create an image using a rectangle, and while and if statements to create different images everytime. */ package npw; import painter.SPainter; import shapes.SRectangle; import javax.swing.*; import java.awt.*; import java.util.Random; public class Invention2 { public Invention2() { paintTheImage(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Invention2(); } }); } private void paintTheImage() { SPainter painter = new SPainter("Picture" , 600, 600); int nrOfRects = 7; paintRectangles(painter,nrOfRects); } private void paintRectangles(SPainter painter, int nrOfBalloons) { int i = 1; while ( i <= nrOfBalloons ) { paintOneRectangle(painter); i = i + 1; } } private void paintOneRectangle(SPainter painter) { SRectangle rectangle = new SRectangle(90, 120); Random rgen = new Random(); int rn = rgen.nextInt(8); if ( rn == 0 ) { painter.setColor(Color.CYAN); rectangle.shrink(40, 60); } else if ( rn == 3 ) { painter.setColor(Color.ORANGE); } else if ( rn == 5 ) { painter.setColor(Color.RED); rectangle.expand(50, 100); } else if ( rn == 7 ) { painter.setColor(Color.GREEN); rectangle.expand(100, 50); } else { painter.setColor(Color.MAGENTA); } painter.move(); painter.paint(rectangle); painter.setColor(Color.BLACK); painter.draw(rectangle); } }