Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question Need help with a timer/thread/loopish thing.

    Hi.
    I've made a little game with a timer down the right corner. I want the timer to stop when you press Escape, currently if you press escape the menu comes up but the timer continues.

    Edit: Int tic = 30; >.<

    And don't mind the "SetVisible", this run method starts the game, setVisible makes the game frame visible..

    PHP Code:
    public void run() {
    		setVisible(true);
    		for(int dup = 0; dup < 30; dup++){
    			try {
    				tic--;
    				Thread.sleep(1000);
    			} catch (Exception ex) {}
    				left.setText("Time left: " + tic);
    				
    				if(tic == 0){
    					finish b = new finish();
    					new finish();
    					panel.setEnabled(false);
    					randomNumber = 1;
    					randomNumber2 = 1;
    					
    				}
    		}
    		
    	}
    This timer starts at 30 and counts down as time passes.
    I tried to do like if(rest = false):thread.sleep(1000), else if(rest = true) thread.sleep(10000000), the rest boolean was connected to the escape button..that way of doing it worked, kinda, the timer stopped when you pressed escape. But it didn't start again when you press "Resume" ^-^


    And also, is there a JFrame command like: setMoveable(false)?
    Last edited by AndroidAppNewbie; 02-27-2011 at 11:05 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you taking care not to call Thread.sleep(...) on the EDT, the main Swing thread? Myself, I'd use a Swing Timer for this, and would stop the Timer simply by calling stop() on it.

  3. #3
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Okey, i replaced my timer with the swing timer, but if i write timer.stop(); inside my keyPressed method, "timer" gets underlined with red(AKA it can't reach into the run method), how should i do to make KeyEvent.VK_ESCAPE start the "timer.stop()" :/

    public void run() and public void keyPressed() is both in the same class.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    Okey, i replaced my timer with the swing timer, but if i write timer.stop(); inside my keyPressed method, "timer" gets underlined with red(AKA it can't reach into the run method), how should i do to make KeyEvent.VK_ESCAPE start the "timer.stop()" :/
    Without code, it's hard to "see" what your exact error is. Is the Timer a local variable? If so and you're trying to access it from within an inner class? If this is the problem, then consider declaring it as final.

  5. #5
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Here's the code of the class including run and keypressed..
    The other classes don't have anything with the timer to do, except the main class that starts the run method..
    PHP Code:
    public class full extends JFrame implements Runnable, KeyListener{
    	
    	
    	
    	
    	
    	
    	int tic = 30;
    	int points = 0;
    	int randomNumber = new Random().nextInt(453) + 30;
    	int randomNumber2 = new Random().nextInt(220) + 30;
    	int colorz = new Random().nextInt(5) + 1;
    	int woop1 = randomNumber - 4;
    	int woop12 = randomNumber + 17;
    	int woop2 = randomNumber2 - 26;
    	int woop22 = randomNumber2 + 1;
    	int ass = 1000000000;
    	public boolean rest = false;
    	JButton butt = new JButton("Woop");
    	private JPanel panel;
    	private JLabel label;
    	private JLabel label2;
    	private JLabel left;
    	public boolean end = false;
    	
    	
    	
    		public void run(){
    			setVisible(true);
    			 Timer timer = new Timer(1000, new Mover());
    			 
    			    timer.start();
    			    try {
    			    	
    			      Thread.sleep(12000);
    			      
    			    } catch (InterruptedException e) {}
    			    timer.stop();
    			    
                    left.setText("Time left: " + tic);
    				
    				if(tic == 0){
    					finish b = new finish();
    					new finish();
    					panel.setEnabled(false);
    					randomNumber = 1;
    					randomNumber2 = 1;
    					
    				}
    				
    		}
    				
    		
    		
    	
    	
    	  public void paint(Graphics g){
    	    	super.paintComponents(g);
    	    	this.setBackground(Color.WHITE);
    	    	
    	    	
    	    	g.setColor(Color.BLUE);
    	    	if(colorz == 5){
    	    		g.setColor(Color.YELLOW);
    	    		points += 10;
    	    	}else if(tic == 0){
    	    		g.setColor(Color.BLACK);
    	    	}
    	    	g.fillRect(randomNumber, randomNumber2, 20, 20);
    	    	
    	    	
    	    	
    	    	
    	    }
    	public full(){
    		super("Click-Game");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(false);
    		setSize(500, 300);
    		
    		panel = new JPanel();
    		setResizable(false);
    		add(panel,BorderLayout.CENTER);
    		setLocation(700, 100);
    		addKeyListener(this);
    		setFocusable(true);
    		
    		label = new JLabel("Wooooooooooooop");
    		label2 = new JLabel("        You have " + points + " points");
    		left = new JLabel("Time left: " + tic);
    		add(left, BorderLayout.SOUTH);
    		panel.add(label);
    		panel.add(label2);
    		panel.setBackground(Color.BLACK);
    		panel.setForeground(Color.WHITE);
    	
    		label2.setForeground(Color.WHITE);
    		label.setForeground(Color.WHITE);
    		
    		Mover move = new Mover();
    		panel.addMouseListener(move);
    		panel.addMouseMotionListener(move);
    		
    		
    		
    	}
    
    
    	
    	
    	
    	public void keyPressed(KeyEvent e) {
    	  //Ain't working :/
    		timer.stop();
    			new finish();
    		}
    		
    	}
    	
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		
    		
    	}
    I'll add the classwhich includes the actionlistener to the timer aswell:
    PHP Code:
    private class Mover implements MouseMotionListener, MouseListener, ActionListener{
    
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			
    			left.setText("Time left: " + tic);
    			tic--;
    			
    			if(tic == 0){
    				finish b = new finish();
    				new finish();
    				panel.setEnabled(false);
    				randomNumber = 1;
    				randomNumber2 = 1;
    				
    			}
    			
    		}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Some random questions and thoughts:

    • What's the purpose of having the JFrame class implement Runnable and the run method?
    • What's the purpose of the Thread.sleep(12000)?
    • If you want to control a state of an object from another object, you often do so via a public method. Since you want to stop a Timer held in one object by another object, consider making the Timer a private class field, and give the class a public method to stop the Timer (checking that it's not null first).
    • Swing painting should be done from within a paintComponent method override held in a JPanel or other JComponent.

  7. #7
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    I've modified the code now abit, but i still don't really know how to make a timer. the "Thread.sleep(12000) is supposed to be 32000, and it makes the timer work, hard to explain. Though if someone could tell me how to make a proper timer, then please tell me.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    ....the "Thread.sleep(12000) is supposed to be 32000, and it makes the timer work, hard to explain.

    If you can explain it better, perhaps we can help you better.



    .... Though if someone could tell me how to make a proper timer, then please tell me.

    Please have a look here: How to Use a Swing Timer

Similar Threads

  1. system creats unsense Timer thread-0
    By henry123 in forum Advanced Java
    Replies: 0
    Last Post: 02-20-2011, 10:23 AM
  2. Timer won't execute untill daemon thread yields
    By dsollen in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-24-2009, 07:17 AM
  3. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  4. Thread vs Timer
    By dawiz001 in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-07-2009, 08:25 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •