forked from League-Archive/IntroToJavaWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScaryMaze.java
More file actions
executable file
·116 lines (93 loc) · 3.17 KB
/
ScaryMaze.java
File metadata and controls
executable file
·116 lines (93 loc) · 3.17 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package section5;
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 ScaryMaze extends JPanel implements Runnable, MouseMotionListener {
BufferedImage maze;
final int frameWidth = 500;
final int frameHeight = 500;
ScaryMaze() throws Exception {
// 1. Use this online tool to make a maze image and drop it into your section5
// package: https://www.pixilart.com/
maze = ImageIO.read(getClass().getResource("maze.png"));
// 2. Change the line of code above so that it matches your maze's file name
// 3. Set the mouse pointer to the start of your maze using:
new Robot().mouseMove(130, 90);
// 4. 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);
// 5. Print the mouseColor variable
System.out.println(mouseColor);
// 6. Run your program and put your mouse over the background to find out what
// color it is
// 7. Create an int variable that holds the background color.
int cow = -16777216;
// 8. If the mouse falls off the path (if it is on the background)
if(mouseColor==cow) {
// call the scare method
scare();
}
// 13. If the mouse is on the end color
if(mouseColor == 65536) {
// pop up a message to tell them they won
JOptionPane.showMessageDialog(null,"You have won");
}
}
private void scare() {
System.out.println("BOO!");
// 9. Find a scary sound and put it in the section5 package where you put your
// maze picture. You can find a sound on freesound.org. Log in as
// leagueofamazing/code4life.
// 10. Use the code below to load your sound. Change the file name to match the
// name of your sound file.
// AudioClip sound =
// JApplet.newAudioClip(getClass().getResource("standardScarySound.wav"));
// 11. Play the scary sound. Hint: type "sound" and then a period.
// 12. Drop an image into your section5 package, and use the showScaryImage
// method to scare your victim!
//showScaryImage("download.jpeg");
}
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 ScaryMaze());
}
@Override
public void run() {
JFrame frame = new JFrame("Scary 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, frameWidth, frameWidth, null);
}
@Override
public void mouseDragged(MouseEvent e) {
}
}