forked from UCSB-CS56-F16/F16-lab04
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from dkirby13/master
Adding all of the lab
- Loading branch information
Showing
13 changed files
with
898 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) ; | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
src/edu/ucsb/cs56/drawings/dkirby/advanced/AllMyDrawings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/edu/ucsb/cs56/drawings/dkirby/advanced/HouseWithWindows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.