forked from UCSB-CS56-W14/CS56-W14-lab06
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from christopherluo/lab07branch
chrisluo: finished lab07 added animation, also updated my object class
- Loading branch information
Showing
4 changed files
with
115 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/edu/ucsb/cs56/W14/drawings/chrisluo/advanced/AnimatedPictureViewer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters