Click here to Skip to main content
15,885,655 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
        public class Game extends JPanel{
	private JPanel panel;
	//to show how much is missed and caught
	private JLabel caught, missed;
	//slider to allow speed adjustment
	private JSlider slider;

	private String catchString, missString;
	//icon which shows image
	private ImageIcon creature;
	//set timer
	private Timer run; 
	//for randomizing location
	private Random xPoint, yPoint;
	//for other variables
	private int xCor, yCor, grabX, grabY, a, b, x, y, speed, speed2;
	//for coordinates
	private final int WIDTH, HEIGHT, IMAGE_SIZE, DOT;
	private int SPEED;
		//sets up to display what is missed
		public void setMissedString(int x){
			missString = "Missed: " + x;
		//speeds up the image randomization
		public void setCatchString(int x){
			catchString = "Caught: " + x;
		public String getMissedString(){
			return missString;
		public String getCatchString(){
			return catchString;

           public Game(){
                SPEED = 2500;
		speed2 = 0;
//PROBLEM>>	slider.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent ev){
				 JSlider slider1 = (JSlider) ev.getSource();
				 	speed2 = slider1.getValue();
				 	setSpeed(getSpeed() - (speed2 * 100));

		run = new Timer(SPEED, new ActionListener(){
			public void actionPerformed(ActionEvent ev){
				setXcor(xPoint.nextInt(WIDTH - IMAGE_SIZE));
				setYcor(yPoint.nextInt(HEIGHT) + 50);
                slider = new JSlider(JSlider.HORIZONTAL, 10, 2500, 50);
		panel = new JPanel();
		panel.setPreferredSize(new Dimension(WIDTH - 10,40));
		panel.setLayout(new GridLayout(1,3));
Updated 14-Apr-15 9:16am
Mohibur Rashid 14-Apr-15 19:01pm    
Where is your question?
Member 11607315 16-Apr-15 15:01pm    
the question given was:
Design and implement an application that plays a game called Catch-the-Creature. Use an image to represent the creature. Have the creature appear at random location for a random duration, then disappear and reappear somewhere else. The goal is to “catch” the creature by pressing the mouse button while the mouse pointer is on the creature image. Create a separate class to represent the creature, and include in it a method that determines if the location of the mouse click corresponds to the current location of the creature. Display a count of the number of times the creature is caught. Modifications: Use the slider control to allow the user to change the speed at which the creature moves. You are also required to provide a list or combo box which will allow the users to select different creatures.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900