forked from League-Archive/IntroToJavaWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.java
More file actions
104 lines (81 loc) · 2.83 KB
/
Maze.java
File metadata and controls
104 lines (81 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package day5;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Maze extends JPanel implements Runnable, MouseMotionListener {
BufferedImage maze;
final int frameWidth = 800;
final int frameHeight = 400;
Maze() throws Exception {
//1. Use this online tool to make a maze image and drop it into your day5 package: http://pixlr.com/editor/
maze = ImageIO.read(getClass().getResource("maze.jpg"));
//2. set the mouse pointer to the start of your maze using:
new Robot().mouseMove(28,31);
//3. add a mouse motion listener using:
addMouseMotionListener(this);
}
@Override
public void mouseMoved(MouseEvent e) {
int mouseX = e.getX();
int mouseY = e.getY();
int mouseColor = maze.getRGB(mouseX, mouseY);
//4. print the mouseColor variable to see what color the mouse is touching
System.out.println(mouseColor);
//5. make a variable to hold the background color.
int backround=-1;
//6. if the mouse falls off the path (if it is on the background)
if(mouseColor ==backround){
scare();
}
// call the scare method
//10. if the mouse is on the end color
if(mouseColor ==-16777216){JOptionPane.showMessageDialog(null, "you won");}
// pop up a message to tell them they won
}
private void scare() {
System.out.println("BOO!");
//7. find a scary sound and put it in the day5 package where you put your maze picture. You can find a sound on freesound.org. Log in as leagueofamazing/code4life.
AudioClip sound = JApplet.newAudioClip(getClass().getResource("sound.wav"));
//8. play the scary sound. Hint: type "sound" and then a period.
sound.play();
//9. drop an image into your day5 package, and use the showScaryImage method to scare your victim!
showScaryImage("scare.jpg");
}
private void showScaryImage(String imageName) {
try {
maze = ImageIO.read(getClass().getResource(imageName));
} catch (Exception e) {
System.err.println("Couldn't find this image: " + imageName);
}
repaint();
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Maze());
}
@Override
public void run() {
JFrame frame = new JFrame("Jonathan's Maze");
frame.add(this);
setPreferredSize(new Dimension(frameWidth, frameHeight));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(maze, 0, 0, null);
}
@Override
public void mouseDragged(MouseEvent e) {}
}