Question: Hello, I am trying to create this Java program which is called Mouse Trap game. Below are the instructions on how the game should work
Hello,
I am trying to create this Java program which is called Mouse Trap game. Below are the instructions on how the game should work along with the code I have developed so far. Problem is when I save, compile and run it just brings up a gray play area and nothing happens when I move my mouse over the play area. Anyone have any ideas why this might not be working?
Assignment:
For this assignment, you are to write a GUI program that plays a sick game. Heres the idea:
Creates a GUI with a playing/drawing area. The size of the area should be 500 by 500 pixels. Alternatively, you can generate a dialog box(es) to let the user set the size.
Your program should randomly determine a position where the mouse trap is hiding. To do this, you would generate a random number for the x position and the y position. That point (x,y) is the mouse trap. However, it isnt just the point, but any point plus or minus 10 pixels (both in the x and y coordinates). For example, if your random point was (123,345), then the trap is considered when the x position is from 103-133 AND the y position is from 335-355. (More on where the x,y position is to follow.
Produce a color picker (see chapter 13) to allow the user to pick a mouse droppings color. J You should default to a dark brown.
The game begins when the mouse pointer is over your game area. The player tries to move the mouse around the board. As the mouse moves, you should capture the events (see chapter 14) and draw the mouse droppings in the desired color (see 3) on the screen.
The player continues to move the mouse, trying to color the board. But, as each mouse dropping is produced, you are to determine if the mouse has sprung the trap, i.e. has entered the trap area.
When the mouse enters the trap, you are to put up a message box with the total number of mouse droppings.
My code below:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; import java.util.Scanner; class CatchTheRat extends JFrame {
// The Rat
JLabel lb;
// Move it randomly!
Random r;
public CatchTheRat(int k) {
// Set frame properties
setTitle("Catch The Rat"); setLayout(new FlowLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true);
// Set the background (just for a good look)
setContentPane(new JLabel(new ImageIcon("background.jpg")));
// Set layout to the content pane getContentPane().setLayout(new FlowLayout());
// Create the rat
lb=new JLabel(new ImageIcon("rat.png"));
// Add the rat
getContentPane().add(lb);
// Create Random object
r=new Random();
// Create a timer and call it for every k seconds
new Timer(k,new ActionListener(){
public void actionPerformed(ActionEvent ae) {
// Move the rat randomly, subtract 75, so that the rat should not meet the edges lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
// Add mouselistener, notify when user clicks it!
lb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me) {
// Create a beep sound when clicked to notify
Toolkit.getDefaultToolkit().beep();
// Also print it!
System.out.println("Caught!");
}
});
// Maximize the frame
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[]) {
// Create Scanner object
Scanner s=new Scanner(System.in);
// Let the user enter his capability of catching the rat!
System.out.println("Enter the speed");
// Read the input
int k=s.nextInt();
// Create the frame and send the value of k
new CatchTheRat(k);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
