From 885bfb70494f0edbb35f0fad60cf61297ec54b19 Mon Sep 17 00:00:00 2001 From: Dominic Kirby Date: Fri, 28 Oct 2016 00:36:41 -0700 Subject: [PATCH 1/2] adding everything --- .../ucsb/cs56/drawings/dkirby/SimpleGui1.java | 34 ++++ .../dkirby/advanced/AllMyDrawings.java | 150 ++++++++++++++++++ .../drawings/dkirby/advanced/CoffeeCup.java | 85 ++++++++++ .../cs56/drawings/dkirby/advanced/House.java | 68 ++++++++ .../dkirby/advanced/HouseWithWindows.java | 55 +++++++ .../drawings/dkirby/advanced/MrPeanut.java | 80 ++++++++++ .../advanced/MultiPictureComponent.java | 49 ++++++ .../dkirby/advanced/MultiPictureViewer.java | 52 ++++++ .../cs56/drawings/dkirby/advanced/Peanut.java | 48 ++++++ .../dkirby/advanced/WritePictureToFile.java | 83 ++++++++++ .../cs56/drawings/dkirby/simple/Circle.java | 31 ++++ .../dkirby/simple/PictureComponent.java | 110 +++++++++++++ .../drawings/dkirby/simple/PictureViewer.java | 42 +++++ 13 files changed, 887 insertions(+) create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/SimpleGui1.java create mode 100755 src/edu/ucsb/cs56/drawings/dkirby/advanced/AllMyDrawings.java create mode 100755 src/edu/ucsb/cs56/drawings/dkirby/advanced/CoffeeCup.java create mode 100755 src/edu/ucsb/cs56/drawings/dkirby/advanced/House.java create mode 100755 src/edu/ucsb/cs56/drawings/dkirby/advanced/HouseWithWindows.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureComponent.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureViewer.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/advanced/Peanut.java create mode 100755 src/edu/ucsb/cs56/drawings/dkirby/advanced/WritePictureToFile.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/simple/Circle.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/simple/PictureComponent.java create mode 100644 src/edu/ucsb/cs56/drawings/dkirby/simple/PictureViewer.java diff --git a/src/edu/ucsb/cs56/drawings/dkirby/SimpleGui1.java b/src/edu/ucsb/cs56/drawings/dkirby/SimpleGui1.java new file mode 100644 index 00000000..bf8aadec --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/SimpleGui1.java @@ -0,0 +1,34 @@ +package edu.ucsb.cs56.drawings.dkirby; + +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 D. Kirby + @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 baby one more time") ; + + java.awt.Color myColor = new java.awt.Color(154,17,200); // 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/drawings/dkirby/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/AllMyDrawings.java new file mode 100755 index 00000000..61b017ee --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/AllMyDrawings.java @@ -0,0 +1,150 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; + +import java.awt.Graphics2D; +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.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.drawings.utilities.GeneralPathWrapper; + +/** + * A class with static methods for drawing various pictures + * + * @author Phill Conrad + * @version for UCSB CS56, W16 + */ + +public class AllMyDrawings +{ + /** Draw a picture with a few peanuts + */ + + public static void drawPicture1(Graphics2D g2) { + + Peanut p1 = new MrPeanut(250,250,50); + g2.setColor(Color.CYAN); g2.draw(p1); + + // Make a black house that's half the size, + // and moved over 150 pixels in x direction + + Shape p2 = ShapeTransforms.scaledCopyOfLL(p1,0.5,0.5); + p2 = ShapeTransforms.translatedCopyOf(p2,150,0); + g2.setColor(Color.BLACK); g2.draw(p2); + + // Here's a house that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + p2 = ShapeTransforms.scaledCopyOfLL(p2,4,4); + p2 = ShapeTransforms.translatedCopyOf(p2,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(p2); + + // Draw two houses with Windows + + Peanut p3 = new MrPeanut(150,350,40); + Peanut p4 = new MrPeanut(200,350,200); + + g2.draw(p3); + g2.setColor(new Color(0x8F00FF)); g2.draw(p4); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A few MrPeanuts by Dominic Kirby", 20,20); + } + + + /** Draw a picture with a bunch of MrPeanuts + */ + public static void drawPicture2(Graphics2D g2) { + + // Draw some coffee cups. + + MrPeanut large = new MrPeanut(150,50,225); + MrPeanut smallMP = new MrPeanut(120,50,40); + MrPeanut tallSkinny = new MrPeanut(120,150,20); + MrPeanut shortFat = new MrPeanut(120,250,40); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallMP); + g2.setColor(Color.BLUE); g2.draw(tallSkinny); + g2.setColor(Color.MAGENTA); g2.draw(shortFat); + + MrPeanut p1 = new MrPeanut(200,250,50); + g2.setColor(Color.CYAN); g2.draw(p1); + + // Make a black house that's half the size, + // and moved over 150 pixels in x direction + Shape p2 = ShapeTransforms.scaledCopyOfLL(p1,0.5,0.5); + p2 = ShapeTransforms.translatedCopyOf(p2,150,0); + g2.setColor(Color.BLACK); g2.draw(p2); + + // Here's a house that's 4x as big (2x the original) + // and moved over 150 more pixels to right. + p2 = ShapeTransforms.scaledCopyOfLL(p2,4,4); + p2 = ShapeTransforms.translatedCopyOf(p2,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(p2); + + // Draw two houses with Windows + + MrPeanut mp1 = new MrPeanut(150,350,40); + MrPeanut mp2 = new MrPeanut(250,350,200); + + g2.draw(mp1); + g2.setColor(new Color(0x8F00FF)); + + // Rotate the second house 45 degrees around its center. + Shape mp3 = ShapeTransforms.rotatedCopyOf(mp2, Math.PI/4.0); + + g2.draw(mp3); + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + + g2.setStroke(orig); + g2.setColor(Color.BLACK); + g2.drawString("A bunch of MrPeanuts by Dominic Kirby", 20,20); + } + + /** Draw a different picture with a few Mr Peanuts + */ + + public static void drawPicture3(Graphics2D g2) { + + // label the drawing + + g2.drawString("A bunch of MrPeanuts by Dominic Kirby", 20,20); + + + // Draw some coffee cups. + + MrPeanut large = new MrPeanut(150,50,225); + MrPeanut smallCC = new MrPeanut(120,50,40); + + g2.setColor(Color.RED); g2.draw(large); + g2.setColor(Color.GREEN); g2.draw(smallCC); + + } +} diff --git a/src/edu/ucsb/cs56/drawings/dkirby/advanced/CoffeeCup.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/CoffeeCup.java new file mode 100755 index 00000000..5015682e --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/CoffeeCup.java @@ -0,0 +1,85 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; +import java.awt.geom.GeneralPath; // combinations of lines and curves +import java.awt.Shape; // general class for shapes + +import edu.ucsb.cs56.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.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, W16, UCSB + +*/ +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/drawings/dkirby/advanced/House.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/House.java new file mode 100755 index 00000000..daa29923 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/House.java @@ -0,0 +1,68 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; +import java.awt.geom.GeneralPath; // combinations of lines and curves +import java.awt.Shape; // general class for shapes + +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; + +import edu.ucsb.cs56.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.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, W16, 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/drawings/dkirby/advanced/HouseWithWindows.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/HouseWithWindows.java new file mode 100755 index 00000000..e4b2db82 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/HouseWithWindows.java @@ -0,0 +1,55 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; +import java.awt.geom.GeneralPath; +import java.awt.Shape; +import java.awt.geom.Rectangle2D; + +/** + A House + + @author Phill Conrad + @version for CS56, W16, UCSB + +*/ +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/drawings/dkirby/advanced/MrPeanut.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java new file mode 100644 index 00000000..1ac24fef --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java @@ -0,0 +1,80 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; +import java.awt.geom.GeneralPath; // combinations of lines and curves +import java.awt.Shape; // general class for shapes +import java.awt.Graphics; +import java.awt.Color; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Ellipse2D.Double; +import java.awt.geom.Ellipse2D; +import java.awt.geom.RectangularShape; +import java.awt.Graphics2D; + +import edu.ucsb.cs56.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.drawings.utilities.GeneralPathWrapper; + +/** + A Peanut with a top-hat, monocle, and arms/legs. + + @author Dominic Kirby + @version for CS56, W16, UCSB + +*/ +public class MrPeanut extends Peanut implements java.awt.Shape +{ + + + public MrPeanut(double x, double y, double height){ + super(x,y, height); + + GeneralPath gp = this.get(); + height = height * 2; + Rectangle2D.Double hatRidge = + new Rectangle2D.Double(x - height/4, y- height/20, height/5, + height/20); + + Rectangle2D.Double hatTop = + new Rectangle2D.Double(x-height/5, y - height/4, height/10, + height/5); + + Line2D.Double leftArmTop = + new Line2D.Double(x - height/4 , y + height/3, + x - 2*height/5, y + height/3); + + Line2D.Double leftArmBottom = + new Line2D.Double(x - 2* height/5 , y+ height/3, + x - 2*height/5 , y +height/2); + + Line2D.Double RightArmTop = + new Line2D.Double(x -height/16 , y + height/3, + x + height/10, y + height/3); + + Line2D.Double RightArmBottom = + new Line2D.Double(x + height/10, y + height/3, + x + height/10, y + height/2); + + Ellipse2D.Double monocle = + new Ellipse2D.Double(x - height/5, y + height/10, + height/20, height/20); + + Line2D.Double leftLeg = + new Line2D.Double(x-height/4, y + 8*height/10, + x - height/4, y + 5*height/4); + + Line2D.Double rightLeg = + new Line2D.Double(x-height/20, y + 8*height/10, + x-height/20, y + 5*height/4); + + GeneralPath MrPeanut = this.get(); + MrPeanut.append(hatRidge, false); + MrPeanut.append(hatTop, false); + MrPeanut.append(leftArmTop, false); + MrPeanut.append(leftArmBottom, false); + MrPeanut.append(RightArmTop, false); + MrPeanut.append(RightArmBottom, false); + MrPeanut.append(monocle, false); + MrPeanut.append(leftLeg, false); + MrPeanut.append(rightLeg, false); + } +} + diff --git a/src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureComponent.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureComponent.java new file mode 100644 index 00000000..48b8cc97 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureComponent.java @@ -0,0 +1,49 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import javax.swing.JComponent; + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @version CS56, W16, 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; + 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/drawings/dkirby/advanced/MultiPictureViewer.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureViewer.java new file mode 100644 index 00000000..01340aed --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MultiPictureViewer.java @@ -0,0 +1,52 @@ +package edu.ucsb.cs56.drawings.dkirby.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, W16 + */ + +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("Dom'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/drawings/dkirby/advanced/Peanut.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/Peanut.java new file mode 100644 index 00000000..6b71d627 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/Peanut.java @@ -0,0 +1,48 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; +import java.awt.geom.GeneralPath; // combinations of lines and curves +import java.awt.Shape; // general class for shapes +import java.awt.Graphics; +import java.awt.Color; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Ellipse2D.Double; +import java.awt.geom.Ellipse2D; +import java.awt.geom.RectangularShape; +import java.awt.Graphics2D; + +import edu.ucsb.cs56.drawings.utilities.ShapeTransforms; +import edu.ucsb.cs56.drawings.utilities.GeneralPathWrapper; + +/** + Two Ovals that when stacked, look mildy like a peanut + + @author Dominic Kirby + @version for CS56, W16, UCSB + +*/ + +public class Peanut extends GeneralPathWrapper implements java.awt.Shape +{ + /* + @param x x coord of the top of the peanut + @param radius, radius of the two bulges of the peanut + */ + public Peanut(double x, double y, double height){ + height = height*2; + Ellipse2D.Double top = + new Ellipse2D.Double(x-height/4,y,height/5, (height/2)); + + Ellipse2D.Double bottom = + new Ellipse2D.Double(x-height/4,y + height/2, + height/5, (height/2)); + + + GeneralPath wholePeanut = this.get(); + wholePeanut.append(top, false); + wholePeanut.append(bottom, false); + + + + + } +} diff --git a/src/edu/ucsb/cs56/drawings/dkirby/advanced/WritePictureToFile.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/WritePictureToFile.java new file mode 100755 index 00000000..e9b5a50f --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/WritePictureToFile.java @@ -0,0 +1,83 @@ +package edu.ucsb.cs56.drawings.dkirby.advanced; + +import java.awt.image.BufferedImage; +import java.awt.Graphics2D; +import java.io.File; +import javax.imageio.ImageIO; +import java.io.IOException; + +/** + * A class with a main method that can write a drawing to a graphics file. + * + * @author P. Conrad, + * @version for CS56, W16, 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 chosen picture to a file. + * + * @param args command line arguments + */ + + public static void main(String[] args) + { + 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); + + 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.drawPicture3(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; + File outputfile = new File(fullFileName); + ImageIO.write(bi, imageType, outputfile); // actually writes file + 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/drawings/dkirby/simple/Circle.java b/src/edu/ucsb/cs56/drawings/dkirby/simple/Circle.java new file mode 100644 index 00000000..fdd92dd4 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/simple/Circle.java @@ -0,0 +1,31 @@ +package edu.ucsb.cs56.drawings.dkirby.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, W16, 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/drawings/dkirby/simple/PictureComponent.java b/src/edu/ucsb/cs56/drawings/dkirby/simple/PictureComponent.java new file mode 100644 index 00000000..2c391832 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/simple/PictureComponent.java @@ -0,0 +1,110 @@ +package edu.ucsb.cs56.drawings.dkirby.simple; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import javax.swing.JComponent; + +// the four tools things we'll use to draw + +import java.awt.geom.Line2D; // single lines +import java.awt.geom.Rectangle2D; + +/** + A component that draws a Picture by Phill Conrad + + @author Phill Conrad (original drawing) + @author @@@ Dominic Kirby(fixed the snowmans's head) + @version for UCSB CS56, W16 +*/ + +// 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 + + Rectangle2D.Double house = new Rectangle2D.Double(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. + + 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 snowManHead = + new Circle( + snowManCenterBottomX, + snowManCenterBottomY - bottomRadius *2 - middleRadius * 2 - topRadius, + topRadius + ); + g2.draw(snowManHead); + + g2.drawString("Added Head Dkirby", 20,20); + + + // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING + // @@@ 20, 20 are suggested coordinates, but you may change them + + // g2.drawString("Description and your name go here", 20,20); + + } +} diff --git a/src/edu/ucsb/cs56/drawings/dkirby/simple/PictureViewer.java b/src/edu/ucsb/cs56/drawings/dkirby/simple/PictureViewer.java new file mode 100644 index 00000000..06cc6917 --- /dev/null +++ b/src/edu/ucsb/cs56/drawings/dkirby/simple/PictureViewer.java @@ -0,0 +1,42 @@ +package edu.ucsb.cs56.drawings.dkirby.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 + * @author ADD YOUR NAME @@@ + * @version CS56, W16, 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("House on a hill"); // @@@ 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); + } +} From 80ffd946f7afc0d3e8a5f5ee9f0be9b8ba1da61b Mon Sep 17 00:00:00 2001 From: Dominic Kirby Date: Fri, 28 Oct 2016 01:16:23 -0700 Subject: [PATCH 2/2] gave him some eyes --- .../drawings/dkirby/advanced/MrPeanut.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java index 1ac24fef..4147b820 100644 --- a/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java +++ b/src/edu/ucsb/cs56/drawings/dkirby/advanced/MrPeanut.java @@ -54,12 +54,21 @@ public MrPeanut(double x, double y, double height){ x + height/10, y + height/2); Ellipse2D.Double monocle = - new Ellipse2D.Double(x - height/5, y + height/10, - height/20, height/20); + new Ellipse2D.Double(x - height/5-height/45 -height/70, + y + height/10-height/70, + height/15, height/15); + + Ellipse2D.Double LEye = + new Ellipse2D.Double(x - height/5-height/45, y + height/10, + height/30, height/30); + + Ellipse2D.Double REye = + new Ellipse2D.Double(x - height/8, y + height/10, + height/30, height/30); Line2D.Double leftLeg = new Line2D.Double(x-height/4, y + 8*height/10, - x - height/4, y + 5*height/4); + x - height/5 -height/15, y + 5*height/4); Line2D.Double rightLeg = new Line2D.Double(x-height/20, y + 8*height/10, @@ -72,7 +81,9 @@ public MrPeanut(double x, double y, double height){ MrPeanut.append(leftArmBottom, false); MrPeanut.append(RightArmTop, false); MrPeanut.append(RightArmBottom, false); - MrPeanut.append(monocle, false); + MrPeanut.append(LEye, false); + MrPeanut.append(REye, false); + MrPeanut.append(monocle,false); MrPeanut.append(leftLeg, false); MrPeanut.append(rightLeg, false); }