import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; import javax.swing.JComponent; // the four tools things we'll use to draw import java.awt.geom.Line2D; // single lines import java.awt.geom.Ellipse2D; // ellipses and circles import java.awt.geom.Rectangle2D; // for the bounding box import java.awt.Rectangle; // squares and rectangles import java.awt.geom.GeneralPath; // combinations of lines and curves import java.awt.geom.AffineTransform; // translation, rotation, scale import java.awt.Shape; // general class for shapes import java.awt.Color; // class for Colors /** A component that draws a Picture by Phill Conrad, with snowmen and coffee cups @author Phill Conrad @version for CS10, S09, UCSB, 05/06/2009 */ public class AbstractedSnowmenCoffeeCupsAndHousesComponent extends JComponent { /** The paintComponent method is always required if you want * any graphics to appear in your JComponent. * */ public void paintComponent(Graphics g) { // Recover Graphics2D--we always do this. // See sections 2.12, p. 60-61 for an explanation Graphics2D g2 = (Graphics2D) g; // Draw a coffee cup. CoffeeCup large = new CoffeeCup(100,75,400,300); CoffeeCup small = new CoffeeCup(20,50,40,30); CoffeeCup tallSkinny = new CoffeeCup(20,150,20,40); CoffeeCup shortFat = new CoffeeCup(20,250,40,20); CoffeeCup ethansCup = new CoffeeCup(100,10,20,25); CoffeeCup diegosCup = new CoffeeCup(150,30,40,10); g2.setColor(Color.RED); g2.draw(large); g2.setColor(Color.GREEN); g2.draw(small); g2.setColor(Color.BLUE); g2.draw(tallSkinny); g2.setColor(Color.MAGENTA); g2.draw(shortFat); g2.setColor(Color.CYAN); g2.draw(ethansCup); g2.setColor(Color.GREEN); g2.draw(diegosCup); Snowman fred = new Snowman(100,460,20,40); g2.setColor(Color.BLACK); g2.draw(fred); g2.setColor(Color.CYAN); Shape s = fred; // now s points to Fred for (int i=0; i<5; i++) { // translating means "moving to a new place" by some change in x and/or y s = ShapeTransforms.translatedCopyOf(s,100,0); // now s is a translated copy of the old s g2.draw(s); } g2.setColor(Color.GREEN); Snowman sheila = new Snowman(200,200,100); g2.draw(sheila); Snowman jim = new Snowman(300,200,60); Snowman jeff = new Snowman(400,200,40); g2.setColor(Color.BLUE); g2.draw(jim); g2.setColor(Color.GREEN.darker()); g2.draw(jeff); for (int i=0; i<10; i++) { // pick a color where the green varies from 0 to 0.9 // red is hard coded at 0.0, and blue is hard coded at 0.5 Color c = new Color((float) 0.0, (float) (i/10.0), (float) 0.5); g2.setColor(c); Snowman personOfSnow = new Snowman(100 + i * 40.0, 380, 30, 70); g2.draw(personOfSnow); } g2.setColor( new Color( (float) 0.2, (float) 0.4, (float) 0.4)); House ourHouse = new House(250,300, 70,50); g2.draw(ourHouse); g2.setColor(Color.PINK); House pinkHouse = new House(500,10, 70,50); s = pinkHouse; // as a tribute to John Cougar Mellencamp, draw some little pink houses for you and me for (int i = 0; i<5; i++) { s = ShapeTransforms.translatedCopyOf(s, 10, 75); // 10 over and 100 down. s = ShapeTransforms.scaledCopyOf(s, 0.9, 0.9); // scale to 90% in both directions g2.draw(s); } } }