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 12 of 12

Thread: replacing thread with timer

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default replacing thread with timer

    	//###########################################################################################################
    	/*** stat method ***/
    	public void start()
    	{
    		Thread thread = new Thread(this); //set up thread
    		thread.start();                   //start thread jump inside run method
    	}/*** end of start method ***/
     
     
    	//###########################################################################################################
    	/*** run method ***/
    	public void run()
    	{
     
    		while(true) //main game loop
    		{
    			//move image here
    			x += dx;
     
    			if(x+width >= getWidth())
    			{
    				x -= dx;
    			}
     
    			repaint();
     
    			try
    			{
    				Thread.sleep(17);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    		}//end of while loop
    	}/*** end of run method ***/



    i want to keep same structure but replacing thread with timer.


    in start method i can set and start timer.
    public void start()
    	{
    		Timer timer = new Timer(17,this); 
    		timer.start();                  
    	}/*** end of start method ***/

    but not sure about run method bc run method it is thread method.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    run method it is thread method.
    The run() method can be used by any code. It doesn't belong only to a thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: replacing thread with timer

    for thread i was implements Runnable. but with timer do i use TimerTask ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    What does the API doc say is needed for the Timer class you are using?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: replacing thread with timer

    what is api?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    Here's a link to the API doc:
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: replacing thread with timer

    i look under javax.management.timer but i didnt see it what iam looking for. am i missing some thing?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    The most commonly used timers are in the java.util and the javax.swing packages.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: replacing thread with timer

    just want to make sure. if you use timer than u replace run() method with
    public void actionPerformed(ActionEvent e)? for ex

    public void actionPerformed(ActionEvent e)
    {
              while(true) //main game loop
    		{
    			//move image here
    			x += dx;
     
    			if(x+width >= getWidth())
    			{
    				x -= dx;
    			}
     
    			repaint();
     
    			try
    			{
    				timer.delay(17);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    Please make a small simple program that compiles, executes and shows the problem.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: replacing thread with timer

    here try compiling this. as you can see it will flicker. let me know if you know how to fix this problem.



    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JApplet;
    import javax.swing.Timer;
     
     
    public class main extends JApplet  implements ActionListener
    {
    	//player information variable
    	int x = 10;
    	int y = 50;
    	int width = 30;
    	int height = 30;
    	int dx = 1;
     
    	//#####################
    	/*** init method ***/
    	public void init()
    	{
    		setSize(800, 400);
    	}/*** end of init method ***/
     
     
     
    	//#####################
    	/*** stat method ***/
    	public void start()
    	{
    		Timer timer = new Timer(30, this); 
    		timer.start();                  
    	}/*** end of start method ***/
     
     
    	//##########################
    	/*** game loop ***/
    	public void actionPerformed(ActionEvent e)
    	{
    			//move image here
    			x += dx;
     
    			if(x+width >= getWidth())
    			{
    				x -= dx;
    			}
     
    			repaint();
    	}/*** end of run method ***/
     
     
     
     
    	//#################
    	/*** paint method ***/ 
    	public void paint(Graphics g)
    	{	
    		super.paint(g); //redraw paint method. so it doesnt draw on top of each other
     
     
    		g.fillRect(x, y, width, height);   //player	
     
    	}/** end of paint method ***/
    }



    i am think probem might be with my eclipe.


    i have eclipse indigo and runing

    name=Eclipse Platform
    id=org.eclipse.platform
    version=3.7.0

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: replacing thread with timer

    Try getting double buffering for the drawing by using a Swing component and the paintComponent() method.

    I get a black square moving to the right when I execute this.
    Not any flicker.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replacing File Content
    By asheshrocky in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2012, 09:48 PM
  2. How to convert thread.sleep() into a timer in an Applet
    By all_pro in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:45 AM
  3. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  4. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  5. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM