即使我调用渲染方法

时间:2017-11-18 18:27:17

标签: java while-loop render

为什么我的tick();方法被调用,而不是我的render();方法,这就是问题 所以,我有一个类来保存我的更新和渲染:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class MapI extends Map {

    int players = 8, islands = 8;

    private int dimAm, irAm, golAm, emAm;

    @Override
    public void tick() {
        while(irAm < 48) {
            tickIr();
        }
        while(dimAm < 4) {
            tickDim();
        }
    }

    public void tickDim() {
        try {
            Thread.sleep(20000);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        dimAm += 1;
        System.out.println(dimAm + " diamonds");
    }

    public void tickIr() {
        try {
            Thread.sleep(2000);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        irAm += 1;
        System.out.println(irAm + " iron");
    }

    @Override
    public void render(Graphics g) {
        g.setFont(new Font("Arial", Font.BOLD, 12));
        g.setColor(Color.BLACK);
        g.fillRect(30, 10, 50, 50); // dim gen(s)
        g.drawString(dimAm + "", 41, 29);
        g.setColor(Color.CYAN);
        g.fillRect(40, 20, 30, 30);
        g.setColor(Color.YELLOW);
        g.fillRect(30, 100, 50, 50); // yellow base
    }

}

这是Map类:

import java.awt.Graphics;

public abstract class Map {

    protected int players, islands;

    public abstract void tick();

    public abstract void render(Graphics g);

}

发生的事情是,当我的Game类中的run()方法被调用时,它有while(running),并且在该循环内部是我指定呈现和更新的位置:

public void run(){

    init();

    while(running){
        render();
        tick();
    }

    stop();

}

public synchronized void start(){
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop(){
    if(!running)
        return;
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

有了这个,我应该渲染和更新,但是,截至目前,所有这些都是更新。我认为我的问题可能会附带

@Override
public void tick() {
    while(irAm < 48) {
        tickIr();
    }
    while(dimAm < 4) {
        tickDim();
    }
}

,它位于MapI内部。我认为正在发生的事情是,除非这些循环中没有一个仍然在运行,否则将无法呈现,但话又说回来,我不知道如何绕过它。
我的GameAspect类包含了render方法:

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import blaze.game.gfx.Assets;
import blaze.game.maps.MapI;

public class GameAspect implements Runnable {

    private Display display;
    public int width, height;
    public String title;

    private boolean running = false;
    private Thread thread;

    private BufferStrategy bs;
    private Graphics g;

    private MapI map1;

    public GameAspect(String title, int width, int height){
        this.width = width;
        this.height = height;
        this.title = title;

        map1 = new MapI();
    }

    private void init(){
        display = new Display(title, width, height);
        Assets.init();
    }

    private void tick(){
        map1.tick();
    }

    private void render(){
        bs = display.getCanvas().getBufferStrategy();
        if(bs == null){
            display.getCanvas().createBufferStrategy(3);
            return;
        }
        g = bs.getDrawGraphics();
        //Clear Screen
        g.clearRect(0, 0, width, height);
        //Draw Here!

        map1.render(g);

        //End Drawing!
        bs.show();
        g.dispose();
    }

    public void run(){

        init();

        while(running){
            render();
            tick();
        }

        stop();

    }

    public synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running)
            return;
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

0 个答案:

没有答案