This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2022/6/11

act and main method

Roshan123 Roshan123

2022/6/11

#
How to make main method (or any user defined method) work like greenfoot act method?
Roshan123 Roshan123

2022/6/11

#
Saw some discussions but can't really find it helpful, in other words I want to know how to make it if it's possible
danpost danpost

2022/6/11

#
Roshan123 wrote...
How to make main method (or any user defined method) work like greenfoot act method?
The greenfoot act method has no content. Simply:
public void act()
{
}
Spock47 Spock47

2022/6/12

#
Roshan123 wrote...
How to make main method (or any user defined method) work like greenfoot act method?
Well, the question is a little bit ambiguous. It is not obvious what you mean with "work like Greenfoot act method". 1. danpost already gave an answer to one interpretation of it. 2. Another interpretation would be that you want to use Greenfoot in a normal Java project, ie. "use Greenfoot library, but not the Greenfoot IDE". That is possible, an explanation on how to do that is here at tab "Documentation", scroll down to "Going further" and check "Running Greenfoot projects on Netbeans."; this can be done more or less similarly with any other IDE. 3. Then again, you probably mean whether it is possible to (re-)implement Greenfoot. That is possible, the people here did implement it, so it is possible. You find the source code of Greenfoot at tab "Download" and then the link "source code" at lower right. But they already did the work, so best way is to just use it (see point 2). However, if you want to reimplement the Greenfoot algorithm as a training exercise, that is possible, too. In fact, I did this some months ago within some days - obviously a light-weight variant with only the most important features, not the whole library and not the IDE. Note: I implemented it in Kotlin, but Kotlin is basically Java with a little bit cleaned-up syntax, and fully compatible with Java. I hope some of these answers helped you. Please let me know, if you need more details to any of this - or whether you meant the question in another way than danpost or I understood it.
Roshan123 Roshan123

2022/6/13

#
danpost wrote...
The greenfoot act method has no content. Simply:
public void act()
{
}
Thanks for it.
Spock47 wrote...
the question is a little bit ambiguous.
Sorry for that. As you know that main method is not called again and again. So I was thinking to make a user defined method which will be called simultaneously and continuously for every secs like greenfoot act method. I think by writing "while(ture){....}", it will behave like a clone of greenfoot act method but I am not sure whether using while loop is a good choice or not. If their is any better idea then please do share it with me
Spock47 Spock47

2022/6/13

#
Yes, in principle, that is the way to do it, but as you have suspected, there are some refinements one should consider: 1. We want to end (or probably only pause) the execution at some point - that's basically a general rule. Because of that, one normally never uses "while (true)", but e.g. having an attribute (e.g. "active") and using this as condition: "while (active)". That gives a simple way to pause or end the simulation - from within this thread or even from other threads (e.g. UI of pause button). 2. We want that each step/frame has (roughly) the same duration: the time difference between each of the e.g. 60 calls to act should be the same. For this we need to interrupt the execution until it is time for the next call - best without blocking the processor. Greenfoot uses "Thread.sleep(millis, nanos)" for that (see Greenfoot source code: util/HDTimer). With this information you can implement your central application method (Greenfoot has this in Simulation::runContent, but obviously their version is more sophisticated, having breaks, nano precision and much more). Here is a sketch:
public class Simulation {
    private boolean active = true;
    private long stepDuration = 20;
    private World world;
    
    public void run() {
        long lastCallTime;
        while (active) {
            lastCallTime = System.currentTimeMillis();
            world.act();

            final long timeTillNextStep = lastCallTime + stepDuration - System.currentTimeMillis();
            if (timeTillNextStep > 0) {
                Thread.sleep(timeTillNextStep);
            }
        }
    }    
} 
danpost danpost

2022/6/13

#
What are you looking to do in the long run with creating a act loop? Really, whatever the answer may be, you probably should just use greenfoot. It was created by the team to make your job a lot easier (so beginners can just dive into coding without having to work with all the unpleasantries.
Roshan123 Roshan123

2022/6/13

#
@Spock47, thanks for the answer. Will go into the details after some time..... @danpost, No. Not actually. I am trying to make a clone of greenfoot “act” method in BlueJ and I guess BlueJ doesn't have any feature/method like Greenfoot's act method
You need to login to post a reply.