将链接列表保存在Notepad.txt中

发布于 2024-08-06 17:05:16 字数 1160 浏览 8 评论 0原文

我绘制了许多三角形多边形并将其存储在链接列表中。我的问题是,当我将绘图存储在记事本文件中时,数据不可读(奇怪的符号)。当我尝试使用 println 打印它时,输出类似于 java.awt.Polygon@1d6096。

如何在记事本中存储多边形的坐标?

... 
java.util.List<Polygon> triangles = new LinkedList<Polygon>();
String pathname = "eyemovement.txt";
...
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y }; 
triangles.add(new Polygon(xs, ys,3));

...
public void actionPerformed(ActionEvent e) {
   if(e.getSource() == saveBtn){
      try {
      FileOutputStream fos = new FileOutputStream(pathname);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(triangles);
      oos.flush();
      oos.close();
      fos.close();
  }
  catch (Exception ex) {
    System.out.println("Trouble writing display list vector");
  }
}

编辑:

我已经尝试了所有建议,但仍然无法获得如下输出。我也尝试过“Printwriter”,但无法解决问题。请帮助我,我的头很沉重:-(

我绘制三角形,进行更改,并将其存储在链接列表中。完成绘制并进行更改后,我单击保存按钮并将其保存在 Notepad.txt 中希望我能在记事本中得到如下输出:

40 60 50 这条线代表三角形 1 的顶点 Xs
40 40 50 这条线代表三角形 1 的顶点 Ys

60 80 70 三角形 2
60 60 70

100 120 110 三角形3
100 100 110

I draw many triangle polygons and store it in Linked List. My problem is that, when I store the drawing in a Notepad file, the data is unreadable (weird symbol). When I try to print it using println the output is like this java.awt.Polygon@1d6096.

How to store the coordinate of the polygon in Notepad?

... 
java.util.List<Polygon> triangles = new LinkedList<Polygon>();
String pathname = "eyemovement.txt";
...
int[] xs = { startDrag.x, endDrag.x, midPoint.x };
int[] ys = { startDrag.y, startDrag.y, midPoint.y }; 
triangles.add(new Polygon(xs, ys,3));

...
public void actionPerformed(ActionEvent e) {
   if(e.getSource() == saveBtn){
      try {
      FileOutputStream fos = new FileOutputStream(pathname);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(triangles);
      oos.flush();
      oos.close();
      fos.close();
  }
  catch (Exception ex) {
    System.out.println("Trouble writing display list vector");
  }
}

EDITED:

I have tried all the suggestions but still I can't managed to get the output as the following. I have tried the "Printwriter" as well, but I cant solved the problem. Help me, please, my head is so heavy with this :-(

I draw the triangles, make changes, and store it in Linked List. After finished drawing, and make changes, I click save button and save it in Notepad.txt with hope that I will get the output in Notepad like this:

40 60 50 this line represents vertices Xs of triangle 1
40 40 50 this line represents vertices Ys of triangle 1

60 80 70 triangle 2
60 60 70

100 120 110 triangle 3
100 100 110

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末 2024-08-13 17:05:16

如果您只想存储坐标,并且只想以一种方式写入(写入文件),那么您应该在 Polygon: 上编写一个覆盖方法:

String toString() {
  return this.x + ", " + this.y;
}

或类似的方法。

If you just want to store co-ordinates, and only want to write one way (into the file) then you should write an override method on your Polygon:

String toString() {
  return this.x + ", " + this.y;
}

or something similar.

昨迟人 2024-08-13 17:05:16

当然,数据是不可读的。它是“数据”,而不是“文本”。您必须使用 ObjectInputStream 类再次读取该文件。使用方法`readObject();该方法返回一个对象。当然,您必须以这种方式投射它:

Object o = ois.readObject(); // ois is the ObjectInputStream
List<Polygon> list = new ArrayList<Polygon>((List) o));

我认为您只想保存三角形以便在关闭程序后继续使用它。

Of course the data is unreadable. It is "Data", not "Text". You have to read the file again with the ObjectInputStream class. Use the method `readObject(); This method returns an Object. Of course you have to cast it on this way:

Object o = ois.readObject(); // ois is the ObjectInputStream
List<Polygon> list = new ArrayList<Polygon>((List) o));

I think you just want to save the triangle to continue working with it after closing your program.

乖乖哒 2024-08-13 17:05:16

实际上没有人发布绝对最简单的方法来做到这一点,所以就这样吧。

取一个Polygon p,输出一个表示p的x/y坐标的字符串(假设p有至少1个点),形式为“ (x1 y1,x2 y2,x3 y3,...)”:

System.out.print("(" + p.xpoints[0] + p.ypoints[0]);
for (int i = 0; i < p.npoints; i++) {
  System.out.print(", " + p.xpoints[i] + " " + p.ypoints[i]);
}
System.out.println(")");

Nobody actually posted the absolute simplest way to do this, so here it goes.

Take a Polygon p, output a string representing the x/y coordinates of p (assuming p has at least 1 point) of the form "(x1 y1, x2 y2, x3 y3, ...)":

System.out.print("(" + p.xpoints[0] + p.ypoints[0]);
for (int i = 0; i < p.npoints; i++) {
  System.out.print(", " + p.xpoints[i] + " " + p.ypoints[i]);
}
System.out.println(")");
半世蒼涼 2024-08-13 17:05:16

我从一个测试用例开始。

import java.awt.Polygon;

import junit.framework.TestCase;

public class PolygonTest extends TestCase {
    public void testToString() throws Exception {
        Polygon polygon = new Polygon();
        polygon.addPoint(0, 1);
        polygon.addPoint(1, 1);
        polygon.addPoint(1, 0);
        assertEquals("(0,1;1,1;1,0)", polygon.toString());
    }
}

我在这里假设您正在使用 awt Polygon 类。该测试失败,因为 awt 的 Polygon 类不会覆盖默认行为。但是 Polygon 有很多你不想失去的好东西(也许),所以为了添加我们想要的新行为(一个 toString() 方法),让我们稍微改变一下:

import java.awt.Polygon;

import junit.framework.TestCase;

public class PolygonTest extends TestCase {
    public void testToString() throws Exception {
        Polygon polygon = new Triangle();
        polygon.addPoint(0, 1);
        polygon.addPoint(1, 1);
        polygon.addPoint(1, 0);
        assertEquals("(0,1;1,1;1,0)", polygon.toString());
    }
}

这甚至无法编译,因为 Triangle 类还不存在。那么让我们创建它(我正在使用 eclipse;我将运行 QuickFix 来为我创建该类):

import java.awt.Polygon;

public class Triangle extends Polygon {

}

现在测试可以编译,但和以前一样失败。那么让我们编写 toString() 方法:

import java.awt.Polygon;

public class Triangle extends Polygon {
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (int i = 0; i < npoints; i++) 
            sb.append(String.format("%s,%s;", xpoints[i], ypoints[i]));
        sb.deleteCharAt(sb.length() - 1); // get rid of the final semicolon
        sb.append(")");
        return sb.toString();
    }
}

现在测试通过了。

请注意,我对您要求的格式做了一些更改,因为我认为您可能希望能够区分点 (5, 17) 和点 (51, 7)。

I start with a test case.

import java.awt.Polygon;

import junit.framework.TestCase;

public class PolygonTest extends TestCase {
    public void testToString() throws Exception {
        Polygon polygon = new Polygon();
        polygon.addPoint(0, 1);
        polygon.addPoint(1, 1);
        polygon.addPoint(1, 0);
        assertEquals("(0,1;1,1;1,0)", polygon.toString());
    }
}

I'm assuming here that you are using the awt Polygon class. This test fails, because awt's Polygon class doesn't override the default behavior. But Polygon has lots of good stuff in it you don't want to lose (maybe), so to add the new behavior we want (a toString() method), let's change this just a little bit:

import java.awt.Polygon;

import junit.framework.TestCase;

public class PolygonTest extends TestCase {
    public void testToString() throws Exception {
        Polygon polygon = new Triangle();
        polygon.addPoint(0, 1);
        polygon.addPoint(1, 1);
        polygon.addPoint(1, 0);
        assertEquals("(0,1;1,1;1,0)", polygon.toString());
    }
}

This doesn't even compile, because the Triangle class doesn't exist yet. So let's create it (I'm using eclipse; I'll run QuickFix to create the class for me):

import java.awt.Polygon;

public class Triangle extends Polygon {

}

And now the test compiles, but fails as before. So let's write the toString() method:

import java.awt.Polygon;

public class Triangle extends Polygon {
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (int i = 0; i < npoints; i++) 
            sb.append(String.format("%s,%s;", xpoints[i], ypoints[i]));
        sb.deleteCharAt(sb.length() - 1); // get rid of the final semicolon
        sb.append(")");
        return sb.toString();
    }
}

and now the test passes.

Note that I changed the format a little from what you requested, because I think you probably want to be able to distinguish between the point (5, 17) and the point (51, 7).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文