Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Misterious problem

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Misterious problem

    Hello, I was creating a simple game, and I wanted to test program. But when I press "Run" on eclipse, nothing happens, "Terminate" button is active so I think, that program running. Here is my code:
    import javax.swing.*;
     
    public class MainClass {
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Catch Paulenas");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new GameClass());
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
     
    }

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
     
    public class GameClass extends JPanel{
     
    	//Declaring variables
    	private int numberOfHits;
    	private JLabel resultsBar;
    	private BufferedImage target;
    	private int targetX;
    	private int targetY;
    	private Timer timer;
    	private int tryX;
    	private int tryY;
    	private Random rand;
     
    	//Creating constructor, setting everything default
    	public GameClass(){
    		numberOfHits = 0;
    		setLayout(new BorderLayout());
    		setBackground(Color.WHITE);
    		rand = new Random();
    		getReady();
    	}
     
    	//Continue get ready for game
    	private void getReady(){
    		try{
    			target = ImageIO.read(new File("target.jpg"));
    		}catch(IOException e){}
     
    		resultsBar = new JLabel("Number of hits "+ numberOfHits);
    		add(BorderLayout.SOUTH, resultsBar);
    		addMouseListener(new PanelListener());
    		playGame();
    	}
     
    	//Playing game
    	private void playGame(){
    		while(numberOfHits < 20){
    			timer = new Timer(rand.nextInt(2000)+1000, new TimerListener());
    		}
    	}
     
    	public void paintComponent(Graphics g){
    		g.drawImage(target,targetX,targetY,null);
    	}
     
    	//Listener for pressing buttons
    	private class PanelListener implements MouseListener{
     
    		public void mouseClicked(MouseEvent e) {
    			tryY = e.getY();
    			tryX = e.getX();
    			if(tryY == targetY || tryX == targetX){
    				numberOfHits++;
    			}
    		}
    		public void mouseEntered(MouseEvent arg0) {}
    		public void mouseExited(MouseEvent arg0) {}
    		public void mousePressed(MouseEvent arg0) {}
    		public void mouseReleased(MouseEvent arg0) {}
     
    	}
     
    	//Listener for timer
    	private class TimerListener implements ActionListener{
     
    		public void actionPerformed(ActionEvent e) {
    			targetX = rand.nextInt(400)+1;
    			targetY = rand.nextInt(380)+1;
    			repaint();
    		}
     
    	}
    }

    Maybe you can see where is the problem, because with syntax is everything good, no errors.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Misterious problem

    In your playGame() method: you loop until numberOfHits is less than 20, but you never increment numberOfHits in the loop, so it will always be less than 20. So, you've got an infinite loop.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Misterious problem

    Add some println's in there to see what is being executed....Hint: see the playGame method...the while loop will continue indefinitely

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM