Skip to content

Commit

Permalink
Merge pull request #22 from christopherluo/lab07branch
Browse files Browse the repository at this point in the history
chrisluo: finished lab07 added animation, also updated my object class
  • Loading branch information
kjorg50 committed Mar 22, 2014
2 parents 643270c + 1024ca1 commit 77bbac8
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void drawPicture1(Graphics2D g2) {
g2.draw(p2);


PumpkinJack pj1 = new PumpkinJack(25,25,50,50);
PumpkinJack pj1 = new PumpkinJack(150,150,50,50);
PumpkinJack pj2 = new PumpkinJack(50,50,75,50);

g2.draw(pj1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package edu.ucsb.cs56.w14.drawings.chrisluo.advanced;

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

public class AnimatedPictureViewer {

private DrawPanel panel = new DrawPanel();

private PumpkinJack pj = new PumpkinJack(150, 150, 150, 150);

Thread anim;

private int x = 210;
private int y = 150;

private int dx = 5;
private int dy = 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.BLACK);
g2.fillRect(0,0,this.getWidth(), this.getHeight());

// Draw the Pumpkin
g2.setColor(Color.ORANGE);
PumpkinJack test = new PumpkinJack(x, y, x, y);
g2.draw(test);
}
}

class Animation extends Thread {
public void run() {
try {
while (true) {

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

}
7 changes: 2 additions & 5 deletions src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/Pumpkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ public Pumpkin(double x, double y, double width, double height)
Ellipse2D.Double circle =
new Ellipse2D.Double(x,y,width,height);

Line2D.Double stem =
new Line2D.Double (x+1.25*x,y-0.05*y,x+0.5*x,y+3.5);

// put the whole house together
Line2D.Double stem =
new Line2D.Double (x+width/2,y,x+width/2,y-height/6);

GeneralPath wholePumpkin = this.get();
wholePumpkin.append(circle, false);
wholePumpkin.append(stem, false);

}

}
23 changes: 19 additions & 4 deletions src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/PumpkinJack.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ public PumpkinJack(double x, double y, double width, double height)
GeneralPath gp = this.get();

Ellipse2D.Double eye1 =
new Ellipse2D.Double(x-25,y-25,x-25,y-25);
new Ellipse2D.Double(x+width/6,y+height/4,width*.25,height*.25);
Ellipse2D.Double eye2 =
new Ellipse2D.Double(x+25, y-25,width-10, height +10);
new Ellipse2D.Double(x+width/1.7,y+height/4,width*.25, height*.25);
Line2D.Double mouth =
new Line2D.Double(x, y, width, height);
new Line2D.Double(x+width/4.5, y+height/1.35, x+width/1.35, y+height/1.35);
Line2D.Double nose =
new Line2D.Double(x+width/2,y+height/2.2,x+width/2,y+height/1.65);
Line2D.Double mouth1 =
new Line2D.Double(x+width/4.5, y+height/1.35, (x+width/4.5)*.9, (y+height/1.35)*.9);
Line2D.Double mouth2 =
new Line2D.Double(x+width/1.35, y+height/1.35, (x+width/1.35)/.9195, (y+height/1.35)*.9);
Ellipse2D.Double eye3 =
new Ellipse2D.Double((x+width/4),y+height/3,width*.1,height*.1);
Ellipse2D.Double eye4 =
new Ellipse2D.Double((x+width/1.5),y+height/3,width*.1,height*.1);

// add the eyes to the pumpkin
// Look up the meaning of the second parameter of append
Expand All @@ -47,7 +57,12 @@ public PumpkinJack(double x, double y, double width, double height)
GeneralPath wholepumpkin = this.get();
wholepumpkin.append(eye1, false);
wholepumpkin.append(eye2, false);
wholepumpkin.append(mouth, false);
wholepumpkin.append(mouth, false);
wholepumpkin.append(nose, false);
wholepumpkin.append(mouth1, false);
wholepumpkin.append(mouth2, false);
wholepumpkin.append(eye3, false);
wholepumpkin.append(eye4,false);
}

}

0 comments on commit 77bbac8

Please sign in to comment.