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

Trevor Lab 04, EighthNotes extends Notes #9

Merged
merged 1 commit into from
Nov 3, 2016
Merged
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/trevorhecht/SimpleGui1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.ucsb.cs56.drawings.trevorhecht;

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!!!!") ;

java.awt.Color myColor = new java.awt.Color(204,000,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) ;
}
}
98 changes: 98 additions & 0 deletions src/edu/ucsb/cs56/drawings/trevorhecht/advanced/AllMyDrawings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package edu.ucsb.cs56.drawings.trevorhecht.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 houses
*/

public static void drawPicture1(Graphics2D g2) {

Note h1 = new Note(100,250,50);
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

// HouseWithWindows hw1 = new HouseWithWindows(50,350,40,75);
// HouseWithWindows hw2 = new HouseWithWindows(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("A few houses by Phill Conrad", 20,20);
}


/** Draw a picture with a few houses and coffee cups
*/
public static void drawPicture2(Graphics2D g2) {
EighthNote h1 = new EighthNote(100,250,50);
g2.setColor(Color.CYAN); g2.draw(h1);

Shape h2 = ShapeTransforms.scaledCopyOfLL(h1,0.5,0.5);
h2 = ShapeTransforms.translatedCopyOf(h2,150,0);



Stroke thick = new BasicStroke (4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);

Stroke orig=g2.getStroke();
g2.setStroke(thick);
g2.setColor(new Color(0x002FA7));

g2.setStroke(orig);
g2.setColor(Color.BLACK);


}

/** Draw a different picture with a few houses and coffee cups
*/




}
31 changes: 31 additions & 0 deletions src/edu/ucsb/cs56/drawings/trevorhecht/advanced/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.ucsb.cs56.drawings.trevorhecht.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, 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 */
}
}
85 changes: 85 additions & 0 deletions src/edu/ucsb/cs56/drawings/trevorhecht/advanced/CoffeeCup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package edu.ucsb.cs56.drawings.trevorhecht.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));

}

}
66 changes: 66 additions & 0 deletions src/edu/ucsb/cs56/drawings/trevorhecht/advanced/EighthNote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package edu.ucsb.cs56.drawings.trevorhecht.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 EighthNote extends Note implements Shape
{

public EighthNote(double x, double y, double radius) //house
{
super(x,y,radius);

double circleradius = radius;
double lineheight = radius * 4.5;
double linewidth = radius * .42;
double Circle_x = x;
double line_x = x + radius;
double Circle_y = y;
double line_y = y - 4.5*radius;



Circle secondcircleOfNote = new Circle
(
Circle_x + lineheight * .7,
Circle_y,
circleradius
);

Rectangle2D.Double secondLine =
new Rectangle2D.Double(line_x + linewidth, .12* lineheight ,
lineheight * .7 - linewidth, linewidth);

Rectangle2D.Double thirdLine =
new Rectangle2D.Double(line_x + lineheight * .7, line_y,
linewidth, lineheight);




GeneralPath note = this.get();
note.append(secondcircleOfNote, false);
note.append(secondLine, false);
note.append(thirdLine, false);




}

}

68 changes: 68 additions & 0 deletions src/edu/ucsb/cs56/drawings/trevorhecht/advanced/House.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package edu.ucsb.cs56.drawings.trevorhecht.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);
}
}
Loading