|
| 1 | +package day5; |
| 2 | + |
| 3 | +import java.applet.AudioClip; |
| 4 | +import java.awt.Dimension; |
| 5 | + |
| 6 | +import java.awt.Graphics; |
| 7 | +import java.awt.Robot; |
| 8 | +import java.awt.event.MouseEvent; |
| 9 | +import java.awt.event.MouseMotionListener; |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | + |
| 12 | +import javax.imageio.ImageIO; |
| 13 | +import javax.swing.JApplet; |
| 14 | +import javax.swing.JFrame; |
| 15 | +import javax.swing.JPanel; |
| 16 | +import javax.swing.SwingUtilities; |
| 17 | + |
| 18 | +public class ScaryMaze extends JPanel implements Runnable, MouseMotionListener { |
| 19 | + |
| 20 | + BufferedImage maze; |
| 21 | + final int frameWidth = 600; |
| 22 | + final int frameHeight = 400; |
| 23 | + |
| 24 | + 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 | + 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 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void mouseMoved(MouseEvent e) { |
| 37 | + int mouseX = e.getX(); |
| 38 | + int mouseY = e.getY(); |
| 39 | + 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 | + |
| 52 | + } |
| 53 | + |
| 54 | + 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 | + |
| 63 | + } |
| 64 | + |
| 65 | + private void showScaryImage(String imageName) { |
| 66 | + try { |
| 67 | + maze = ImageIO.read(getClass().getResource(imageName)); |
| 68 | + } catch (Exception e) { |
| 69 | + System.err.println("Couldn't find this image: " + imageName); |
| 70 | + } |
| 71 | + repaint(); |
| 72 | + } |
| 73 | + |
| 74 | + public static void main(String[] args) throws Exception { |
| 75 | + SwingUtilities.invokeLater(new ScaryMaze()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + JFrame frame = new JFrame("June's Scary Maze"); |
| 81 | + frame.add(this); |
| 82 | + setPreferredSize(new Dimension(frameWidth, frameHeight)); |
| 83 | + frame.pack(); |
| 84 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 85 | + frame.setResizable(false); |
| 86 | + frame.setVisible(true); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void paintComponent(Graphics g) { |
| 91 | + g.drawImage(maze, 0, 0, null); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void mouseDragged(MouseEvent e) {} |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments