Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Java Canvas being implemented differently

This topic is 2111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I seen implementations of the Game Canvas being treated like this:

 

one as a threaded canvas running independently like this

 

public class Game extends Canvas implements Runnable
{
 
 
}

 

the other one being treated as a main thread of execution: not threaded

 

public class Game extends Canvas
{
 
}

 

Is there a reason to thread a Canvas versus not threading a canvas? Any future issues with either architecture?

Share this post


Link to post
Share on other sites
Advertisement

I think that the only difference between them is where you put the run method:

// First example
public class Game extends Canvas implements Runnable {
    public void startThread() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        // Run code here
    }
}
 
 
// Second example
public class Game extends Canvas {
    public void startThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // Run code here
            }
        });
        thread.start();
    }
}

 

Ultimately, you need to have a thread and some object that implements the Runnable interface; these differ on whether it's an anonymous implementation or not, though I have no idea about whether there's a performance difference.

Share this post


Link to post
Share on other sites

I think that the only difference between them is where you put the run method:

// First example
public class Game extends Canvas implements Runnable {
    public void startThread() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        // Run code here
    }
}
 
 
// Second example
public class Game extends Canvas {
    public void startThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // Run code here
            }
        });
        thread.start();
    }
}

Ultimately, you need to have a thread and some object that implements the Runnable interface; these differ on whether it's an anonymous implementation or not, though I have no idea about whether there's a performance difference.

The thing is, I was able to get my game to run but I did not need to use threads to achieve this. It seems the Game class is by default is a main thread of execution so it is considered a thread without using the syntax to actually make it threaded. right?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!