Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top