diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/SimpleGui1.java new file mode 100644 index 0000000..cede391 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Allison Shedden + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("Click me, you tall drink of water") ; + java.awt.Color myColor = new java.awt.Color(102,255,255); + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/AllMyDrawings.java new file mode 100755 index 0000000..526bb0b --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/AllMyDrawings.java @@ -0,0 +1,159 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Allison Shedden + * @version for CS56, lab06, Spring 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few Cereal Bowls + */ + + public static void drawPicture1(Graphics2D g2) { + + CerealBowl c1 = new CerealBowl(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(c1); + + // Make a black bowl that's half the size, + // and moved over 150 pixels in x direction + + Shape c2 = ShapeTransforms.scaledCopyOfLL(c1,0.5,0.5); + c2 = ShapeTransforms.translatedCopyOf(c2,150,0); + g2.setColor(Color.BLACK); g2.draw(c2); + + // Here's a bowl that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + c2 = ShapeTransforms.scaledCopyOfLL(c2,4,4); + c2 = ShapeTransforms.translatedCopyOf(c2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(c2); + + // Draw two Cereal bowls with Chex + + CerealBowlWithChex cc1 = new CerealBowlWithChex(50,350,40,75); + CerealBowlWithChex cc2 = new CerealBowlWithChex(200,350,200,100); + + g2.draw(cc1); + g2.setColor(new Color(0x8F00FF)); g2.draw(cc2); + + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few cereal bowls with and without Chex cereal by Allison Shedden", 20,20); + } + + + /** Draw a picture with a few houses and coffee cups + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some cereal bowls with and without Chex + + CerealBowlWithChex large = new CerealBowlWithChex(100,50,225,150); + CerealBowl smallCB = new CerealBowl(20,50,40,30); + CerealBowlWithChex tallSkinny = new CerealBowlWithChex(20,150,20,40); + CerealBowl shortFat = new CerealBowl(20,250,40,20); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCB); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + CerealBowl c1 = new CerealBowl(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(c1); + + // Make a black bowl that's half the size, + // and moved over 150 pixels in x direction + Shape c2 = ShapeTransforms.scaledCopyOfLL(c1,0.5,0.5); + c2 = ShapeTransforms.translatedCopyOf(c2,150,0); + g2.setColor(Color.BLACK); g2.draw(c2); + + // Here's a cereal bowl that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + c2 = ShapeTransforms.scaledCopyOfLL(c2,4,4); + c2 = ShapeTransforms.translatedCopyOf(c2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(c2); + + // Draw two houses with Windows + + CerealBowlWithChex cc1 = new CerealBowlWithChex(50,350,40,75); + CerealBowlWithChex cc2 = new CerealBowlWithChex(200,350,200,100); + + g2.draw(cc1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape cc3 = ShapeTransforms.rotatedCopyOf(cc2, Math.PI/4.0); + + g2.draw(cc3); + + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few cereal bowls with and without Chex cereal by Allison Shedden", 20,20); + } + + /** Draw a different picture with a few cereal bowls + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A couple of cereal bowls by Allison Shedden", 20,20); + + + // Draw some ceral bowls + + CerealBowl large = new CerealBowl(100,50,225,150); + CerealBowl smallCB = new CerealBowl(20,50,40,30); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCB); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowl.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowl.java new file mode 100755 index 0000000..ef0e4c2 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowl.java @@ -0,0 +1,100 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Cereal Bowl (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Allison Shedden + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class CerealBowl extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CerealBowl + */ + public CerealBowl(double x, double y, double width, double height) + { + // original coordinates for bowl + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + GeneralPath leftSide = new GeneralPath(); + + // left side of bowl + + leftSide.moveTo(200,400); + leftSide.lineTo(160,340); + 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 bowl + + topAndBottom.moveTo(200,400); + topAndBottom.lineTo(400,400); // bottom of bowl + + GeneralPath stripe = new GeneralPath(); + + stripe.moveTo(100,150); + stripe.lineTo(500,150); // top of stripe + + stripe.moveTo(100,200); + stripe.lineTo(500,200); // bottom of stripe + + Shape rightSide = ShapeTransforms.horizontallyFlippedCopyOf(leftSide); + + // flip and move over + + rightSide = ShapeTransforms.translatedCopyOf(rightSide, 400.0, 0.0); + + // now we put the whole thing together ino a single path. + + GeneralPath wholeBowl = new GeneralPath (); + wholeBowl.append(topAndBottom, false); + wholeBowl.append(leftSide, false); + wholeBowl.append(rightSide, false); + wholeBowl.append(stripe, false); + + // translate to the origin + + Shape s = ShapeTransforms.translatedCopyOf(wholeBowl, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable bowl + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowlWithChex.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowlWithChex.java new file mode 100755 index 0000000..eac036c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CerealBowlWithChex.java @@ -0,0 +1,67 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Cereal Bowl with Chex + + @author Allison Shedden + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class CerealBowlWithChex extends CerealBowl implements Shape +{ + /** Constructor */ + public CerealBowlWithChex(double x, double y, double width, double height) + { + // construct the basic cereal bowl shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // dimensions for Chex + + double c = 0.10 * width; + double chexTop = y - 0.10 * height; + double chexHt = 0.10 * height; + + Rectangle2D.Double chex1 = + new Rectangle2D.Double(x + c, chexTop, 1.5 * c, chexHt); + Rectangle2D.Double chex2 = + new Rectangle2D.Double(x + 3.0*c, chexTop, 1.5 * c, chexHt); + Rectangle2D.Double chex3 = + new Rectangle2D.Double(x + 6.0*c, chexTop, 1.5 * c, chexHt); + Rectangle2D.Double chex4 = + new Rectangle2D.Double(x + 8.0*c, chexTop, 1.5 * c, chexHt); + + GeneralPath splash = new GeneralPath(); + + splash.moveTo(x + c, chexTop - 1.5*c); + splash.lineTo(x + c + 10, chexTop - c); // left splash + + splash.moveTo(x + 9.5*c, chexTop - 1.5*c); + splash.lineTo(x + 9.5*c - 10, chexTop - c); // right splash + + // add the Chex to the cereal bowl + + GeneralPath wholeBowl = this.get(); + wholeBowl.append(chex1, false); + wholeBowl.append(chex2, false); + wholeBowl.append(chex3, false); + wholeBowl.append(chex4, false); + wholeBowl.append(splash, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CoffeeCup.java new file mode 100755 index 0000000..c3b9b0a --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/House.java new file mode 100755 index 0000000..c6cde56 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 11, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/HouseWithWindows.java new file mode 100755 index 0000000..a11bb18 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..9925d46 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..1f7c866 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Allison's First Drawing :D"); + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/WritePictureToFile.java new file mode 100755 index 0000000..12ff962 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/Circle.java new file mode 100644 index 0000000..41d7ef1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureComponent.java new file mode 100644 index 0000000..b1a6f45 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureComponent.java @@ -0,0 +1,112 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Allison Shedden + @version for UCSB CS56, S13 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start reading about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius *2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Allison Shedden", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureViewer.java new file mode 100644 index 0000000..70f0bd4 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/allisonshedden/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.allisonshedden.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); + + // Set your own title + frame.setTitle("Allison's Drawing"); + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/george123/SimpleGui1.java new file mode 100644 index 0000000..8063c33 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.george123; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author TODO: George Chen + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me (or not)") ; + java.awt.Color myColor = new java.awt.Color(204,255,153); + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/AllMyDrawings.java new file mode 100755 index 0000000..e9a8776 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/AllMyDrawings.java @@ -0,0 +1,166 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author George Chen + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few balls and Pokeballs + */ + + public static void drawPicture1(Graphics2D g2) { + + Ball b1 = new Ball(70,180,70); + g2.setColor(Color.CYAN); g2.draw(b1); + + // Make a ball that's half the size, + // and moved over 300 pixels in x direction + + Shape b2 = ShapeTransforms.scaledCopyOfLL(b1,0.5,0.5); + b2 = ShapeTransforms.translatedCopyOf(b2,300,0); + g2.setColor(Color.BLACK); g2.draw(b2); + + // Here's a ball that's 4x as big (2x the original) + // and moved over 100 more pixels to right. + b2 = ShapeTransforms.scaledCopyOfLL(b2,4,4); + b2 = ShapeTransforms.translatedCopyOf(b2,100,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(b2); + + // Draw two Pokeballs + + Pokeball pb1 = new Pokeball(50,300,120); + Pokeball pb2 = new Pokeball(150,150,180); + + g2.draw(pb1); + g2.setColor(new Color(0x8F00FF)); g2.draw(pb2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLUE); + g2.drawString("A few balls and Pokeballs by George Chen", 20,20); + } + + + /** Draw a picture with a few balls and Pokeballs + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some Pokeballs. + + Pokeball large = new Pokeball(100,50,180); + Pokeball small = new Pokeball(20,50,50); + Pokeball pb1 = new Pokeball(20,150,80); + Pokeball pb2 = new Pokeball(20,250,80); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(small); + g2.setColor(Color.BLUE); g2.draw(pb1); + g2.setColor(Color.MAGENTA); g2.draw(pb2); + + Ball b1 = new Ball(100,250,50); + g2.setColor(Color.CYAN); g2.draw(b1); + + // Make a ball that's half the size, + // and moved over 250 pixels up. + Shape b2 = ShapeTransforms.scaledCopyOfLL(b1,0.5,0.5); + b2 = ShapeTransforms.translatedCopyOf(b2,0,-250); + g2.setColor(Color.BLACK); g2.draw(b2); + + // Here's a ball that's 4x as big (2x the original) + // and moved over 200 pixels to right and 100 units down. + b2 = ShapeTransforms.scaledCopyOfLL(b2,4,4); + b2 = ShapeTransforms.translatedCopyOf(b2,200,100); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(b2); + + // Draw two Pokeballs + + Pokeball pb4 = new Pokeball(50,350,100); + Pokeball pb5 = new Pokeball(200,250,180); + + g2.draw(pb4); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second Pokeball 45 degrees around its center. + Shape pb3 = ShapeTransforms.rotatedCopyOf(pb5, Math.PI/4.0); + + g2.draw(pb3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLUE); + g2.drawString("A bunch of Pokeballs and a few balls by George Chen", 20,20); + } + + /** Draw a different picture with a bunch of Pokeballs + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A rainbow-colored giant cartwheel Pokeball by George Chen", 20,20); + + Pokeball pb1 = new Pokeball(100, 100, 300); + Shape pb2 = ShapeTransforms.rotatedCopyOf(pb1, Math.PI/2.0); + Shape pb3 = ShapeTransforms.rotatedCopyOf(pb1, Math.PI/4.0); + Shape pb4 = ShapeTransforms.rotatedCopyOf(pb1, ((3 * Math.PI)/4.0)); + + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + Stroke orig = g2.getStroke(); + g2.setStroke(thick); + + g2.setColor(Color.GREEN); + g2.draw(pb1); + g2.setColor(Color.RED); + g2.draw(pb3); + g2.setColor(Color.MAGENTA); + g2.draw(pb4); + g2.setColor(Color.BLUE); + g2.draw(pb2); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Ball.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Ball.java new file mode 100644 index 0000000..51b3a7d --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Ball.java @@ -0,0 +1,53 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Ellipse2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a ball that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author George Chen + @version for CS56, Winter 14, UCSB + +*/ +public class Ball extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of center of ball + @param y y coord of center of ball + @param radius radius of ball + */ + public Ball(double x, double y, double radius) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, radius. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + Ellipse2D.Double outerBall = + new Ellipse2D.Double(x, y, radius, radius); + + GeneralPath wholeBall = this.get(); + wholeBall.append(outerBall, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..12e2be4 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..8275f92 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("George Chen's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Pokeball.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Pokeball.java new file mode 100644 index 0000000..5058b2c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/Pokeball.java @@ -0,0 +1,56 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Ellipse2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Pokeball + + @author George Chen + @version for CS56, W14, UCSB, 02/23/2011 + +*/ +public class Pokeball extends Ball implements Shape +{ + /** + * Constructor for objects of class Pokeball + */ + public Pokeball(double x, double y, double radius) + { + // construct the basic ball shell + super(x,y,radius); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + double r1 = 0.15 * radius; + double r2 = 0.25 * radius; + + Ellipse2D.Double CenterCircle1 = + new Ellipse2D.Double((x + ((0.5 * radius) - (0.5 * r1))), (y + ((0.5 * radius) - (0.5 * r1))), r1, r1 ); + Ellipse2D.Double CenterCircle2 = + new Ellipse2D.Double((x + ((0.5 * radius) - (0.5 * r2))), (y + ((0.5 * radius) - (0.5 * r2))), r2, r2 ); + Line2D.Double Line1 = + new Line2D.Double(x, (y + (0.5 * radius)), (x + ((0.5 * radius) - (0.5 * r2))), (y + (0.5 * radius)) ); + Line2D.Double Line2 = + new Line2D.Double((x + ((0.5 * radius) + (0.5 * r2))) , (y + (0.5 * radius)), x + radius, (y + (0.5 * radius))); + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(CenterCircle1, false); + wholeHouse.append(CenterCircle2, false); + wholeHouse.append(Line1, false); + wholeHouse.append(Line2, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/WritePictureToFile.java new file mode 100755 index 0000000..0a0f50c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.george123.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/george123/simple/Circle.java new file mode 100644 index 0000000..4f000d7 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.george123.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureComponent.java new file mode 100644 index 0000000..300f72d --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureComponent.java @@ -0,0 +1,118 @@ +package edu.ucsb.cs56.w14.drawings.george123.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author George Chen (fixed the snowmans's head) + @version for UCSB CS56, S13 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by George Chen", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureViewer.java new file mode 100644 index 0000000..145d949 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/george123/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.george123.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/gmak/SimpleGui1.java new file mode 100644 index 0000000..59c1440 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/SimpleGui1.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.gmak; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Gary Mak + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("give me the mouse") ; + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + + java.awt.Color newColor = new java.awt.Color(200,150,150); + button.setBackground(newColor); + button.setOpaque(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/AllMyDrawings.java new file mode 100755 index 0000000..86b8669 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/AllMyDrawings.java @@ -0,0 +1,163 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Phill Conrad + * @version for CS10, lab06, Spring 2009 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a ticTacToeTables + */ + + public static void drawPicture1(Graphics2D g2) { + + TicTacToeTable h1 = new TicTacToeTable(100,250,50); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black ticTacToeTable that's half the size, + // and moved over 150 pixels in x direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a ticTacToeTable that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two filledInTicTacToeTable + + FilledInTicTacToeTable hw1 = new FilledInTicTacToeTable(50,350,40); + FilledInTicTacToeTable hw2 = new FilledInTicTacToeTable(200,350,200); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); g2.draw(hw2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few TicTacToeTables by Gary Mak", 20,20); + } + + + /** Draw a picture with a few TicTacToeTables + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some TicTacToeTables + + TicTacToeTable large = new TicTacToeTable(100,50,225); + TicTacToeTable smallCC = new TicTacToeTable(20,50,40); + TicTacToeTable tallSkinny = new TicTacToeTable(20,150,20); + TicTacToeTable shortFat = new TicTacToeTable(20,250,40); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + FilledInTicTacToeTable h1 = new FilledInTicTacToeTable(100,250,50); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black table that's half the size, + // and moved over 150 pixels in x direction + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a table that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw FilledInTicTacToeTables + + FilledInTicTacToeTable hw1 = new FilledInTicTacToeTable(50,350,40); + FilledInTicTacToeTable hw2 = new FilledInTicTacToeTable(200,350,200); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second filledInTable 45 degrees around its center. + Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI/4.0); + + g2.draw(hw3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of TicTacToeTables by Gary Mak", 20,20); + } + + /** Draw a different picture with a TicTacToeTables and FilledInTicTacToeTables + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of TicTacToeTables by Gary Mak", 20,20); + + + // Draw some tables. + + TicTacToeTable table1 = new TicTacToeTable(200,300,225); + FilledInTicTacToeTable table2 = new FilledInTicTacToeTable(300,400,40); + FilledInTicTacToeTable table3 = new FilledInTicTacToeTable(400,300,100); + + g2.setColor(Color.RED); g2.draw(table1); + g2.setColor(Color.GREEN); g2.draw(table2); + g2.setColor(Color.BLUE); g2.draw(table3); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/Circle.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/Circle.java new file mode 100644 index 0000000..455aaef --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/CoffeeCup.java new file mode 100755 index 0000000..76b30d6 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/FilledInTicTacToeTable.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/FilledInTicTacToeTable.java new file mode 100755 index 0000000..8379a31 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/FilledInTicTacToeTable.java @@ -0,0 +1,94 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A TicTacToeTable filled with X's and O's + + @author Gary Mak + @version for CS56, W14, UCSB, 02/20/2014 + +*/ +public class FilledInTicTacToeTable extends TicTacToeTable implements Shape +{ + /** + * Constructor for objects of class FilledInTicTacToeTable + */ + public FilledInTicTacToeTable(double x, double y, double side) + { + // construct the basic TicTacToeTable + super(x,y,side); + + + GeneralPath wholeTable = this.get(); + + //Filling in with X's and O's + // O's are a circle while X's are 2 separate lines + + Circle bottomLeft = + new Circle(x + side / 6, y - side / 6, side / 6); + Circle bottomRight = + new Circle(x + 5 * side / 6, y - side / 6, side / 6); + Circle middleMiddle = + new Circle(x + 3 * side / 6, y - 3 * side / 6, side / 6); + Circle middleRight = + new Circle(x + 5 * side / 6, y - 3 * side / 6 , side / 6); + Circle topMiddle = + new Circle(x + 3 * side / 6, y - 5 * side / 6, side / 6); + + Line2D.Double bottomMiddleLeftToRight = + new Line2D.Double(x + 2 * side / 6, y - 2 * side / 6, x + 4 * side / 6, y); + Line2D.Double bottomMiddleRightToLeft = + new Line2D.Double(x + 4 * side / 6, y - 2 * side / 6, x + 2 * side / 6, y); + + Line2D.Double topLeftLeftToRight = + new Line2D.Double(x , y - side, x + 2 * side / 6 , y - 4 * side / 6); + Line2D.Double topLeftRightToLeft = + new Line2D.Double(x + 2 * side / 6, y - side, x , y - 4 * side / 6); + + Line2D.Double topRightLeftToRight = + new Line2D.Double(x + 4 * side / 6, y - side, x + side, y - 4 * side / 6); + Line2D.Double topRightRightToLeft = + new Line2D.Double(x + side, y - side, x + 4 * side / 6, y - 4 * side / 6); + + Line2D.Double middleLeftLeftToRight = + new Line2D.Double(x, y - 4 * side / 6, x + 2 * side / 6, y - 2 * side / 6); + Line2D.Double middleLeftRightToLeft = + new Line2D.Double(x + 2 * side / 6, y - 4 * side / 6, x, y - 2 * side / 6); + + + //circle's + wholeTable.append(bottomLeft,false); + wholeTable.append(bottomRight,false); + wholeTable.append(middleMiddle,false); + wholeTable.append(middleRight,false); + wholeTable.append(topMiddle,false); + + //X's + wholeTable.append(bottomMiddleLeftToRight,false); + wholeTable.append(bottomMiddleRightToLeft,false); + + wholeTable.append(topLeftLeftToRight,false); + wholeTable.append(topLeftRightToLeft,false); + + wholeTable.append(topRightLeftToRight,false); + wholeTable.append(topRightRightToLeft,false); + + wholeTable.append(middleLeftLeftToRight,false); + wholeTable.append(middleLeftRightToLeft,false); + + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/House.java new file mode 100755 index 0000000..c91d16c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 11, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/HouseWithWindows.java new file mode 100755 index 0000000..8cc6db8 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..8d4fe79 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..9f465b1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Gary's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/TicTacToeTable.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/TicTacToeTable.java new file mode 100755 index 0000000..f513f99 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/TicTacToeTable.java @@ -0,0 +1,63 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a TicTacToeTable that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Gary Mak + @version for CS56, Winter 14, UCSB + +*/ +public class TicTacToeTable extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of table + @param y y coord of lower left corner of table + @param side length/height of table + */ + public TicTacToeTable(double x, double y, double side) + { + //rectangle that encompasses the whole table + Rectangle2D.Double wholeTable = + new Rectangle2D.Double(x,y - side ,side ,side); + + //lines that separate the table into 3 columns + Line2D.Double leftColumn = + new Line2D.Double(x + side / 3, y, x + side / 3, y-side); + Line2D.Double rightColumn = + new Line2D.Double(x + 2 * side / 3, y, x + 2 * side / 3, y-side); + + //lines that separate the table into 3 rows + Line2D.Double topRow = + new Line2D.Double(x , y - 2 * side / 3 , x + side , y - 2 * side / 3); + Line2D.Double bottomRow = + new Line2D.Double(x , y - side / 3 , x + side , y - side / 3); + + + //appends all lines together to a drawing + GeneralPath wholeTicTacToeTable = this.get(); + wholeTicTacToeTable.append(wholeTable,false); + wholeTicTacToeTable.append(leftColumn,false); + wholeTicTacToeTable.append(rightColumn,false); + wholeTicTacToeTable.append(topRow,false); + wholeTicTacToeTable.append(bottomRow,false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/WritePictureToFile.java new file mode 100755 index 0000000..18beaaa --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.gmak.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/Circle.java new file mode 100644 index 0000000..433b201 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.gmak.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureComponent.java new file mode 100644 index 0000000..dda666b --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureComponent.java @@ -0,0 +1,114 @@ +package edu.ucsb.cs56.w14.drawings.gmak.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Gary Mak (fixed the snowmans's head) + @version for UCSB CS56, S13 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw(house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Gary Mak", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureViewer.java new file mode 100644 index 0000000..a1cb1ea --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/gmak/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.gmak.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/SimpleGui1.java new file mode 100644 index 0000000..9bf2bac --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.jennyso; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Jenny So + @version CS56, Winter 2014, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me, please?") ; + java.awt.Color myColor = new java.awt.Color(204,255,153); // R, G, B values. + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/AllMyDrawings.java new file mode 100755 index 0000000..c047a59 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/AllMyDrawings.java @@ -0,0 +1,158 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Phill Conrad + * @author Jenny So + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few houses + */ + + public static void drawPicture1(Graphics2D g2) { + + Cat h1 = new Cat(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black cat that's half the size, + // and moved over 150 pixels in x direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a cat that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two cats with top hats + + FancyCat hw3 = new FancyCat(100,600,100,100); + FancyCat hw4 = new FancyCat(200,350,200,300); + + g2.draw(hw3); + g2.setColor(new Color(0x8F00FF)); g2.draw(hw4); + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few cats by Jenny So", 20,20); + } + + + /** Draw a picture with a few houses and coffee cups + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some cats. + + Cat large = new Cat(100,50,225,150); + Cat smallCC = new Cat(20,50,40,30); + Cat tallSkinny = new Cat(20,150,20,40); + Cat shortFat = new Cat(20,250,40,20); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + Cat h1 = new Cat(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black cat that's half the size, + // and moved over 150 pixels in x direction + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a cat that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two cats with top hats + + FancyCat hw1 = new FancyCat(50,350,40,75); + FancyCat hw2 = new FancyCat(200,350,200,100); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second cat 45 degrees around its center. + Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI/4.0); + + g2.draw(hw3); + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of Cats by Jenny So", 20,20); + } + + /** Draw a different picture with a few cats + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of Cats by Jenny So", 20,20); + + + // Draw some coffee cups. + + Cat large = new Cat(300,200,225,150); + Cat smallC = new Cat(20,50,40,30); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallC); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/Cat.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/Cat.java new file mode 100755 index 0000000..c5e3333 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/Cat.java @@ -0,0 +1,124 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; +import java.awt.geom.Ellipse2D; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a cat that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @author Jenny So + @version for CS56, Winter 14, UCSB + +*/ +public class Cat extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of bottom middle of the Cat + @param y y coord of bottom middle of the Cat + @param width width of the house + @param height of house (from bottom to top of cat ears) + */ + public Cat(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double earsHeight = height*.30; + double eyesHeight = height*.55; + double whiskerHeight = height*.2; + + // Make eyes + + Rectangle2D.Double rightEye = + new Rectangle2D.Double(x-width/5, y-eyesHeight , + width*.1, height*.1); + Rectangle2D.Double leftEye = + new Rectangle2D.Double(x+width/10, y-eyesHeight, width*.1, height*.1); + + // make the ears. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftEar1 = + new Line2D.Double (x-width/2.0, y-height, + x-width/2.0, y-height+earsHeight); + + Line2D.Double rightEar1 = + new Line2D.Double (x - width/2.0, y-height, + x-width/2.0 + width*.1, y - height + height*.1); + + Line2D.Double leftEar2 = + new Line2D.Double (x+width/2.0, y-height, + x+width/2.0, y-height+earsHeight); + + Line2D.Double rightEar2 = + new Line2D.Double (x + width/2.0, y-height, + x+width/2.0 - width*.1, y - height + height*.1); + + // make the whiskers + + Line2D.Double bottomRightWhisker = + new Line2D.Double (x-width/4.0, y-whiskerHeight, + x-width/1.5, y-whiskerHeight); + Line2D.Double middleRightWhisker = + new Line2D.Double (x-width/4.0, y-height*.4, + x-width/1.5, y-height*.4); + Line2D.Double topRightWhisker = + new Line2D.Double (x-width/4.0, y-height*.3, + x-width/1.5, y-height*.3); + + Line2D.Double bottomLeftWhisker = + new Line2D.Double (x+width/4.0, y-whiskerHeight, + x+width/1.5, y-whiskerHeight); + Line2D.Double middleLeftWhisker = + new Line2D.Double (x+width/4.0, y-height*.4, + x+width/1.5, y-height*.4); + Line2D.Double topLeftWhisker = + new Line2D.Double (x+width/4.0, y-height*.3, + x+width/1.5, y-height*.3); + + // make the face + Ellipse2D.Double face = + new Ellipse2D.Double (x-width/2.0, y-height, + width, height); + + // put the whole house together + + GeneralPath wholeCat = this.get(); + wholeCat.append(rightEye, false); + wholeCat.append(leftEye, false); + wholeCat.append(rightEar1, false); + wholeCat.append(leftEar1, false); + wholeCat.append(rightEar2, false); + wholeCat.append(leftEar2, false); + wholeCat.append(bottomRightWhisker, false); + wholeCat.append(middleRightWhisker, false); + wholeCat.append(topRightWhisker, false); + wholeCat.append(bottomLeftWhisker, false); + wholeCat.append(middleLeftWhisker, false); + wholeCat.append(topLeftWhisker,false); + wholeCat.append(face, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/FancyCat.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/FancyCat.java new file mode 100755 index 0000000..d9718e8 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/FancyCat.java @@ -0,0 +1,60 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class FancyCat extends Cat implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public FancyCat(double x, double y, double width, double height) + { + // construct the basic cat shell + super(x,y,width*.75,height*.75); + + //modify cat head dimensions to accomodate hat + double headHeight = height*.75; + double headWidth = width*.75; + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + double brimWidth = 0.6 * width; + double hatHeight = .25*height; + double hatWidth = 0.3*width; + + Line2D.Double brim = + new Line2D.Double(x - brimWidth/2.0, y-headHeight, + x + brimWidth/2.0, y-headHeight); + Rectangle2D.Double hat = + new Rectangle2D.Double(x-hatWidth/2.0, y-height, + hatWidth, hatHeight); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeCat = this.get(); + wholeCat.append(brim, false); + wholeCat.append(hat, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..30024a0 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..5d132f8 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/WritePictureToFile.java new file mode 100755 index 0000000..03de700 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/Circle.java new file mode 100644 index 0000000..5d50669 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureComponent.java new file mode 100644 index 0000000..740a0a1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureComponent.java @@ -0,0 +1,113 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Jenny So (fixed the snowmans's head) + @version for UCSB CS56, W14 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - 2*(bottomRadius + middleRadius) - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Jenny So", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureViewer.java new file mode 100644 index 0000000..6e4dfd1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/jennyso/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.jennyso.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Jenny's Drawing"); + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/SimpleGui1.java new file mode 100644 index 0000000..0334cc4 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author TODO: Add additional author here + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me, OR ELSE!") ; + java.awt.Color myColor = new java.awt.Color(102,153,204); + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/AllMyDrawings.java new file mode 100755 index 0000000..6239fbb --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/AllMyDrawings.java @@ -0,0 +1,162 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Phill Conrad + * @author Logan Schmidt + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few triangles and triforces + */ + + public static void drawPicture1(Graphics2D g2) { + + Triangle t1 = new Triangle(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(t1); + + // Make a black house that's half the size, + // and moved over 150 pixels in x direction + + Shape t2 = ShapeTransforms.scaledCopyOfLL(t1,0.5,0.5); + t2 = ShapeTransforms.translatedCopyOf(t2,150,0); + g2.setColor(Color.BLACK); g2.draw(t2); + + // Here's a house that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + t2 = ShapeTransforms.scaledCopyOfLL(t2,4,4); + t2 = ShapeTransforms.translatedCopyOf(t2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(t2); + + // Draw two houses with Windows + + Triforce tf1= new Triforce(50,350,40,75); + Triforce tf2= new Triforce(200,350,200,100); + + g2.draw(tf1); + g2.setColor(new Color(0x8F00FF)); g2.draw(tf2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("Triangles and Triforces by Logan Schmidt", 20,20); + } + + + /** Draw a picture with a triangles and triforces + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some triforces. + + Triforce large = new Triforce(100,450,225,150); + Triforce smallCC = new Triforce(20,50,40,30); + Triforce tallSkinny = new Triforce(20,150,20,40); + Triforce shortFat = new Triforce(20,250,40,20); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + Triangle t1 = new Triangle(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(t1); + + // Make a black house that's half the size, + // and moved over 150 pixels in x direction + Shape t2 = ShapeTransforms.scaledCopyOfLL(t1,0.5,0.5); + t2 = ShapeTransforms.translatedCopyOf(t2,150,0); + g2.setColor(Color.BLACK); g2.draw(t2); + + // Here's a house that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + t2 = ShapeTransforms.scaledCopyOfLL(t2,4,4); + t2 = ShapeTransforms.translatedCopyOf(t2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(t2); + + // Draw two houses with Windows + + Triforce tf1 = new Triforce(50,350,40,75); + Triforce tf2 = new Triforce(200,350,200,100); + + g2.draw(tf1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape tf3 = ShapeTransforms.rotatedCopyOf(tf2, Math.PI/4.0); + + g2.draw(tf3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of Triangles and Triforces by Logan Schmidt", 20,20); + } + + /** Draw a different picture with a few houses and coffee cups + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of Triforces by Logan Schmidt", 20,20); + + + // Draw some triforces. + + Triforce large = new Triforce(100,450,225,150); + Triforce smallCC = new Triforce(20,50,40,30); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..67984a4 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..314ad55 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triangle.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triangle.java new file mode 100755 index 0000000..6df5b51 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triangle.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a triangle that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @author Logan Schmidt + @version for CS56, Winter 14, UCSB + +*/ +public class Triangle extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of triangle + @param y y coord of lower left corner of triangle + @param width width of the triangle from lower left corner to lower right corner + @param height of triangle from bottom to upper point + */ + public Triangle(double x, double y, double width, double height) + { + //Draws each line of the triangle + Line2D.Double leftLine = new Line2D.Double(x, y, x + (width/2), y - height); + Line2D.Double rightLine = new Line2D.Double(x + (width/2), y - height, x + width, y); + Line2D.Double bottomLine = new Line2D.Double(x, y, x + width, y); + + //Adds to general path + GeneralPath wholeTriangle = this.get(); + wholeTriangle.append(leftLine, false); + wholeTriangle.append(rightLine, false); + wholeTriangle.append(bottomLine, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triforce.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triforce.java new file mode 100755 index 0000000..15ebe57 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/Triforce.java @@ -0,0 +1,54 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Triforce + + @author Phill Conrad + @author Logan Schmidt + @version for CS56, W14, UCSB, 02/28/2014 + +*/ +public class Triforce extends Triangle implements Shape +{ + /** + * Constructor for objects of class Triforce + */ + public Triforce(double x, double y, double width, double height) + { + // construct the basic triangle + super(x,y,width,height); + + //Calculate the midpoints of the lines of the basic triangle + double xMidPointLeft = (x+(x+(width/2)))/2; + double yMidPointLeft = (y+(y-height))/2; + double xMidPointRight = ((x+(width/2))+(x+width))/2; + double yMidPointRight = ((y-height)+y)/2; + double xMidPointBottom = (x+(x+width))/2; + double yMidPointBottom = (y+y)/2; + + //Draw inverted triangle within the basic triangle using midpoints + Line2D.Double leftLine2 = new Line2D.Double(xMidPointLeft, yMidPointLeft, xMidPointBottom, yMidPointBottom); + Line2D.Double rightLine2 = new Line2D.Double(xMidPointBottom, yMidPointBottom, xMidPointRight, yMidPointRight); + Line2D.Double topLine = new Line2D.Double(xMidPointLeft, yMidPointLeft, xMidPointRight, yMidPointRight); + + //Add to general path + GeneralPath wholeTriforce = this.get(); + wholeTriforce.append(leftLine2, false); + wholeTriforce.append(rightLine2, false); + wholeTriforce.append(topLine, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/WritePictureToFile.java new file mode 100755 index 0000000..52cdbcf --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/Circle.java new file mode 100644 index 0000000..faa6daa --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureComponent.java new file mode 100644 index 0000000..522e5bd --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureComponent.java @@ -0,0 +1,116 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Logan Schmidt (fixed the snowmans's head) + @version for UCSB CS56, S13 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Logan Schmidt", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureViewer.java new file mode 100644 index 0000000..c98c19b --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/l_y_s/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.l_y_s.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/SimpleGui1.java new file mode 100644 index 0000000..1511205 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author TODO: Add additional author here + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me you gorgeous creature") ; + java.awt.Color myColor = new java.awt.Color(204,255,153); // R, G, B values. + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/AllMyDrawings.java new file mode 100755 index 0000000..dec0e61 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/AllMyDrawings.java @@ -0,0 +1,159 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Mena Iskander + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few swords + */ + + public static void drawPicture1(Graphics2D g2) { + + Blade h1 = new Blade(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black blade that's half the size, + // and moved over 150 pixels in x direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a blade that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + //Create two swords + Sword hw1 = new Sword(50,350,40,75); + Sword hw2 = new Sword(200,50,100,100); + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); g2.draw(hw2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few swords by Mena Iskander", 20,20); + } + + + /** Draw a picture with a few blades and swords + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some swords + + Sword large = new Sword(100,50,225,150); + Sword smallCC = new Sword(20,50,40,30); + Sword tallSkinny = new Sword(20,150,20,40); + Sword shortFat = new Sword(20,250,40,20); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + Blade h1 = new Blade(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black blade that's half the size, + // and moved over 150 pixels in x direction + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a blade that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two more swords + + Sword hw1 = new Sword(50,350,40,75); + Sword hw2 = new Sword(200,350,200,100); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI/4.0); + + g2.draw(hw3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of Swords and a few blades by Mena Iskander", 20,20); + } + + /** Draw a different picture with a few blades and swords + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of Swords by Mena Iskander", 20,20); + + + // Draw some swords + Sword large = new Sword(100,50,225,150); + Sword smallCC = new Sword(20,50,40,30); + + //Set colors of swords + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Blade.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Blade.java new file mode 100644 index 0000000..d7f36c9 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Blade.java @@ -0,0 +1,79 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A drawing of a blade that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Mena Iskander + @version for CS56, Winter 14, UCSB + +*/ +public class Blade extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of blade + @param y y coord of lower left corner of blade + @param width width of the blade + @param height of blade + */ + public Blade(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double bladeHeight = .75 * height; + double tipHeight = height - bladeHeight; + + double tipUpperLeftY = y + tipHeight; + + // Make the body of the blade + + Rectangle2D.Double bladeBody = + new Rectangle2D.Double(x, tipUpperLeftY , + width, bladeHeight); + + + // make the tip of the blade. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftTip = + new Line2D.Double (x, y + tipHeight, + x + width/2.0, y); + + Line2D.Double rightTip = + new Line2D.Double (x + width/2.0, y, + x + width, y + tipHeight); + + Line2D.Double middle = new Line2D.Double (x+width/2, y, x+width/2,y+height ); + // put the blade together + + GeneralPath wholeBlade = this.get(); + wholeBlade.append(bladeBody, false); + wholeBlade.append(leftTip, false); + wholeBlade.append(rightTip, false); + wholeBlade.append(middle, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/CoffeeCup.java new file mode 100755 index 0000000..398c281 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/House.java new file mode 100755 index 0000000..3f3ab87 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 11, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/HouseWithWindows.java new file mode 100755 index 0000000..2d5e028 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..ef2687c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..50b8c1e --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author Mena Iskander + * @version for UCSB CS56, W14, 02/27/2014 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Mena's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Sword.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Sword.java new file mode 100644 index 0000000..f61890e --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/Sword.java @@ -0,0 +1,58 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Sword by Mena Iskander + + @author Mena Iskander + @version for CS56, W14, UCSB, 02/27/2014 + +*/ +public class Sword extends Blade implements Shape +{ + /** + * Constructor for objects of class Sword + */ + public Sword(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + //create the guard of the sword + + double guardWidth = 2 * width; + double guardHeight = 0.1 * height; + + Rectangle2D.Double guard = + new Rectangle2D.Double(x-width/2, y+height, guardWidth, guardHeight); + + //create the handle of the guard + + double handleWidth=width*.3; + double handleHeight=.6*height; + + Rectangle2D.Double handle = new Rectangle2D.Double((x+width/2)-handleWidth/2, y+height,handleWidth, handleHeight); + + //Put the sword together + + GeneralPath wholeSword = this.get(); + wholeSword.append(guard, false); + wholeSword.append(handle, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/WritePictureToFile.java new file mode 100755 index 0000000..01b97fb --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 bar"); + System.out.println(" produces bar.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/Circle.java new file mode 100644 index 0000000..02ce896 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureComponent.java new file mode 100644 index 0000000..a0fcfa0 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureComponent.java @@ -0,0 +1,115 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Mena Iskander (fixed the snowmans's head) + @version for UCSB CS56, W14 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 3.5 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Mena Iskander", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureViewer.java new file mode 100644 index 0000000..9dafd3d --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/m_iskander/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.m_iskander.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Mena's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/SimpleGui1.java new file mode 100644 index 0000000..d722a66 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Natasha Issayeva + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click pls") ; + java.awt.Color myColor = new java.awt.Color(204,255,153); + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AllMyDrawings.java new file mode 100755 index 0000000..6b5bbf8 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AllMyDrawings.java @@ -0,0 +1,161 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Natasha Issayeva + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few mushrooms + */ + + public static void drawPicture1(Graphics2D g2) { + + Mushroom m1 = new Mushroom(100,250,75,50); + g2.setColor(Color.CYAN); g2.draw(m1); + + // Make a black musrhoom that's half the size, + // and moved over 150 pixels in x direction + + Shape m2 = ShapeTransforms.scaledCopyOfLL(m1,0.5,0.5); + m2 = ShapeTransforms.translatedCopyOf(m2,150,0); + g2.setColor(Color.BLACK); g2.draw(m2); + + // Here's a mushroom that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + m2 = ShapeTransforms.scaledCopyOfLL(m2,4,4); + m2 = ShapeTransforms.translatedCopyOf(m2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(m2); + + // Draw two Mario mushrooms + + MarioMushroom mm1 = new MarioMushroom(50,350,75,40); + MarioMushroom mm2 = new MarioMushroom(200,350,200,100); + + g2.draw(mm1); + g2.setColor(new Color(0x8F00FF)); g2.draw(mm2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few mushrooms by Natasha Issayeva", 20,20); + } + + + // Draw a picture with a few mushrooms and Mario mushrooms + + public static void drawPicture2(Graphics2D g2) { + + // Draw some mushrooms. + + Mushroom large = new Mushroom(100,50,225,150); + Mushroom small = new Mushroom(20,50,40,30); + Mushroom tallSkinny = new Mushroom(20,150,20,40); + Mushroom shortFat = new Mushroom(20,250,40,20); + + 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); + + MarioMushroom m1 = new MarioMushroom(100,250,75,50); + g2.setColor(Color.CYAN); g2.draw(m1); + + // Make a black mushroom that's half the size, + // and moved over 150 pixels in x direction + Shape m2 = ShapeTransforms.scaledCopyOfLL(m1,0.5,0.5); + m2 = ShapeTransforms.translatedCopyOf(m2,150,0); + g2.setColor(Color.BLACK); g2.draw(m2); + + // Here's a mushrooom that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + m2 = ShapeTransforms.scaledCopyOfLL(m2,4,4); + m2 = ShapeTransforms.translatedCopyOf(m2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(m2); + + // Draw two Mario mushrooms + + MarioMushroom mm1 = new MarioMushroom(50,350,75,40); + MarioMushroom mm2 = new MarioMushroom(200,350,200,100); + + g2.draw(mm1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape mm3 = ShapeTransforms.rotatedCopyOf(mm2, Math.PI/4.0); + + g2.draw(mm3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of mushrooms by Natasha Issayeva", 20,20); + } + + // Draw a different picture with a few mushrooms and Mario mushrooms + + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of mushrooms by Natasha Issayeva ", 20,20); + + + // Draw some mushrooms + + Mushroom large = new Mushroom(100,300,225,150); + MarioMushroom small = new MarioMushroom(20,100,40,30); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(small); + } + + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AnimatedPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AnimatedPictureViewer.java new file mode 100644 index 0000000..534f210 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/AnimatedPictureViewer.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class AnimatedPictureViewer { + + private DrawPanel panel = new DrawPanel(); + + private Face face = new Face(100, 100, 100); + + Thread anim; + + private int x = 100; + private int y = 100; + + private int dx = 5; + private int dy = 5; + + + public static void main (String[] args) { + new AnimatedPictureViewer().go(); + } + + public void go() { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.getContentPane().add(panel); + frame.setSize(640,480); + frame.setVisible(true); + + frame.getContentPane().addMouseListener(new MouseAdapter() { + public void mouseEntered(MouseEvent e){ + System.out.println("mouse entered"); + anim = new Animation(); + anim.start(); + } + + public void mouseExited(MouseEvent e){ + System.out.println("Mouse exited"); + // Kill the animation thread + anim.interrupt(); + while (anim.isAlive()){} + anim = null; + panel.repaint(); + } + }); + + } // go() + class DrawPanel extends JPanel { + public void paintComponent(Graphics g) { + + Graphics2D g2 = (Graphics2D) g; + + // Clear the panel first + g2.setColor(Color.white); + g2.fillRect(0,0,this.getWidth(), this.getHeight()); + + // Draw the Face + Face test = new Face(x, y, 100); + g2.setColor(Color.black); + + g2.draw(test); + } + } + + class Animation extends Thread { + public void run() { + try { + while (true) { + // Bounce off the walls + if (x >= 550) {dx = -5;} + if (x <= 70) {dx = 5;} + if (y >= 300) {dy = -5;} + if (y <= 0) {dy = 5;} + + x += dx; + y += dy; + panel.repaint(); + Thread.sleep(50); + } + } catch(Exception ex) { + if (ex instanceof InterruptedException) { + // Do nothing - expected on mouseExited + } else { + ex.printStackTrace(); + System.exit(1); + } + } + } + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Circle.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Circle.java new file mode 100644 index 0000000..8ae4b83 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Face.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Face.java new file mode 100644 index 0000000..0f6d2da --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Face.java @@ -0,0 +1,55 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + + +import java.awt.Rectangle; +import java.awt.Shape; // general class for shapes +import java.awt.geom.AffineTransform; // translation, rotation, scale +import java.awt.geom.GeneralPath; // combinations of lines and curves +import java.awt.geom.Line2D; +import java.awt.geom.PathIterator; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Ellipse2D; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a Face that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Natasha Issayeva + @version for CS56, W14, UCSB, 03/13/2014 + +*/ +public class Face extends GeneralPathWrapper implements Shape +{ + /** + Constructor + @param x the coordinate of the top + @param y the coordinate of the top + @param radius radius of face + */ + public Face(double x, double y, double radius) + { + Ellipse2D.Double head = new Ellipse2D.Double(x-radius, y, radius*2, radius*2 ); + Ellipse2D.Double leftEye = new Ellipse2D.Double(x-radius/2, y+ radius/2, radius/7, radius/7); + Ellipse2D.Double rightEye = new Ellipse2D.Double(x+radius/2 - 2*radius/7, y+ radius/2, radius/7, radius/7); + Line2D.Double topMouth = new Line2D.Double(x-radius/2, y+ 3*radius/4, x+radius/2, y+3*radius/4); + Line2D.Double leftSide = new Line2D.Double(x-radius/2, y+3*radius/4, x-radius/5, y+7*radius/8); + Line2D.Double rightSide = new Line2D.Double(x+radius/5, y+7*radius/8, x+radius/2, y+3*radius/4); + Line2D.Double bottomMouth = new Line2D.Double(x-radius/5, y+7*radius/8, x+radius/5, y+7*radius/8); + + GeneralPath face = this.get(); + face.append(head, false); + face.append(leftEye, false); + face.append(rightEye, false); + face.append(topMouth, false); + face.append(leftSide, false); + face.append(rightSide, false); + face.append(bottomMouth, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MarioMushroom.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MarioMushroom.java new file mode 100755 index 0000000..4519876 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MarioMushroom.java @@ -0,0 +1,64 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; +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.Graphics; +import java.awt.Graphics2D; +import javax.swing.JPanel; +import javax.swing.JComponent; + + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Ellipse2D; // ellipses and circles +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Mushroom with spots on the top + + @author Natasha Issayeva + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class MarioMushroom extends Mushroom implements Shape +{ + /** + * Constructor for objects of class Mushroom + * @param x x coordinate of lower left corner of mushroom top + * @param y y coordinate of lower left corner of mushroom top + * @param width width of mushroom top + * @param height height of whole mushroom + */ + public MarioMushroom(double x, double y, double width, double height) + { + // construct the basic mushroom + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + Circle spot1 = + new Circle(x + (2*width)/9, y - (2*height)/9, width/9); + Circle spot2 = + new Circle(x + (3*width)/5, y - height/3, width/6); + Circle spot3 = + new Circle(x + width/2, y - (11*height)/12, height/12); + + // add the spots to the mushroom + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeMushroom = this.get(); + wholeMushroom.append(spot1, false); + wholeMushroom.append(spot2, false); + wholeMushroom.append(spot3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..1361b52 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Natasha Issayeva + + @author Natasha Issayeva (original drawing) + @version CS56, Winter 2014, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..c77b8de --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author Natasha Issayeva + * @version for UCSB CS56, W14, 02/23/2014 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Natasha's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Mushroom.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Mushroom.java new file mode 100755 index 0000000..3c3769a --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/Mushroom.java @@ -0,0 +1,83 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; +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.Graphics; +import java.awt.Graphics2D; +import javax.swing.JPanel; +import javax.swing.JComponent; + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Ellipse2D; // ellipses and circles +import java.awt.geom.Line2D; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Mushroom (wrapper around a General Path, implements Shape) + + @author Natasha Issayeva + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class Mushroom extends GeneralPathWrapper implements Shape +{ + + + /** + Constructor for objects of class Mushroom + + @param x x coordinate of lower left corner of mushroom top + @param y y coordinate of lower left corner of mushroom top + @param width width of mushroom top + @param height height of whole mushroom + */ + + public Mushroom(double x, double y, double width, double height) + { + height = 200; + Rectangle2D.Double stem = new Rectangle2D.Double(x+width/6, + y, (2*width)/3, + height/3); + + Line2D.Double top1 = new Line2D.Double(x, y, x+width, y); + + Line2D.Double top2 = new Line2D.Double(x, y, x, y-(height/3)); + + Line2D.Double top3 = new Line2D.Double(x, y-(height/3), + x+width/3, + y-(2*height)/3); + + Line2D.Double top4 = new Line2D.Double(x+width/3, + y-(2*height)/3, + x+(2*width)/3, + y-(2*height)/3); + + Line2D.Double top5 = new Line2D.Double(x+(2*width)/3, + y-(2*height)/3, + x+width, y-height/3); + + Line2D.Double top6 = new Line2D.Double(x+width, y-height/3, + x+width, y); + + + GeneralPath wholeMushroom = this.get(); + wholeMushroom.append(stem, false); + wholeMushroom.append(top1, false); + wholeMushroom.append(top2, false); + wholeMushroom.append(top3, false); + wholeMushroom.append(top4, false); + wholeMushroom.append(top5, false); + wholeMushroom.append(top6, false); + + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/WritePictureToFile.java new file mode 100755 index 0000000..a4e443f --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/Circle.java new file mode 100644 index 0000000..501a690 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureComponent.java new file mode 100644 index 0000000..017c4dd --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureComponent.java @@ -0,0 +1,116 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Natasha Issayeva (fixed the snowmans's head) + @version for UCSB CS56, S13 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius*2 - middleRadius*2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Natasha Issayeva", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureViewer.java new file mode 100644 index 0000000..e50a021 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/nissayeva/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.nissayeva.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/SimpleGui1.java new file mode 100644 index 0000000..52914a9 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.rkjha; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Rishabh Jha + @version CS56, Winter 2014, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("OH YES CLICK ME") ; + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + java.awt.Color myColor = new java.awt.Color(125,230,193); + button.setBackground(myColor); + button.setOpaque(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/AllMyDrawings.java new file mode 100755 index 0000000..d81a612 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/AllMyDrawings.java @@ -0,0 +1,163 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Rishabh Jha + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few hockey sticks + + */ + + public static void drawPicture1(Graphics2D g2) { + + HockeyStick h1 = new HockeyStick(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black hockey stick that's half the size + // and moved over 150 pixels in x direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a hockey stick that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two hockey sticks with pucks + + HockeyStickWithPuck hw1 = new HockeyStickWithPuck(50,350,30,120); + HockeyStickWithPuck hw2 = new HockeyStickWithPuck(200,350,50,200); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); g2.draw(hw2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few hockey sticks by Rishabh Jha", 20,20); + } + + /** + Draw a picture with more hockey sticks and pucks that'll + really knock your socks off + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some hockey sticks. + + HockeyStick large = new HockeyStick(100,50,225,150); + HockeyStick smallCC = new HockeyStick(20,50,30,75); + HockeyStick tallSkinny = new HockeyStick(20,150,10,40); + HockeyStick shortFat = new HockeyStick(20,250,30,10); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + HockeyStickWithPuck h1 = new HockeyStickWithPuck(150,200,80,250); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black hockey stick with puck that's half the size, + // and moved over 150 pixels in x direction + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a hockey stick with puck that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two houses with Windows + + HockeyStickWithPuck hw1 = new HockeyStickWithPuck(50,350,40,75); + HockeyStickWithPuck hw2 = new HockeyStickWithPuck(200,350,100,300); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI/4.0); + + g2.draw(hw3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of hockey sticks and pucks by Rishabh Jha", 20,20); + } + + /** Draw a different picture with a few houses and coffee cups + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of swag (ok fine hockey sticks and pucks again) by Rishabh Jha", 20,20); + + + // Draw some coffee cups. + + HockeyStickWithPuck large = new HockeyStickWithPuck(100,50,100,225); + HockeyStick smallCC = new HockeyStick(20,50,40,100); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/CoffeeCup.java new file mode 100755 index 0000000..981eb35 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStick.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStick.java new file mode 100644 index 0000000..169e3f1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStick.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Rishabh Jha + @version for CS56, Winter 14, UCSB + +*/ +public class HockeyStick extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of upper left corner of HockeyStick + @param y y coord of upper left corner of HockeyStick + @param width width of the base of the HockeyStick + @param height height of HockeyStick from base to top of grip + */ + public HockeyStick(double x, double y, double width, double height) + { + //NOTE: I'm aware that this is two rectangles overlapping each other, hence a small square will be shown at the intersecting area + // I like to call this "added aesthetic value" to what would otherwise be a boring block-shaped L :-) + + + //Setting up parameters + + double baseHeight = .15 * height; // How high the portion of the stick you try to hit the puck with is + double gripWidth = .15 * width; // How wide the hockey stick is in terms of gripping area + + double upperLeftBaseCorner = y + (.85 * height); + + // Make the long, handle part + + Rectangle2D.Double handle = + new Rectangle2D.Double(x, y, gripWidth, height); + + // Make the base (striking) part + + Rectangle2D.Double base = + new Rectangle2D.Double(x, upperLeftBaseCorner, width, baseHeight); + // put the whole hockey stick together + + GeneralPath wholeHockeyStick = this.get(); + wholeHockeyStick.append(handle, false); + wholeHockeyStick.append(base, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStickWithPuck.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStickWithPuck.java new file mode 100644 index 0000000..d84f53a --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HockeyStickWithPuck.java @@ -0,0 +1,51 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Ellipse2D; +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Hockey stick with a puck + + @author Rishabh Jha + @version for CS56, W14, UCSB, 02/23/2014 + +*/ + +public class HockeyStickWithPuck extends HockeyStick implements Shape{ + + /** + Constructor for HockeyStickWithPuck + */ + + public HockeyStickWithPuck(double x, double y, double width, double height) { + //construct the basic hockey stick + super(x,y,width,height); + + GeneralPath gp = this.get(); + + double xTopLeftCorner = x + (0.5 * width); + double yTopLeftCorner = y + (0.6 * height); + double puckDiameter = 0.2 * height; + + //construct the circle + Ellipse2D.Double puck = new Ellipse2D.Double(xTopLeftCorner, yTopLeftCorner, puckDiameter, puckDiameter); + + //get the GeneralPath + GeneralPath wholeFigure = this.get(); + wholeFigure.append(puck, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/House.java new file mode 100755 index 0000000..68d5c1e --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 14, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of UPPER left corner of house + @param y y coord of UPPER left corner of house + @param width width of the house + @param height height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HouseWithWindows.java new file mode 100755 index 0000000..9c828a5 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W14, UCSB, 02/23/2014 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class HouseWithWindows + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..cec2a23 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureComponent.java @@ -0,0 +1,78 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Rishabh Jha (Hockey Stick Drawings) + @version CS56, Winter 2014, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..9326cca --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, W14, 02/23/2014 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/WritePictureToFile.java new file mode 100755 index 0000000..c636e2c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W14 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/Circle.java new file mode 100644 index 0000000..fecb9e9 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureComponent.java new file mode 100644 index 0000000..4b1cd3c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureComponent.java @@ -0,0 +1,117 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Rishabh Jha (fixed the snowmans's head) + @version for UCSB CS56, W14 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Rishabh Jha", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureViewer.java new file mode 100644 index 0000000..a99cdcf --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/rkjha/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.rkjha.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/SimpleGui1.java new file mode 100644 index 0000000..eee41a6 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Sarah Darwiche + @version CS56, Winter 2014, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me right now") ; + java.awt.Color myColor = new java.awt.Color(204,000,051); // R, G, B values. + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/#House.java# b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/#House.java# new file mode 100755 index 0000000..bb57198 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/#House.java# @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a Computer Monitor that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Sarah Darwiche + @version for CS56, Winter 14, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/AllMyDrawings.java new file mode 100755 index 0000000..ff5674b --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/AllMyDrawings.java @@ -0,0 +1,178 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Phill Conrad + * @author Sarah Darwiche + * @version for CS56, lab06, Winter 2014 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few computer monitors + */ + + public static void drawPicture1(Graphics2D g2) { + + ComputerMonitor h1 = new ComputerMonitor(100,150,75,50); + g2.setColor(Color.CYAN); g2.draw(h1); + + // Make a black computer monitor that's half the size, + // and moved over 150 pixels in x direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a computer monitor that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two Computers + + Computer c1 = new Computer(50,350,40,75); + Computer c2 = new Computer(200,350,200,100); + + g2.draw(c1); + g2.setColor(new Color(0x8F00FF)); g2.draw(c2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few computer monitors by Sarah Darwiche", 20,20); + } + + + /** Draw a picture with a few computer monitors and computers + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some computers. + + Computer large = new Computer(100,50,225,150); + Computer smallCC = new Computer(20,50,40,30); + Computer tallSkinny = new Computer(20,150,20,40); + Computer shortFat = new Computer(20,250,40,20); + + g2.setColor(Color.RED); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.YELLOW); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); + + ComputerMonitor C1 = new Computer(100,250,50,75); + g2.setColor(Color.CYAN); g2.draw(C1); + + // Make a Computer monitor that's half the size, + // and moved over 150 pixels in x direction + Shape C2 = ShapeTransforms.scaledCopyOfLL(C1,0.5,0.5); + C2 = ShapeTransforms.translatedCopyOf(C2,150,0); + g2.setColor(Color.BLACK); g2.draw(C2); + + // Here's a computer monitor that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + C2 = ShapeTransforms.scaledCopyOfLL(C2,4,4); + C2 = ShapeTransforms.translatedCopyOf(C2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(C2); + + // Draw two Computers + + Computer c_1 = new Computer(50,350,40,75); + Computer c_2 = new Computer(200,350,200,100); + + g2.draw(c_1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second computer 45 degrees around its center. + Shape c_3 = ShapeTransforms.rotatedCopyOf(c_2, Math.PI/4.0); + Shape c_4 = ShapeTransforms.rotatedCopyOf(shortFat, Math.PI/3.0); + Shape c_5 = ShapeTransforms.rotatedCopyOf(large, Math.PI/1.0); + + g2.draw(c_3); + g2.draw(c_4); + g2.draw(c_5); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of Computers and Computer Monitors by Sarah Darwiche", 20,20); + } + + /** Draw a different picture with a few computers and computer monitors + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of Computers and Computer Monitors by Sarah Darwiche", 20,20); + + + // Draw some Computer Monitors. + + ComputerMonitor large = new ComputerMonitor(100,50,225,150); + ComputerMonitor smallCC = new ComputerMonitor(20,50,40,30); + Computer medium = new Computer(450,50,90,30); + Computer medium2 = new Computer(450,300,90,30); + Computer medium3 = new Computer(250,400,90,30); + + g2.setColor(Color.BLUE); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); + g2.setColor(Color.RED); g2.draw(medium2); + g2.setColor(Color.YELLOW); g2.draw(medium3); + + g2.setColor(Color.GREEN); + + Shape copy = ShapeTransforms.rotatedCopyOf(medium, Math.PI/1.0); + g2.draw(copy); + + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/CoffeeCup.java new file mode 100755 index 0000000..faf8f5a --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/Computer.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/Computer.java new file mode 100755 index 0000000..15782df --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/Computer.java @@ -0,0 +1,59 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A Computer + + @author Sarah Darwiche + @version for CS56, W14, UCSB, 02/16/2014 + +*/ +public class Computer extends ComputerMonitor implements Shape +{ + /** + * Constructor for objects of class Computer + */ + public Computer(double x, double y, double width, double height) + { + // construct the basic computer monitor shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + //scale base hight and width + double w = 0.25 * width; + double base_top = y + height; + double base_height = 0.25 * height; + + // Make a base for the computer + + Rectangle2D.Double comp1 = + new Rectangle2D.Double(x +((width-w)/2), base_top, w, base_height/2); + + Rectangle2D.Double comp2 = + new Rectangle2D.Double(x +((width-w)/3), base_height / 2 + base_top , 2*w, base_height/4); + + //add base to the computers + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeComputer = this.get(); + wholeComputer.append(comp1, false); + wholeComputer.append(comp2, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/ComputerMonitor.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/ComputerMonitor.java new file mode 100755 index 0000000..6ee592b --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/ComputerMonitor.java @@ -0,0 +1,74 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a Computer Monitor that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Sarah Darwiche + @version for CS56, Winter 14, UCSB + +*/ +public class ComputerMonitor extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of monitor + @param y y coord of lower left corner of monitor + @param width width of the monitor + @param height of monitor + */ + public ComputerMonitor(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double outsidemonitor_height = height; + double insidemonitor_height =.90 * height; + double insidemonitor_width = .90 * width; + + double outsidemonitorUpperLeftY = y; + double insidemonitorUpperLeftY =( y + (outsidemonitor_height-insidemonitor_height)/2); + + // Make the outside monitor + + Rectangle2D.Double outsidemonitor = + new Rectangle2D.Double(x, outsidemonitorUpperLeftY , + width,outsidemonitor_height); + + //Make the inside monitor + + Rectangle2D.Double insidemonitor = + new Rectangle2D.Double(x +(width - insidemonitor_width)/2, insidemonitorUpperLeftY , + insidemonitor_width, insidemonitor_height); + + + // put the whole monitor together + + GeneralPath wholeMonitor = this.get(); + wholeMonitor.append(outsidemonitor, false); + wholeMonitor.append(insidemonitor, false); + + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/House.java new file mode 100755 index 0000000..c13d3cf --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 11, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/HouseWithWindows.java new file mode 100755 index 0000000..45b2f2d --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..b57f752 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..c8f6447 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/MultiPictureViewer.java @@ -0,0 +1,53 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @author S. Darwiche + * @version for UCSB CS56, W14, 02/27/2014 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Sarah's First Drawing"); + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/WritePictureToFile.java new file mode 100755 index 0000000..648e8e3 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/Circle.java new file mode 100644 index 0000000..fa30800 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureComponent.java new file mode 100644 index 0000000..c86d34f --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureComponent.java @@ -0,0 +1,117 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Sarah Darwiche (fixed the snowmans's head) + @version for UCSB CS56, W14 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + + + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Sarah Darwiche", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureViewer.java new file mode 100644 index 0000000..33df9a8 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/sarahdarwiche/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.sarahdarwiche.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("SNOWMAN"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/SimpleGui1.java b/src/edu/ucsb/cs56/W14/drawings/vlara/SimpleGui1.java new file mode 100644 index 0000000..5cb7e90 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/SimpleGui1.java @@ -0,0 +1,30 @@ +package edu.ucsb.cs56.w14.drawings.vlara; +import javax.swing.*; + +/** SimpleGui1 comes from Head First Java 2nd Edition p. 355. + It illustrates a simple GUI with a Button that doesn't do anything yet. + + @author Head First Java, 2nd Edition p. 355 + @author P. Conrad (who only typed it in and added the Javadoc comments) + @author Vincente Lara + @version CS56, Spring 2013, UCSB +*/ + +public class SimpleGui1 { + + /** main method to fire up a JFrame on the screen + @param args not used + */ + + public static void main (String[] args) { + JFrame frame = new JFrame() ; + JButton button = new JButton("click me for fun stuff") ; + java.awt.Color myColor = new java.awt.Color(12,150,150);// R, G, B value + button.setBackground(myColor); + button.setOpaque(true); + frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ; + frame. getContentPane() . add(button) ; + frame. setSize(300,300) ; + frame. setVisible(true) ; + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AllMyDrawings.java new file mode 100755 index 0000000..803d5e2 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AllMyDrawings.java @@ -0,0 +1,164 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; + +import java.awt.Graphics2D; +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 +import java.awt.Stroke; +import java.awt.BasicStroke; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Vincente Lara + * @version for CS56, lab06, W14 + */ + + +public class AllMyDrawings +{ + /** Draw a picture with a few hourglasses + */ + + public static void drawPicture1(Graphics2D g2) { + + Hourglass h1 = new Hourglass(156,356,45,88); + g2.setColor(Color.darkGray); g2.draw(h1); + + // Make a blue hourglass, + // and moved over 150 pixels in x direction + // and 25 pixels in y direction + + Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.25,0.25); + h2 = ShapeTransforms.translatedCopyOf(h2,150,25); + g2.setColor(Color.blue); g2.draw(h2); + + // Here's a hourglass that's 6x as big (3x the original) + // and moved 150 more pixels to down. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,0,150); + + // Drawn with a thinner stroke + Stroke thin = new BasicStroke (5.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thin); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two houses with Windows + + HourglassFull hw1 = new HourglassFull(30,400,18,91); + HourglassFull hw2 = new HourglassFull(200,350,50,100); //initally 200,100 + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); g2.draw(hw2); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.magenta); + g2.drawString("A few Hourglasses by Vincente Lara", 25,25); + } + + + /** Draw a picture with a few hourglasses + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some hourglasses. + +/* + HourglassFull large = new HourglassFull(100,50,225,150); + HourglassFull smallCC = new HourglassFull(20,50,40,30); + HourglassFull tallSkinny = new HourglassFull(20,150,20,40); + HourglassFull shortFat = new HourglassFull(20,250,40,20); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); +*/ + + HourglassFull hf1 = new HourglassFull(25,30,100,300); + g2.setColor(Color.CYAN); g2.draw(hf1); + + // Make a black house that's half the size, + // and moved over 150 pixels in x direction + Shape h2 = ShapeTransforms.scaledCopyOfLL(hf1,0.5,0.5); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + g2.setColor(Color.BLACK); g2.draw(h2); + + // Here's a house that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + h2 = ShapeTransforms.scaledCopyOfLL(h2,4,4); + h2 = ShapeTransforms.translatedCopyOf(h2,150,0); + + // We'll draw this with a thicker stroke + Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors + // #002FA7 is "International Klein Blue" according to Wikipedia + // In HTML we use #, but in Java (and C/C++) its 0x + + Stroke orig=g2.getStroke(); + g2.setStroke(thick); + g2.setColor(new Color(0x002FA7)); + g2.draw(h2); + + // Draw two houses with Windows + + HourglassFull hw1 = new HourglassFull(50,350,40,75); + HourglassFull hw2 = new HourglassFull(200,350,200,100); + + g2.draw(hw1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI/4.0); + + g2.draw(hw3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of Coffee Cups and a few houses by Phill Conrad", 20,20); + } + + /** Draw a different picture with a few houses and coffee cups + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A couple Hourglasses by Vincente Lara", 20,20); + + + // Draw some coffee cups. + + HourglassFull large = new HourglassFull(100,50,225,150); + HourglassFull smallCC = new HourglassFull(20,50,40,30); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + + + } + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AnimatedPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AnimatedPictureViewer.java new file mode 100644 index 0000000..257534c --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/AnimatedPictureViewer.java @@ -0,0 +1,119 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.AffineTransform; +//import java.awt.Shape; +//import java.geom.Rectangle2D; + + +public class AnimatedPictureViewer { + + private DrawPanel panel = new DrawPanel(); + + private HourglassFull hourglass = new HourglassFull(100, 100, 100, 100); + + Thread anim; + + private int x = 100; + private int y = 100; + private int width = 100; + private int height = 100; + + private int dy = 5; + + public static void main (String[] args) { + new AnimatedPictureViewer().go(); + } + + public void go() { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.getContentPane().add(panel); + frame.setSize(640,600); + frame.setVisible(true); + + frame.getContentPane().addMouseListener(new MouseAdapter() { + public void mouseEntered(MouseEvent e){ + System.out.println("mouse entered"); + anim = new Animation(); + anim.start(); + } + + public void mouseExited(MouseEvent e){ + System.out.println("Mouse exited"); + // Kill the animation thread + anim.interrupt(); + while (anim.isAlive()){} + anim = null; + panel.repaint(); + } + }); + + } // go() + + class DrawPanel extends JPanel { + public void paintComponent(Graphics g) { + + Graphics2D g2 = (Graphics2D) g; + + // Clear the panel first + g2.setColor(Color.white); + g2.fillRect(0,0,this.getWidth(), this.getHeight()); + + // Draw the HourglassFull + g2.setColor(Color.BLUE); + + HourglassFull test = new HourglassFull(x, y, width, height); + + //AffineTransform af = new AffineTransform(); + //Rectangle2D box = test.getBounds2D(); + + //double cx = box.getCenterX(); + //double cy = box.getCenterY(); + + //af.translate(cx, cy); + //af.rotate(30.0); + //af.translate(-cx, -cy); + + //Shape Hg = af.createTransformedShape(Hg); + + g2.draw(test); + + + + } + } + + class Animation extends Thread { + public void run() { + try { + while (true) { + // Bounce off the walls + + if (y >= 400) { dy = -5; } + if (y <= 50) { dy = 5; } + + y += dy; + x += dy; + width += dy; + height += dy; + + + panel.repaint(); + Thread.sleep(50); + } + } catch(Exception ex) { + if (ex instanceof InterruptedException) { + // Do nothing - expected on mouseExited + } else { + ex.printStackTrace(); + System.exit(1); + } + } + } + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/CoffeeCup.java new file mode 100755 index 0000000..249ed84 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/CoffeeCup.java @@ -0,0 +1,96 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A Coffee Cup (wrapper around a General Path, implements Shape) + + This provides an example of how you can start with the coordinates + of a hard coded object, and end up with an object that can be + drawn anywhere, with any width or height. + + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class CoffeeCup extends GeneralPathWrapper implements Shape +{ + + + /** + * Constructor for objects of class CoffeeCup + */ + public CoffeeCup(double x, double y, double width, double height) + { + + // Specify the upper left corner, and the + // width and height of the original points used to + // plot the *hard-coded* coffee cup + + final double ORIG_ULX = 100.0; + final double ORIG_ULY = 100.0; + final double ORIG_HEIGHT = 300.0; + final double ORIG_WIDTH = 400.0; + + 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 + + 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); + + // translate to the origin by subtracting the original upper left x and y + // then translate to (x,y) by adding x and y + + Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); + + // scale to correct height and width + s = ShapeTransforms.scaledCopyOf(s, + width/ORIG_WIDTH, + height/ORIG_HEIGHT) ; + + // Use the GeneralPath constructor that takes a shape and returns + // it as a general path to set our instance variable cup + + this.set(new GeneralPath(s)); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/Hourglass.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/Hourglass.java new file mode 100755 index 0000000..b754556 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/Hourglass.java @@ -0,0 +1,81 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a hourglass that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Vincente Lara + @version for CS56, Winter 14, UCSB + +*/ +public class Hourglass extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of upper left corner of house + @param y y coord of upper left corner of house + @param width width of the hourglass + @param height of hourglass + */ + public Hourglass(double x, double y, double width, double height) + { + //Fixed initial bug that had the width as just a number + //and not the acutal width. + // + double actualWidth = x + width; + double actualHeight = y + height; + + //The first diagonal of the hourglass + // \ + // \ + //looks like this \ + + Line2D.Double firstDiagonal = + new Line2D.Double(x, y, actualWidth, actualHeight); + + //The second diagonal of the hourglass + // \ / + // \ / + // / \ + //looks like this / \ + + Line2D.Double secondDiagonal = + new Line2D.Double(actualWidth, y, x, actualHeight); + + //Add the top and bottom lines of the hourglass + // ------- + // \ / + // \ / + // / \ + // / \ + // ------- + + Line2D.Double top = + new Line2D.Double(x, y, actualWidth, y); + + Line2D.Double bottom = + new Line2D.Double(x, actualHeight, actualWidth, actualHeight); + + GeneralPath wholeHourglass = this.get(); + wholeHourglass.append(firstDiagonal, false); + wholeHourglass.append(secondDiagonal, false); + wholeHourglass.append(top, false); + wholeHourglass.append(bottom, false); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HourglassFull.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HourglassFull.java new file mode 100755 index 0000000..a703b79 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HourglassFull.java @@ -0,0 +1,56 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Vincente Lara + @version for CS56, W14, UCSB, 02/25/2014 + +*/ +public class HourglassFull extends Hourglass implements Shape +{ + + public HourglassFull(double x, double y, double width, double height) + { + // construct the basic hourglass + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + //add a top and bottom base to the hourglass + //simple rectangle shape like so + // + double widthBase = width; + double heightBase = .1 * height; + + //Fixed + //width was just a number and not the distance between the points + + Rectangle2D.Double topBase = + new Rectangle2D.Double(x, y - heightBase, widthBase, heightBase); + Rectangle2D.Double bottomBase = + new Rectangle2D.Double(x, (height + y), widthBase, heightBase); + + // add bases to the hourglass + + GeneralPath wholeHourglass = this.get(); + wholeHourglass.append(topBase, false); + wholeHourglass.append(bottomBase, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/House.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/House.java new file mode 100755 index 0000000..e69fa4a --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/House.java @@ -0,0 +1,76 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + +/** + A vector drawing of a house that implements + the Shape interface, and so can be drawn, as well as + rotated, scaled, etc. + + @author Phill Conrad + @version for CS56, Winter 11, UCSB + +*/ +public class House extends GeneralPathWrapper implements Shape +{ + /** + Constructor + + @param x x coord of lower left corner of house + @param y y coord of lower left corner of house + @param width width of the house + @param height of house (including first story and second story) + */ + public House(double x, double y, double width, double height) + { + + // Rather than having to scale at the end, we can just + // draw things the right way to begin with, using the + // x, y, width and height. If you haven't already + // hard coded a particular drawing, this may be an easier + // way. + + double firstStoryHeight = .75 * height; + double roofHeight = height - firstStoryHeight; + + double firstStoryUpperLeftY = y + roofHeight; + + // Make the first story + + Rectangle2D.Double firstStory = + new Rectangle2D.Double(x, firstStoryUpperLeftY , + width, firstStoryHeight); + + // make the roof. Remember that y goes DOWN the page, + // so we ADD to y to get a "lower" value on the screen + + Line2D.Double leftRoof = + new Line2D.Double (x, y + roofHeight, + x + width/2.0, y); + + Line2D.Double rightRoof = + new Line2D.Double (x + width/2.0, y, + x + width, y + roofHeight); + + // put the whole house together + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(firstStory, false); + wholeHouse.append(leftRoof, false); + wholeHouse.append(rightRoof, false); + + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HouseWithWindows.java new file mode 100755 index 0000000..27b3c36 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/HouseWithWindows.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +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 + +// all imports below this line needed if you are implementing Shape +import java.awt.geom.Point2D; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.Rectangle; +import java.awt.geom.PathIterator; +import java.awt.geom.AffineTransform; + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; +/** + A House + + @author Phill Conrad + @version for CS56, W11, UCSB, 02/23/2011 + +*/ +public class HouseWithWindows extends House implements Shape +{ + /** + * Constructor for objects of class CoffeeCup + */ + public HouseWithWindows(double x, double y, double width, double height) + { + // construct the basic house shell + super(x,y,width,height); + + // get the GeneralPath that we are going to append stuff to + GeneralPath gp = this.get(); + + // Make three windows, spaced like this, where w=width/10.0; + // | +--+ +--+ +--+ | + // | | | | | | | | + // | +--+ +--+ +--+ | + // |w 2w w 2w w w2 w| + // + // The top of window will be at y + 0.5*height and the + // height of the window is 0.25height; + + double w = 0.10 * width; + double winTop = y + 0.5 * height; + double winHt = 0.25 * height; + + Rectangle2D.Double win1 = + new Rectangle2D.Double(x + w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win2 = + new Rectangle2D.Double(x + 4.0*w, winTop, 2.0 * w, winHt); + Rectangle2D.Double win3 = + new Rectangle2D.Double(x + 7.0*w, winTop, 2.0 * w, winHt); + + // add the windows to the house + // Look up the meaning of the second parameter of append + // (Hint--is a method of "GeneralPath") + + GeneralPath wholeHouse = this.get(); + wholeHouse.append(win1, false); + wholeHouse.append(win2, false); + wholeHouse.append(win3, false); + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureComponent.java new file mode 100644 index 0000000..3fafc02 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureComponent.java @@ -0,0 +1,77 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; + +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +import java.awt.geom.Rectangle2D; // for rectangles drawing with Doubles + +import java.awt.Color; // class for Colors +import java.awt.Shape; // Shape interface +import java.awt.Stroke; // Stroke interface +import java.awt.BasicStroke; // class that implements stroke + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, Spring 2012, UCSB + + +*/ + + +public class MultiPictureComponent extends JComponent +{ + private int whichPicture = 0; + + public MultiPictureComponent(int whichPicture) { + this.whichPicture = whichPicture; + } + + /** 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. + */ + + public void paintComponent(Graphics g) + { + + Graphics2D g2 = (Graphics2D) g; + + // Call a static method for drawing picture 2 + + switch (this.whichPicture) { + + case 1: + AllMyDrawings.drawPicture1(g2); + break; + + case 2: + AllMyDrawings.drawPicture2(g2); + break; + + case 3: + AllMyDrawings.drawPicture3(g2); + break; + default: + throw new IllegalArgumentException("Unknown value for whichPicture in MultiPictureComponent" + this.whichPicture); + + } // switch + } // paintComponent + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureViewer.java new file mode 100644 index 0000000..4a5b8dc --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; + +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version for UCSB CS56, S12, 02/09/2011 + */ + +public class MultiPictureViewer +{ + public static void main(String[] args) + { + int whichPicture = 1; + + // If user passed a command line argument, + // get which picture we want to display from the user + + if (args.length== 1) { + whichPicture = Integer.parseInt(args[0]); + } + + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's First Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + MultiPictureComponent component = + new MultiPictureComponent(whichPicture); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/WritePictureToFile.java new file mode 100755 index 0000000..2b45c13 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/advanced/WritePictureToFile.java @@ -0,0 +1,95 @@ +package edu.ucsb.cs56.w14.drawings.vlara.advanced; +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.awt.Color; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + + +import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper; + + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W11 UCSB + */ + +public class WritePictureToFile +{ + public static void usage() + { + System.out.println("Usage: java WritePictureToFile whichImage mypic"); + // @@@ modify the next line to describe your picture + System.out.println(" whichImage should be 1,2 or 3"); + System.out.println(" whichImage chooses from drawPicture1, 2 or 3"); + System.out.println(" .png gets added to the filename"); + System.out.println(" e.g. if you pass mypic, filename is mypic.png"); + System.out.println("Example: java WritePictureToFile 3 foo"); + System.out.println(" produces foo.png from drawPicture3"); + } + + /** Write the drawFourCoffeeCups picture to a file. + * + * @param args The first command line argument is the file to write to. We leave off the extension + * because that gets put on automatically. + */ + + public static void main(String[] args) + { + // make sure we have exactly one command line argument + if (args.length != 2) + { + usage(); + System.exit(1); + } + + String whichPicture = args[0]; // first command line arg is 1, 2, 3 + String outputfileName = args[1]; // second command line arg is which pic + + final int WIDTH = 640; + final int HEIGHT = 480; + + // create a new image + // TYPE_INT_ARGB is "RGB image" with transparency (A = alpha channel) + + BufferedImage bi = + new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + + // g2 is a Graphics2D object that will draw into the BufferedImage object + + Graphics2D g2 = bi.createGraphics(); + + if (whichPicture.equals("1")) { + AllMyDrawings.drawPicture1(g2); + } else if (whichPicture.equals("2")) { + AllMyDrawings.drawPicture2(g2); + } else if (whichPicture.equals("3")) { + AllMyDrawings.drawPicture2(g2); + } + + final String imageType = "png"; // choices: "gif", "png", "jpg" + + // We must declare this variable outside the try block, + // so we can see it inside the catch block + + String fullFileName = ""; + + try + { + fullFileName = outputfileName + "." + imageType; // make the file name + File outputfile = new File(fullFileName); // the file we will try to write + ImageIO.write(bi, imageType, outputfile); // actually write it + System.out.println("I created " + fullFileName); // tell the user + } + catch (IOException e) + { + System.err.println("Sorry, an error occurred--I could not create "+ fullFileName +"\n The error was: "+ e.toString()); + } + + + } +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/simple/Circle.java b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/Circle.java new file mode 100644 index 0000000..92436af --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.w14.drawings.vlara.simple; + + + +/** + * Circle extends Ellipse2D to make it easier to draw circles + * because the parameters to the constructor are more convenient + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ +public class Circle extends java.awt.geom.Ellipse2D.Double implements java.awt.Shape +{ + /** + * Constructor for objects of class Circle + * @param x x coordinate of center of circle + * @param y y coordinate of center of circle + * @param r radius of circle + */ + public Circle(double x, double y, double r) + { + // invoke the super class constructor, + // i.e. the one for Ellipse2D.Double, which takes + // upper-left-x, upper-left-y (of the bounding box) + // width, and height + + super( x - r, y - r, /* upper left corner of bounding box */ + r * 2, r * 2); /* width and height are double the radius */ + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureComponent.java b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureComponent.java new file mode 100644 index 0000000..63223d4 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureComponent.java @@ -0,0 +1,118 @@ +package edu.ucsb.cs56.w14.drawings.vlara.simple; +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.Rectangle; // squares and rectangles +import java.awt.geom.GeneralPath; // combinations of lines and curves + + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author Vincente Lara (fixed the snowmans's head) + @version for UCSB CS56, W14 + + +*/ + +// Your class should "extend JComponent +// This is "inheritance", which we'll start readina about in Chapter 10 +// It means that PictureComponent "is a" JComponent +// that is, a special type of JComponent that is for a specific purpose + +public class PictureComponent 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; + + // Now the fun part---we draw stuff! + // @@@ YOU'LL CUSTOMIZE EVERYTHING BELOW THIS LINE + + Rectangle house = new Rectangle(100, 200, 100, 100); + g2.draw( house); + + // lroof and rroof are the left and right sides of the roof, + Line2D.Double lroof = new Line2D.Double(100, 200, 150, 150); + Line2D.Double rroof = new Line2D.Double(150,150, 200,200); + + g2.draw(lroof); + g2.draw(rroof); + + // now a snowman: three circles + // here we use constants, so that if we want to change + // the dimensions later, or move the snowman around, + // it becomes easier. + // Instead of doing the math ourselves, and putting "hard coded numbers" + // in the constructors for the Ellipses, we let the computer do the math! + + final double bottomRadius = 20; + final double middleRadius = 15; + final double topRadius = 10; + final double snowManCenterBottomX = 400; + final double snowManCenterBottomY = 300; + + Circle snowManBottomCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius, + bottomRadius + ); + g2.draw(snowManBottomCircle); + + Circle snowManMiddleCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius, + middleRadius + ); + g2.draw(snowManMiddleCircle); + + Circle snowManTopCircle = + new Circle + ( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius * 2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManTopCircle); + + // @@@ ADD CODE HERE TO DRAW THE TOP CIRCLE + + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.drawString("A house and a snowman, by Phill Conrad", 20,20); + g2.drawString("Top of snowman added by Vincente Lara", 20,40); + } + + + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureViewer.java new file mode 100644 index 0000000..7b50de1 --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/vlara/simple/PictureViewer.java @@ -0,0 +1,41 @@ +package edu.ucsb.cs56.w14.drawings.vlara.simple; +import javax.swing.JFrame; + +/** A viewer class to see a picture I drew with + * just three simple Java graphics objects, namely + * Rectangle, Line2D.Double, Ellipse2D.Double + * + * @author P. Conrad + * @version CS56, Spring 2013, UCSB + */ + +public class PictureViewer +{ + public static void main(String[] args) + { + JFrame frame = new JFrame(); + + // Set the size to whatever size you like (width, height) + // For projects you turn in, lets not get any bigger than 640,480 + + frame.setSize(640,480); // @@@ MODIFY THIS LINE IF YOU LIKE + + // Set your own title + frame.setTitle("Phill's Drawing"); // @@@ MODIFY THIS LINE + + // Always do this so that the red X (or red circle) works + // to close the window. + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Instantiate your drawing as a "component" + + PictureComponent component = new PictureComponent(); + + // Always add your component to the frame + // and then make the window visible + + frame.add(component); + frame.setVisible(true); + } +}