diff --git a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AllMyDrawings.java b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AllMyDrawings.java index 2ecf99a..483a8e8 100755 --- a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AllMyDrawings.java +++ b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AllMyDrawings.java @@ -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); diff --git a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AnimatedPictureViewer.java b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AnimatedPictureViewer.java new file mode 100755 index 0000000..435708e --- /dev/null +++ b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AnimatedPictureViewer.java @@ -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); + } + } + } + } + +} diff --git a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/Pumpkin.java b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/Pumpkin.java index ce40ac3..a7d2a5b 100644 --- a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/Pumpkin.java +++ b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/Pumpkin.java @@ -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); } - } diff --git a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/PumpkinJack.java b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/PumpkinJack.java index 7df19af..b0d29ae 100644 --- a/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/PumpkinJack.java +++ b/src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/PumpkinJack.java @@ -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 @@ -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); } }