How do I terminate the game below after 30 seconds? I already have 1 timer, so I
ID: 3828714 • Letter: H
Question
How do I terminate the game below after 30 seconds? I already have 1 timer, so I am not sure how to create a 2nd one and where to put it.
I have designed and implemented a game that is simliar to catch the creature (wilbur, in this example). Wilbur appears at a random location for a random duration of time (my first timer), and then disappears to reappear somwhere else. By pressing the mouse button while the pointer is on the image, wilbur is "caught." A count of the number of times Wilbur is caught is dplayed. I have created 3 classes: CatchWilburPanel.java (contains my timer), CatchWilbur.java (the driver), and Wilbur.java.
***CatchWilburPanel.java***
// panel class
package CatchWilbur;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class CatchWilburPanel extends JPanel
{
private Wilbur Wilbur; // instantiates new wilbur object
private Random gen; // instantiates a new random generator
private Timer animate; // instantiates a new timer
public static int seconds = 30;
private final int X = 20, Y = 100;
private final int MOVE_ODDS = 4; // 1 in 4 chance Wilbur will move
private final int DELAY = 500;
// Sets up the panel
public CatchWilburPanel()
{
Wilbur = new Wilbur(X, Y);
gen = new Random();
addMouseListener(new Catcher());
setBackground(Color.white);
setPreferredSize(new Dimension(500, 500));
animate = new Timer(DELAY, new RandomMover());
animate.start();
}
// Draws wilbur and the number of catches
public void paintComponent(Graphics page)
{
super.paintComponent(page);
// paint wilbur here
Wilbur.draw(this, page);
// Update the hit counter for catches and/or misses
String catchString = "Catches: " + Wilbur.getCatchCount();
page.drawString(catchString, 10, 20);
String missString = "Misses: " + Wilbur.getMissCount();
page.drawString(missString, 10, 40);
}
// Inner class listens for mouse presses. If mouse is pressed inside Wilbur,
// move Wilbur and repaint the screen.
public class Catcher extends MouseAdapter
{
private final int X = 20, Y = 100;
public void mouseClicked(MouseEvent event)
{
if (Wilbur.pointInMe(event.getX(), event.getY()))
{
Wilbur.move(getSize());
}
repaint();
}
}
// Inner class listens for timer to move Wilbur & repaint screen during animation.
// Moves Wilbur based on MOVE_ODDS (may not move every time timer fires).
public class RandomMover implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int shouldMove = gen.nextInt(MOVE_ODDS);
if (shouldMove == 0)
{
Wilbur.move(getSize());
repaint();
}
}
}
}
***CatchWilbur.Java***
// driver class
package CatchWilbur;
import javax.swing.JFrame;
public class CatchWilbur
{
// Displays the main frame of the program.
public static void main(String[] args)
{
JFrame frame = new JFrame("Catch Wilbur");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CatchWilburPanel());
frame.pack();
frame.setVisible(true);
}
}
***Wilbur.java***
package CatchWilbur;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Wilbur {
private int X, Y, clickCount, catchCount;
private ImageIcon wilbur; // instantiasted a new ImageIcon
private Random gen;
// Creates Wilbur.
public Wilbur(int initialX, int initialY)
{
wilbur = new ImageIcon("C:\Users\kubert\Documents\NetBeansProjects\CatchtheCreature\src\CatchWilbur\Wilbur.png");
X = X;
Y = Y;
clickCount = catchCount = 0;
gen = new Random();
}
// Moves wilbur to a random location within the play area.
public void move(Dimension area)
{
X = gen.nextInt(area.width - wilbur.getIconWidth());
Y = gen.nextInt(area.height - wilbur.getIconHeight());
}
// Returns true if point (x , y) is in wilbur and increments catch count, else
// returns false.
public boolean pointInMe(int x, int y) // check whether mouse click catches Wilbur
{
clickCount++;
if (x >= X && x <= X + wilbur.getIconWidth())
{
if (y >= Y && y <= Y + wilbur.getIconHeight()) // point is in boudns of Wilbur
{
catchCount++;
return true;
} else
{
return false;
}
} else
{
return false;
}
}
public int getCatchCount() // return number of times Wilbur has been "caught"
{
return catchCount;
}
public int getMissCount() // return the number of times Wilbur has been "missed"
{
return clickCount - catchCount;
}
public void draw(Component panel, Graphics page) // has Wilbur repaint itself
{
wilbur.paintIcon(panel, page, X, Y);
}
}
Explanation / Answer
Ans::
Put these lines in "main" wwhich terminate for 30-sec.//thank you//
code::
// timer --' 30 * 1000 ' for 30 seconds
new Timer(30 * 1000, new ActionListener() {
//override
@Override
// actionPerformed
public void actionPerformed(ActionEvent e) {
//s.o.p wilber.getCatchCount
System.out.println("Catches: " + wilbur.getCatchCount());
// s.o.p wilbur.getMissCount
System.out.println("Misses: " + wilbur.getMissCount());
// exit(o)
System.exit(0);
}
}).start(); //
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.