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 Fish class under gmfoster directory #1

Open
wants to merge 1 commit 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/gmfoster/SimpleGui1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.ucsb.cs56.drawings.gmfoster;

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 I'll give you some candy") ;

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

public static void drawPicture1(Graphics2D g2) {

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

// Make a blackl fish 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 fish 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 fish with fins

FishWithFins hw1 = new FishWithFins(50,350,40,75);
FishWithFins hw2 = new FishWithFins(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 fish by Graham Foster", 20,20);
}


/** Draw a picture with a few fish and coffee cups
*/
public static void drawPicture2(Graphics2D g2) {

// Draw some coffee cups.

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

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

// Make a black fish 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 fish 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 fish with fins and bubbles

FishWithFins hw1 = new FishWithFins(50,350,40,75);
FishWithFins hw2 = new FishWithFins(200,350,200,100);

g2.draw(hw1);
g2.setColor(new Color(0x8F00FF));

// Rotate the second fish 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 fish by Graham Foster", 20,20);
}



public static void drawPicture3(Graphics2D g2) {

// label the drawing

g2.drawString("A bunch of Fish by Graham Foster", 20,20);


// Draw some coffee cups.

Fish large = new Fish(100,50,225,150);
Fish small = new Fish(20,50,40,30);

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

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

}

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

import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;

import edu.ucsb.cs56.drawings.utilities.ShapeTransforms;
import edu.ucsb.cs56.drawings.utilities.GeneralPathWrapper;


public class Fish extends GeneralPathWrapper implements Shape
{

public Fish(double x, double y, double length, double height)
{

GeneralPath tail = new GeneralPath();
tail.moveTo(x,y);
tail.lineTo(x - .25*length ,y + .3*height);
tail.lineTo(x - .25*length ,y - .3*height);
tail.lineTo(x,y);

GeneralPath body = new GeneralPath();
body.moveTo(x + .7*length, y - .4*height);
body.lineTo(x,y);
body.lineTo(x + .7*length, y + .4*height);

Ellipse2D.Double head = new Ellipse2D.Double(x + .4*length, y - .4* height, .8* length, .8* height);


GeneralPath wholeFish = this.get();
wholeFish.append(tail, false);
wholeFish.append(body, false);
wholeFish.append(head, false);
}
}
37 changes: 37 additions & 0 deletions src/edu/ucsb/cs56/drawings/gmfoster/advanced/FishWithFins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.ucsb.cs56.drawings.gmfoster.advanced;
import java.awt.geom.GeneralPath;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

public class FishWithFins extends Fish implements Shape
{
public FishWithFins(double x, double y, double length, double height)
{
super(x,y,length,height);

GeneralPath gp = this.get();

Ellipse2D.Double bub1 =
new Ellipse2D.Double(x + .4 * length, y - 1.2 * height, .1* length, .1 * length);
Ellipse2D.Double bub2 =
new Ellipse2D.Double(x + .6 * length, y -1.5 * height, .12 * length, .12 * length);
Ellipse2D.Double bub3 =
new Ellipse2D.Double(x, y - 1.8 * height, .08 * length, .08 * length);

GeneralPath fin1 = new GeneralPath();
fin1.moveTo(x + .2* length ,y);
fin1.lineTo(x + .4* length ,y);
fin1.lineTo(x + .3* length ,y + .3*height);
fin1.lineTo(x + .2* length ,y);

Ellipse2D.Double eye =
new Ellipse2D.Double(x + length, y - .1 * height, .1 * length, .1 * length);

GeneralPath wholeFish = this.get();
wholeFish.append(bub1, false);
wholeFish.append(bub2, false);
wholeFish.append(bub3, false);
wholeFish.append(eye, false);
wholeFish.append(fin1, false);
}
}
Loading