我写了一些代码,它在 for 循环中逐行读取文本文件;每一行都是用于在 JPanel 上绘制新形状的命令。我有一个 HashMap 用于存储名称和形状。
Map<String, GeoShape> geoObj = new HashMap<String, GeoShape>();
示例输入是这样的:ADD P1 Point Blue 50 50 10
cmd 是一个 String[] ,它包含拆分的输入命令。问题就在这里:
if (cmd[2].equalsIgnoreCase("Point"))
geoObj.put(cmd[1], new Point(cmd, graph));
它陷入无限循环。我在调试模式下运行代码,点类没有问题。
任何帮助是极大的赞赏!
编辑:这是类点
public class Point extends Segment {
private Line2D point;
private Stroke thickness;
private Color fColor;
private double x;
private double y;
public Point(String[] cmd, Graphics2D graph) {
System.out.println("point");
x = Double.parseDouble(cmd[4]);
y = Double.parseDouble(cmd[5]);
try {
fColor = setColor(cmd[3]);
} catch (Exception e) {
e.printStackTrace();
}
thickness = new BasicStroke(Integer.parseInt(cmd[6]));
paint(graph);
}
@Override
public void paint(Graphics2D graph) {
point = new Line2D.Double(x, y, x, y);
graph.setStroke(thickness);
graph.setColor(fColor);
graph.draw(point);
}
}
for循环:
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int linesNum = Integer.parseInt(br.readLine());
String line = br.readLine();
for (int i = 1; i <= linesNum; i++) {
while (line != null) {
cmd = line.split(" ");
if (cmd[0].equalsIgnoreCase("ADD")) {
if (cmd[2].equalsIgnoreCase("Point"))
geoObj.put(cmd[1], new Point(cmd, graph));
// some else if with the same structure for other shapes
//at the end of for loop I have this: line = br.readLine();