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

标签 java swing bufferedreader filereader jfilechooser

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

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();
        }
    }
}

最佳答案

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

这是一个使用字符串中编码的 map 的示例。

enter image description here

重要的是使用结果的int[][]作为 map 的基本“逻辑”。该屏幕截图只是为了检查行/列逻辑是否混淆。 ;)

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();
    }
}

关于java - 如何获取文本文件中每个字符的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50989134/

相关文章:

java - BufferedReader 是否忽略第一行?

java - 在java中使用缓冲读取器读取行时获取备用列名?

java - 自动更新嵌套项的 TreeView

java - OnClickListener = 向下 + 向上?安卓

java - 这个 Swing 代码是如何工作的?

java - 每当我添加一个对象时,它就会出现在屏幕的一角

java - 使用 BufferedReader 每行读取多个数据值

java - Apache ·希罗. WebUtils.isWeb(...) 和 WebUtils.isHttp(...) 有什么区别?

java - BPF 表达式只捕获 arp-reply 数据包

java - 在一个 JOptionPane 中显示所有文本,而不是逐行显示多个文本