Java 如何获取文本文件中每个字符的坐标?
如何获取文件中每个字符的坐标?我的程序允许用户使用JFileChooser打开文件并读取文件内容。由于我使用JFileChooser专门打开的文件是二维地图,因此我必须获得文件中每个字符的坐标。目前,我的代码如下所示:Java 如何获取文本文件中每个字符的坐标?,java,swing,bufferedreader,filereader,jfilechooser,Java,Swing,Bufferedreader,Filereader,Jfilechooser,如何获取文件中每个字符的坐标?我的程序允许用户使用JFileChooser打开文件并读取文件内容。由于我使用JFileChooser专门打开的文件是二维地图,因此我必须获得文件中每个字符的坐标。目前,我的代码如下所示: import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.BufferedReader; import java.io.File; import ja
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();
}
}
}
将文本文件的行号用作行,将每行中的字符位置用作列 下面是一个使用
字符串编码的映射的示例
重要的是使用int[][]
作为映射的基本“逻辑”。该屏幕截图只是为了检查行/列逻辑是否混淆
import java.awt.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
导入java.util.ArrayList;
导入java.io.*;
公共类映射编辑器{
字符串映射
=“10100101\n”
+“10010101\n”
+“11011101\n”
+ "11000001";
public int[]readMapFromString()引发IOException{
ArrayList MapString=新的ArrayList();
ByteArrayInputStream bais=新的ByteArrayInputStream(map.getBytes());
InputStreamReader isr=新的InputStreamReader(BAI);
BufferedReader br=新的BufferedReader(isr);
字符串s=br.readLine();
while(s!=null){
mapStrings.add(s);
s=br.readLine();
}
int h=mapStrings.get(0.length();
int w=mapStrings.size();
int[][]map=新int[w][h];
对于(int ii=0;ii),您的代码有什么问题?您有示例输入吗?以及预期的输出吗?
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();
}
}