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 @author Phill Conrad @version for CS10, S09, UCSB, 05/06/2009 */ // Your class should "extend JComponent // This is "inheritance", which we'll start readina about in Chapter 10 // It means that PhillsPictureComponent "is a" JComponent // that is, a special type of JComponent that is for a specific purpose public class HardCodedCoffeeCupComponent extends JComponent { /** The paintComponent method is always required if you want * any graphics to appear in your JComponent. * * There is a paintComponent * method that is created for you in the JComponent class, but it * doesn't do what we want, so we have to "override" that method with * our own method. * * This overriding is typical when inheritance is used. * In inheritance, you take something that is a "basic" version of * what you want, then you "trick it out" with your own custom features. * Sort of a "pimp my Java class" kind of thing. */ 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. GeneralPath leftSide = new GeneralPath(); // left side of cup leftSide.moveTo(200,400); leftSide.lineTo(160,360); leftSide.lineTo(130,300); leftSide.lineTo(100,200); leftSide.lineTo(100,100); GeneralPath topAndBottom = new GeneralPath(); topAndBottom.moveTo(100,100); topAndBottom.lineTo(500,100); // top of cup topAndBottom.moveTo(200,400); topAndBottom.lineTo(400,400); // bottom of cup g2.draw(leftSide); // This produces a flipped Shape rightSide = ShapeTransforms.horizontallyFlippedCopyOf(leftSide); // after flipping around the upper left hand corner of the bounding box, // we move this over to the right by 400 pixels rightSide = ShapeTransforms.translatedCopyOf(rightSide, 400.0, 0.0); // now we put the whole thing together ino a single path. GeneralPath wholeCup = new GeneralPath(); wholeCup.append(topAndBottom, false); wholeCup.append(leftSide, false); wholeCup.append(rightSide, false); g2.setColor(Color.RED); g2.draw(wholeCup); // we can now make copies of our cup... as many as we like Shape newCup = ShapeTransforms.translatedCopyOf(wholeCup, 50, 50); newCup = ShapeTransforms.scaledCopyOf(newCup, 0.5, 0.5); g2.setColor(Color.GREEN); g2.draw(newCup); Shape tinyCup = ShapeTransforms.scaledCopyOf(newCup, 0.1, 0.1); tinyCup = ShapeTransforms.translatedCopyOf(tinyCup, -80, -80); // move it to 20, 20 // we can make a whole row of these g2.setColor(Color.BLUE); for (int i=0; i<10; i++) { tinyCup = ShapeTransforms.translatedCopyOf(tinyCup, 25, 0); g2.draw(tinyCup); } } }