基本平台

时间:2014-04-14 21:20:22

标签: eclipse bufferedreader

我一直在尝试制作一个基本的平台游戏,但除非我能够为Game1Panel读取外部文本文档以获取地图,否则我无法继续使用它。我将txt文档包含在与其他项目和Game1Panel相同的文件夹中,但没有任何变化。当我使用终端打开Game1时,它所带来的只是一个黑屏..我怎样才能在Game1Panel中获取我的bufferedreader来读取Eclipse中的外部txt文档? :/

import javax.swing.JPanel; 
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class Game1Panel extends JPanel implements Runnable {

public static final int WIDTH = 400;
public static final int HEIGHT = 400;

private Thread thread;
private boolean running;

private BufferedImage image;
private Graphics2D g;

private int FPS = 30;
private int targetTime = 1000 / FPS;

private TileMap1 tileMap;

public Game1Panel () {

    super();
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setFocusable(true);
    requestFocus();

}

public void addNotify() {

    super.addNotify();
    if(thread == null) {
        thread = new Thread(this);
        thread.start();

    }
}

public void run() {

    init();

    long startTime;
    long urdTime;
    long waitTime;

    while(running) {

        startTime = System.nanoTime();

        update();
        render();
        draw();

        urdTime = (System.nanoTime() - startTime) / 1000000; 
        waitTime = targetTime - urdTime;

        try{
            Thread.sleep(waitTime);
        }
        catch(Exception e) {
        }
    }
    }

    private void init() {

        running = true;

        image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) image.getGraphics();

        tileMap = new TileMap1("TestMap.txt", 32);

    }

    private void update() {

        tileMap.update();
    }

    private void render() {

        tileMap.draw(g);

    }

    private void draw() {
        Graphics g2 = getGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
    }
}

import java.io.*;
import java.awt.*;

public class TileMap1 {

private int x;
private int y;

private int tileSize;
private int[][] map;
private int mapWidth;
private int mapHeight;

public TileMap1(String s, int tileSize) {

    this.tileSize = tileSize;

    try {

        BufferedReader br = new BufferedReader(new FileReader(s));

        mapWidth = Integer.parseInt(br.readLine());
        mapHeight = Integer.parseInt(br.readLine());
        map = new int[mapHeight][mapWidth];

        String delimiters = " ";
        for(int row = 0; row < mapHeight; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delimiters);
            for(int col = 0; row < mapWidth; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }

    catch(Exception e) {}

}

public void update() {

}

public void draw(Graphics2D g) {

    for(int row = 0; row < mapHeight; row++) {
        for(int col = 0; col < mapWidth; col++) {

            int rc = map[row][col];

            if(rc == 0) {
                g.setColor(Color.BLACK);
            }
            if(rc == 1) {
                g.setColor(Color.WHITE);
            }

            g.fillRect(x + col * tileSize, y + row * tileSize, tileSize, tileSize);

        }
    }
}

}

0 个答案:

没有答案