|
12 | 12 | import javax.imageio.ImageIO; |
13 | 13 | import javax.swing.JApplet; |
14 | 14 | import javax.swing.JFrame; |
| 15 | +import javax.swing.JOptionPane; |
15 | 16 | import javax.swing.JPanel; |
16 | 17 | import javax.swing.SwingUtilities; |
17 | 18 |
|
18 | 19 | public class ScaryMaze extends JPanel implements Runnable, MouseMotionListener { |
19 | | - |
| 20 | + |
20 | 21 | BufferedImage maze; |
21 | | - final int frameWidth = 600; |
22 | | - final int frameHeight = 400; |
| 22 | + final int frameWidth = 800; |
| 23 | + final int frameHeight = 600; |
23 | 24 |
|
24 | 25 | ScaryMaze() throws Exception { |
25 | | - //1. Use this online tool to make a maze image and drop it into your day5 package: http://pixlr.com/editor/ |
| 26 | + // 1. Use this online tool to make a maze image and drop it into your |
| 27 | + // day5 package: http://pixlr.com/editor/ |
26 | 28 | maze = ImageIO.read(getClass().getResource("maze.png")); |
27 | | - //2. set the mouse pointer to the start of your maze using: |
28 | | - //new Robot().mouseMove(xPosition, yPosition) |
29 | | - |
30 | | - //3. add a mouse motion listener using: |
31 | | - //addMouseMotionListener(this) |
32 | | - |
| 29 | + // 2. set the mouse pointer to the start of your maze using: |
| 30 | + // new Robot().mouseMove(xPosition, yPosition) |
| 31 | + new Robot().mouseMove(50, 100); |
| 32 | + |
| 33 | + // 3. add a mouse motion listener using: |
| 34 | + // addMouseMotionListener(this) |
| 35 | + addMouseMotionListener(this); |
33 | 36 | } |
34 | 37 |
|
35 | 38 | @Override |
36 | 39 | public void mouseMoved(MouseEvent e) { |
37 | 40 | int mouseX = e.getX(); |
38 | 41 | int mouseY = e.getY(); |
39 | 42 | int mouseColor = maze.getRGB(mouseX, mouseY); |
40 | | - //4. print the mouseColor variable to see what color the mouse is touching |
41 | | - |
42 | | - //5. make a variable to hold the background color. |
43 | | - |
44 | | - //6. if the mouse falls off the path (if it is on the background) |
45 | | - |
46 | | - // call the scare method |
47 | | - |
48 | | - //10. if the mouse is on the end color |
49 | | - |
50 | | - // pop up a message to tell them they won |
51 | | - |
| 43 | + // 4. print the mouseColor variable to see what color the mouse is |
| 44 | + // touching |
| 45 | + System.out.println(mouseColor); |
| 46 | + // 5. make a variable to hold the background color. |
| 47 | + int bc = -12553419; |
| 48 | + // 6. if the mouse falls off the path (if it is on the background) |
| 49 | + if (bc == mouseColor) { |
| 50 | + // call the scare method |
| 51 | + scare(); |
| 52 | + } |
| 53 | + // 10. if the mouse is on the end color |
| 54 | + if (mouseColor == -4120798) { |
| 55 | + JOptionPane.showMessageDialog(null, "YOU WON!"); |
| 56 | + } |
| 57 | + // pop up a message to tell them they won |
| 58 | + |
52 | 59 | } |
53 | 60 |
|
54 | 61 | private void scare() { |
55 | | - System.out.println("BOO!"); |
56 | | - //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. |
57 | | - //AudioClip sound = JApplet.newAudioClip(getClass().getResource("scream.wav")); |
58 | | - |
59 | | - //8. play the scary sound. Hint: type "sound" and then a period. |
60 | | - |
61 | | - //9. drop an image into your day5 package, and use the showScaryImage method to scare your victim! |
62 | | - |
| 62 | + //System.out.println("BOO!"); |
| 63 | + // 7. find a scary sound and put it in the day5 package where you put |
| 64 | + // your maze picture. You can find a sound on freesound.org. Log in as |
| 65 | + // leagueofamazing/code4life. |
| 66 | + AudioClip sound = JApplet.newAudioClip(getClass().getResource("scream.wav")); |
| 67 | + |
| 68 | + // 8. play the scary sound. Hint: type "sound" and then a period. |
| 69 | + sound.play(); |
| 70 | + // 9. drop an image into your day5 package, and use the showScaryImage |
| 71 | + // method to scare your victim |
| 72 | + showScaryImage("scare.jpg"); |
63 | 73 | } |
64 | 74 |
|
65 | 75 | private void showScaryImage(String imageName) { |
@@ -92,9 +102,7 @@ public void paintComponent(Graphics g) { |
92 | 102 | } |
93 | 103 |
|
94 | 104 | @Override |
95 | | - public void mouseDragged(MouseEvent e) {} |
| 105 | + public void mouseDragged(MouseEvent e) { |
| 106 | + } |
96 | 107 |
|
97 | 108 | } |
98 | | - |
99 | | - |
100 | | - |
|
0 commit comments