如何获取文本文件中每个字符的坐标?

时间:2018-06-22 13:32:44

标签: java swing bufferedreader filereader jfilechooser

如何获取文件中每个字符的坐标?我的程序允许用户使用JFileChooser打开文件并读取文件的内容。由于我使用JFileChooser特别打开的文件是二维地图,因此我必须获取文件中每个字符的坐标。目前,我的代码如下:

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Scanner;

public class MapEditor extends JFrame {

    public static void main(String[] args) throws FileNotFoundException {
        MapEditor mp = new MapEditor();
    }

    public MapEditor() throws FileNotFoundException {
        getFile();
        readMapFromFile();

    }

    File file;

    public File getFile() {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("txt", "html", "log"));
        int state = fc.showOpenDialog(null);

        if (state == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
        } else {
            System.out.println("Selection cancelled");
            System.exit(0);
        }
        return file;
    }

    public void readMapFromFile() throws FileNotFoundException {
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将文本文件的行号用作行,将每行中的字符位置用作列。

这是一个使用以String编码的地图的示例。

enter image description here

重要的是,将产生的int[][]用作地图的基本“逻辑”。该屏幕截图只是为了检查行/列逻辑是否混淆。 ;)

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.util.ArrayList;
import java.io.*;

public class MapEditor {

    String map
            = "10100101\n"
            + "10010101\n"
            + "11011101\n"
            + "11000001";

    public int[][] readMapFromString() throws IOException {
        ArrayList<String> mapStrings = new ArrayList();
        ByteArrayInputStream bais = new ByteArrayInputStream(map.getBytes());
        InputStreamReader isr = new InputStreamReader(bais);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        while (s != null) {
            mapStrings.add(s);
            s = br.readLine();
        }
        int h = mapStrings.get(0).length();
        int w = mapStrings.size();
        int[][] map = new int[w][h];
        for (int ii=0; ii<w; ii++) {
            s = mapStrings.get(ii);
            for (int jj = 0; jj<h; jj++) {
                map[ii][jj] = Integer.parseInt(s.substring(jj, jj+1));
            }
        }
        return map;
    }

    public MapEditor() throws IOException {
        int[][] map = readMapFromString();
        JPanel p = new JPanel(new GridLayout(map.length, map[0].length,0,0));
        p.setBorder(new LineBorder(Color.RED));
        BufferedImage wallImage = 
                new BufferedImage(40, 40, BufferedImage.TYPE_INT_RGB);
        BufferedImage passageImage = 
                new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
        ImageIcon wallIcon = new ImageIcon(wallImage);
        ImageIcon passageIcon = new ImageIcon(passageImage);
        for (int ii=0; ii<map.length; ii++) {
            for (int jj=0; jj<map[0].length; jj++) {
                int i = map[ii][jj];
                JLabel l = new JLabel();
                if (i==0) {
                    l.setIcon(passageIcon);
                } else {
                    l.setIcon(wallIcon);
                }
                p.add(l);
            }
        }
        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args) throws IOException {
        MapEditor mp = new MapEditor();
    }
}