【问题标题】:Objects get handled wrong when read from text file, why?从文本文件中读取对象时处理错误,为什么?
【发布时间】:2017-10-19 20:33:04
【问题描述】:

我从存储在单独行的文本文件中读取对象(实际上是字符串,但我将它们转换为对象)。

编辑:我的问题是,当对象被添加到面板时,它们会相互重叠。这些对象有自己的独立 X 和 Y 坐标,它们应该被绘制在其中。

像这样(这是我从中读取的保存的文本文件):

Described,Bus,182,73,PlaceWithDesc,random desc
Named,Bus,53,31,Place1
Named,train,84,100,Place2

tokens[0] 只是一个硬编码字符串

[1] 是一种运输类别(它是一个对象,当它在图片面板上绘制时,它会赋予该地点的三角形颜色)

[2] 是对象 Position 的 x 坐标,[3] 是 Y 坐标。

[4] 是构建对象时的 Place 名称。

如果 [0] 等于 Described,则还会创建带有描述的地方。

            FileReader inFile = new FileReader("place.reg");
            BufferedReader in = new BufferedReader(inFile);

            String line;
            while ((line = in.readLine()) != null) {
                String[] tokens = line.split(",");  
                Category category = null;
                String categoryName = tokens[1];

                if (categoryName.equals("Bus")) {
                    category = transportCategory[0];
                } else if (categoryName.equals("Underground")) {
                    category = transportCategory[1];
                } else if (categoryName.equals("Train")) {
                    category = transportCategory[2];
                } else if (categoryName.equals("None")) {
                    category = transportCategory[3];
                }

                String placeName = tokens[0];
                int x = Integer.parseInt(tokens[2]);
                int y = Integer.parseInt(tokens[3]);
                Position pos = new Position(x, y);
                String name = tokens[4];

                if (placeName.equals("Named")) {
                    NamedPlace nPlace = new NamedPlace(pos, category, name);

                    add(nPlace);
                    picturePanel.add(nPlace);
                    nPlace.addMouseListener(placeMouseLis);

                    System.out.println("named place added: " + 
nPlace.position().getX() + " , " + pos.getX());
                } else if (placeName.equals("Described")) {
                    String description = tokens[5];
                    DescribedPlace dPlace = new DescribedPlace(pos, 
category, name, description);

                    add(dPlace);
                    picturePanel.add(dPlace);

                    System.out.println("desc place was added");
                }
            }
                 //  Iterator<Map.Entry<Position, Place>> iterator = 
                 //  allPlaces.entrySet().iterator();
                 //  while(iterator.hasNext()){
                 //  Place place = iterator.next().getValue();
                 //  picturePanel.add(place);
                }
                inFile.close();
                in.close();

现在我的问题是,当我添加 Place 对象时,它们会相互重叠。请注意,NamedPlace 和 DescribedPlace 是超类 Place 的子类。

编辑:我通过执行 picturePanel.repaint(); 解决了它和picturePanel.validate();

【问题讨论】:

标签: java


【解决方案1】:

我是最大的白痴..我忘了做 repaint();和验证();读入所有对象后,在我的图片面板上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2020-08-11
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多