0

I'm working on a game/simulator and I need a timer that shows the elapsed time since the game begun.

I'm using a "SimpleWindow" class as the window, called SimulationWindow, for where the visuals of the simulation is presented. The main class (Named Simulate) does most of the calculations.

I want the clock shown in the SimulationWindow and NOT in the main class. What I mean is that I don't want to use the System.out.println to show the result in the main class Simulation (I know how to do that), but the result needs to be automatically updated every second within the SimulationWindow.

I'm trying to make a function in the SimulationWindow class but can't get it to work. It needs to start counting as soon as the SimulationeWindow opens (when the game starts).

Code from the main class, the Simulation class:

import java.util.ArrayList;
import java.util.Random;

public class Simulate {
public static void main(String[] args)
        throws InterruptedException {

    SimulationWindow w = new SimulationWindow();
    Random rand = new Random();

    ArrayList<Player> player = new ArrayList<Player>();
    ArrayList<Player> hPlaying= new ArrayList<Player>();

    // long startTime = System.nanoTime();
    int gameOn = 1;

    // Adds all players to the game before it starts.

    for (int i = 1; i < 11; i++) {

        int randNbr = rand.nextInt(3);

        if (randNbr == 0) {
            player.add(new BlueM(w, i, randNbr));
        } else if (randNbr == 1) {
            player.add(new RedM(w, i, randNbr));
        } else if (randNbr == 2) {
            player.add(new BlueS(w, i));
        } else if (randNbr == 3) {
            player.add(new RedS(w, i));
        } else if (randNbr == 4) {
            player.add(new BlueJ(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 5) {
            player.add(new RedJ(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 6) {
            player.add(new BlueA(w, i,
                    rand.nextInt(50)));
        } else if (randNbr == 7) {
            player.add(new RedA(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 8) {
            player.add(new BlueT(w, i,
                    1 + rand.nextInt(5)));
        } else if (randNbr == 9) {
            player.add(new RedT(w, i,
                    rand.nextInt(50)));
        }

    }

    // Makes all players alive.
    for (Player h : player) {
        hPlaying.add(h);
    }

    // Starts the game.
    while (gameOn > 0) {

        long startTime = System.nanoTime();

        for (Player h : player) {

            // If the player is alive, continue
            if (hPlaying.contains(h)) {
                h.Step();
            }
        }
        SimulationWindow.delay(10);

        long currentTime = System.nanoTime() - startTime;
        SimulationWindow.timer(currentTime);
    }

    // long currentTime = System.nanoTime() - startTime;

}

}

But I don't know how to send the currentTime in the main class to the function in the SimulationWindow that's supposed to return the time so that it can be displayed in this SimulationWindow and updated every second (or everytime a player takes a step, doesn't matter).

This is what I have in the SimulationWindow;

import se.lth.cs.pt.window.SimpleWindow;

public class SimulationWindow extends SimpleWindow {
public static final int X_START_POS_BLUE = 790;
public static final int X_END_POS_BLUE = 10;
public static final int Y_LINE_START_BLUE = 10;
public static final int Y_LINE_END_BLUE = 790;
public static final int X_START_POS_RED = 10;
public static final int X_END_POS_RED = 790;
public static final int Y_LINE_START_RED = 790;
public static final int Y_LINE_END_RED = 10;

public SimulationWindow() {
    super(800, 690, "game x");
    setUp();
}

private void setUp() {
    super.moveTo(X_START_POS_BLUE - 2,
            Y_LINE_START_BLUE + 2);
    super.writeText("1");
    super.moveTo(X_START_POS_RED - 2,
            Y_LINE_START_RED + 2);
    super.writeText("2");
    super.moveTo(X_START_POS_BLUE - 2,
            Y_LINE_START_BLUE + 0);
    super.writeText("3");
    super.moveTo(X_START_POS_RED - 2,
            Y_LINE_START_RED + 0);
    super.writeText("4");
    super.moveTo(X_START_POS_BLUE - 0,
            Y_LINE_START_BLUE + 2);
    super.writeText("5");
    super.moveTo(X_START_POS_RED - 0,
            Y_LINE_START_RED + 2);
    super.writeText("6");
    super.moveTo(X_START_POS_BLUE - 0,
            Y_LINE_START_BLUE + 4);
    super.writeText("7");
    super.moveTo(X_START_POS_RED - 0,
            Y_LINE_START_RED + 4);
    super.writeText("8");
    super.moveTo(X_START_POS_BLUE - 4,
            Y_LINE_START_BLUE + 0);
    super.writeText("9");
    super.moveTo(X_START_POS_RED - 4,
            Y_LINE_START_RED + 0);
    super.writeText("10");

    super.moveTo(395, 780);
    super.println("Time passed: "+ timer())
}

public static int getStartXPos(int startNbr) {
    if (startNbr == 1 || startNbr == 3 || startNbr == 5
            || startNbr == 7 || startNbr == 9) {
        return X_START_POS_BLUE;
    } else {
        return X_START_POS_RED;
    }
}

public static int getStartYPos(int startNbr) {
    if (startNbr == 1 || startNbr == 3 || startNbr == 5
            || startNbr == 7 || startNbr == 9) {
        return Y_LINE_START_BLUE + startNbr * 20;
    } else {
        return Y_LINE_START_RED + startNbr * 20;
    }

}

public static long timer(long time) {
    return time;

}

}

This, in the SimulationWindow class, is the code I can't get to work:

super.writeText("Time passed: "+ timer())

Grateful for any help!

djokerndthief
  • 455
  • 2
  • 4
  • 16
  • 2
    Why can't you call SimulationWindow.timer(currentTime); ? Am I missing something ? Of course, you need mark timer() method as static – Vasu Nov 14 '16 at 21:41
  • I forgot the static part, thanks alot! But when the function now looks like this: public static long timer(long currentTime) { long time = currentTime; return time; I can't seem to call it: super.writeText("Time passed: "+ timer(long)) – djokerndthief Nov 14 '16 at 22:19
  • Can you add the complete code for both SimulationWindow and Simulation classes ? – Vasu Nov 14 '16 at 22:23

1 Answers1

0

As given in my comments, you can make the timer() method static and then call it as SimulationWindow.timer(currentTime);:

static timer method:

public static long timer(long time); {
    return time;
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • I got it so far and that is now corrected. Thank you. However I can't seem to get it to work in the SimulationWindow to show the elapsed time. I'm trying this: super.moveTo(395, 780); super.println("Time passed: "+ timer()) I'm sorry I don't know how to format the code in a comment... :/ – djokerndthief Nov 14 '16 at 22:26
  • Can you add the complete code for both SimulationWindow and Simulation classes ? update ur question – Vasu Nov 14 '16 at 22:30
  • The super.println("Time passed: "+ timer()) is wrong, and it says: "The method timer(long) in the type SimulationWindow is not applicable for the arguments." – djokerndthief Nov 14 '16 at 23:29