Question

I am drawing this (in the paint method):

g.drawString("NumPipes: " + numPipes + "   " + "DeletedPipes: " + deletedPipes + "    moved: " + numMoved, 0 , 10);

To output the values defined in these following methods:

public void addPipe() {
    for (int i = 0; i < pipe.length; i++) {
        if (pipe[i] == null) {
            pipe[i] = new Pipe(this.width, (int) Math.random() * (this.height - 200));
            System.out.println("Pipe Added.");
            numPipes += 1;
        }
    }
}

public void deletePipe() {
    for (int i = 0; i < pipe.length; i++) {
        if (pipe[i] != null && pipe[i].returnX() <= 0) {
            pipe[i] = null;
            System.out.println("Pipe Deleted.");
            deletedPipes += 1;
        }
    }
}

public void movePipe(int speed) {
    for (int i = 0; i < pipe.length; i++) {
        if (pipe[i] != null) {
            pipe[i].move(speed);
            numMoved = numMoved + 1;
            System.out.println(numMoved);
        }
    }
}

Now when I check the console for the outputted value of numMoved, the values are increasing/updating but they stay the same in the JFrame (See below image, the console displays the value of numMoved and the JFrame shows the drawString values). What is happening/Why is it doing this?

enter image description here

Was it helpful?

Solution

You don't seem to be calling repaint, so that component doesn't know that it should update the values...

For example...

public void addPipe() {
    for (int i = 0; i < pipe.length; i++) {
        if (pipe[i] == null) {
            pipe[i] = new Pipe(this.width, (int) Math.random() * (this.height - 200));
            System.out.println("Pipe Added.");
            numPipes += 1;
        }
    }
    repaint();
}

Now, you could place repaint after numPipes += 1, but because of the way repaint works, it probably won't make a difference...

This assumes that the class that addPipe belongs to is some kind of component, but you've not supplied enough information to make proper diagnoses...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top