Skip to content

Commit

Permalink
added animation
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherluo committed Mar 14, 2014
1 parent aeaf0b6 commit 1024ca1
Show file tree
Hide file tree
Showing 47 changed files with 3,299 additions and 129 deletions.
18 changes: 18 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@
<java fork="true" classpathref="project.class.path"
classname="${myPackage}.advanced.MultiPictureViewer" />
</target>

<target name="myAnim" depends="compile"
description="run AnimatedPictureViewer from ${env.USER} subdir">
<java fork="true" classpathref="project.class.path"
classname="${myPackage}.advanced.AnimatedPictureViewer" />
</target>

<target name="jakeAnim" depends="compile"
description="Jake's example Pencil animation (jstaahl)">
<java fork="true" classpathref="project.class.path"
classname="edu.ucsb.cs56.w14.drawings.jstaahl.advanced.AnimatedPictureViewer" />
</target>

<target name="andrewAnim" depends="compile"
description="Andrews' example iPod animation (andrewberls)">
<java fork="true" classpathref="project.class.path"
classname="edu.ucsb.cs56.w14.drawings.andrewberls.advanced.AnimatedPictureViewer" />
</target>

<target name="myMPV2" depends="compile"
description="run MultiPictureViewer from ${env.USER} subdir, arg 2">
Expand Down
30 changes: 30 additions & 0 deletions src/edu/ucsb/cs56/W14/drawings/andrewberls/SimpleGui1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edu.ucsb.cs56.w14.drawings.andrewberls;
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 Andrew Berls
@version CS56, Winter 2014, 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 if you dare");
java.awt.Color myColor = new java.awt.Color(0, 136, 204);
button.setBackground(myColor);
button.setOpaque(true);
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(300,300);
frame.setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.ucsb.cs56.w14.drawings.andrewberls.advanced;

import java.awt.Graphics2D;
import java.awt.geom.Line2D; // single lines
import java.awt.geom.Ellipse2D; // ellipses and circles
import java.awt.geom.Rectangle2D; // for the bounding box
import java.awt.Rectangle; // squares and rectangles
import java.awt.geom.GeneralPath; // combinations of lines and curves
import java.awt.geom.AffineTransform; // translation, rotation, scale
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.w14.drawings.utilities.ShapeTransforms;
import edu.ucsb.cs56.w14.drawings.utilities.GeneralPathWrapper;

/**
* A class with static methods for drawing various pictures
*
* @author Phill Conrad
* @version for CS10, lab06, Spring 2009
*/


public class AllMyDrawings
{
/** Draw a picture with two basic iPods
*/

public static void drawPicture1(Graphics2D g2) {
// Large black iPod in top left
Ipod i1 = new Ipod(100, 100, 200);
g2.setColor(Color.BLACK); g2.draw(i1);

// Make a squashed iPod and move it over
Shape i2 = ShapeTransforms.scaledCopyOfLL(i1,0.75,0.5);
i2 = ShapeTransforms.translatedCopyOf(i2,250,0);
g2.setColor(Color.BLACK); g2.draw(i2);

// Label the drawing
g2.setColor(Color.BLACK);
g2.drawString("An iPod and its squashed twin by Andrew Berls", 20,20);
}


/** Draw a picture with some colored iPods with detail
*/
public static void drawPicture2(Graphics2D g2) {
// Left
Ipod i1 = new Ipod(75, 100, 150);
g2.setColor(Color.RED); g2.draw(i1);

// Middle
Ipod i2 = new Ipod(225, 137, 125);
g2.setColor(Color.BLUE); g2.draw(i2);

// Right
IpodDetail i3 = new IpodDetail(350, 25, 200);
g2.setColor(Color.ORANGE); g2.draw(i3);

// Label the drawing
g2.setColor(Color.BLACK);
g2.drawString("An few iPods lined up by Andrew Berls", 20,20);
}

/** Draw a different picture with iPods all over the place
*/

public static void drawPicture3(Graphics2D g2) {
// Large black iPod in top left
IpodDetail i1 = new IpodDetail(100, 100, 200);
g2.setColor(Color.BLACK); g2.draw(i1);

// Red iPod on the bottom right
Ipod i2 = new Ipod(400, 200, 115);
g2.setColor(Color.RED); g2.draw(i2);

// Rotated blue iPod in upper right
IpodDetail i3t = new IpodDetail(400, 0, 125);
g2.setColor(Color.BLUE);
Shape i3 = ShapeTransforms.rotatedCopyOf(i3t, Math.PI/-2.0);
g2.draw(i3);

// Label the drawing
g2.drawString("A bunch of iPods by Andrew Berls", 20,20);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.ucsb.cs56.w14.drawings.andrewberls.advanced;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AnimatedPictureViewer {

private DrawPanel panel = new DrawPanel();

private Ipod ipod = new Ipod(100, 100, 100);

Thread anim;

private int x = 100;
private int y = 100;

private int dx = 5;

public static void main (String[] args) {
new AnimatedPictureViewer().go();
}

public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(panel);
frame.setSize(640,480);
frame.setVisible(true);

frame.getContentPane().addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
System.out.println("mouse entered");
anim = new Animation();
anim.start();
}

public void mouseExited(MouseEvent e){
System.out.println("Mouse exited");
// Kill the animation thread
anim.interrupt();
while (anim.isAlive()){}
anim = null;
panel.repaint();
}
});

} // go()

class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

// Clear the panel first
g2.setColor(Color.white);
g2.fillRect(0,0,this.getWidth(), this.getHeight());

// Draw the Ipod
g2.setColor(Color.RED);
Ipod test = new Ipod(x, y, 100);
g2.draw(test);
}
}

class Animation extends Thread {
public void run() {
try {
while (true) {
// Bounce off the walls

if (x >= 400) { dx = -5; }
if (x <= 50) { dx = 5; }

x += dx;
panel.repaint();
Thread.sleep(50);
}
} catch(Exception ex) {
if (ex instanceof InterruptedException) {
// Do nothing - expected on mouseExited
} else {
ex.printStackTrace();
System.exit(1);
}
}
}
}

}
96 changes: 96 additions & 0 deletions src/edu/ucsb/cs56/W14/drawings/andrewberls/advanced/CoffeeCup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package edu.ucsb.cs56.w14.drawings.andrewberls.advanced;
import java.awt.geom.GeneralPath; // combinations of lines and curves
import java.awt.geom.AffineTransform; // translation, rotation, scale
import java.awt.Shape; // general class for shapes

// all imports below this line needed if you are implementing Shape
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.Rectangle;
import java.awt.geom.PathIterator;
import java.awt.geom.AffineTransform;

import edu.ucsb.cs56.w14.drawings.utilities.ShapeTransforms;
import edu.ucsb.cs56.w14.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, W11, UCSB, 02/23/2011
*/
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));

}

}
Loading

0 comments on commit 1024ca1

Please sign in to comment.