Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Bag class and Backpack extending Bag #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/edu/ucsb/cs56/drawings/mmaatubang/SimpleGui1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.ucsb.cs56.drawings.mmaatubang;

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 and the Dodgers win the World Series!") ;

java.awt.Color myColor = new java.awt.Color(10,10,255); // 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 src/edu/ucsb/cs56/drawings/mmaatubang/advanced/AllMyDrawings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package edu.ucsb.cs56.drawings.mmaatubang.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 Matt Maatubang
* @version for UCSB CS56, F17
*/

public class AllMyDrawings
{
/** Draw a picture with bags and backpacks
*/

public static void drawPicture1(Graphics2D g2) {

Bag h1 = new Bag(100,250,50,75);
g2.setColor(Color.CYAN); g2.draw(h1);

// Make a black house 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 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

Backpack hw1 = new Backpack(50,350,40,75);
Backpack hw2 = new Backpack(200,350,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.BLACK);
g2.drawString("Bags by Matt Maatubang", 20,20);
}


/** Draw a picture with bags and backpacks
*/
public static void drawPicture2(Graphics2D g2) {

// Draw some coffee cups.

Bag large = new Bag(100,50,225,150);
Bag smallCC = new Bag(20,50,40,30);
Bag tallSkinny = new Bag(20,150,20,40);
Bag shortFat = new Bag(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);

Backpack h1 = new Backpack(100,250,50,75);
g2.setColor(Color.CYAN); g2.draw(h1);

// Make a black house 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 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

Backpack hw1 = new Backpack(50,350,40,75);
Backpack hw2 = new Backpack(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 bags and backpacks by Matt Maatubang", 20,20);
}

/** Draw a different picture with bags and backpacks
*/

public static void drawPicture3(Graphics2D g2) {

// label the drawing

g2.drawString("A bag and backpack by Matt Maatubang", 20,20);


// Draw some coffee cups.

Backpack large = new Backpack(100,50,225,225);
Bag smallCC = new Bag(20,50,40,30);

g2.setColor(Color.RED); g2.draw(large);
g2.setColor(Color.GREEN); g2.draw(smallCC);

}
}
56 changes: 56 additions & 0 deletions src/edu/ucsb/cs56/drawings/mmaatubang/advanced/Backpack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package edu.ucsb.cs56.drawings.mmaatubang.advanced;
import java.awt.geom.GeneralPath;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

/**
A Backpack

@author Matt Maatubang
@version for CS56, F17, UCSB

*/
public class Backpack extends Bag implements Shape
{
/**
* Constructor for objects of class Backpack
*/
public Backpack(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;

double winHt = 0.35 * height;

Rectangle2D.Double win1 =
new Rectangle2D.Double(x + w, winTop, width* 0.8, winHt);
// add the windows to the house
// Look up the meaning of the second parameter of append
// (Hint--is a method of "GeneralPath")

double s = 0.16 * width;
double sTop = y - 0.4 * height;

Rectangle2D.Double side =
new Rectangle2D.Double(x + 0.42 * width, sTop, s, s);

GeneralPath wholeHouse = this.get();
wholeHouse.append(win1, false);
wholeHouse.append(side, false);
}
}
95 changes: 95 additions & 0 deletions src/edu/ucsb/cs56/drawings/mmaatubang/advanced/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package edu.ucsb.cs56.drawings.mmaatubang.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 Bag (wrapper around a General Path, implements Shape)

@author Matthew Maatubang
@version for CS56, F17, UCSB
@credit Adapted from CoffeeCup by Phil Conrad

*/

public class Bag extends GeneralPathWrapper implements Shape
{
/**
* Constructor for objects of class Bag
*/
public Bag(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(150,400);
leftSide.lineTo(135,360);
leftSide.lineTo(110,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(150,400);
topAndBottom.lineTo(450,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 insideHandle = new GeneralPath();

insideHandle.moveTo(250, 100);
insideHandle.curveTo(295, 65, 305, 65, 350, 100);

GeneralPath outsideHandle = new GeneralPath();

outsideHandle.moveTo(220, 100);
outsideHandle.curveTo(290, 40, 310, 40, 380, 100);

GeneralPath wholeBag = new GeneralPath ();
wholeBag.append(topAndBottom, false);
wholeBag.append(leftSide, false);
wholeBag.append(rightSide, false);
wholeBag.append(insideHandle, false);
wholeBag.append(outsideHandle, 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(wholeBag, -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));

}

}
Loading