2d image app

Last Updated:

  1. alexenduro

    alexenduro Member

    Hi everibody! I have to create an app that draws a map reading data from a text file : for example the text file can be like

    . index x y orientation
    vertex 1 0 0 0
    vertex 2 5 0 0.5
    vertex 3 10 2 0.3

    so i need to do in java something like fscanf does in c (fscanf scans the text file for known formatted data... example, in the file i written i know each line has a string float float float and i need to put them into a specific data structure that can be a list of elements "vertex"

    next, when i have all the data, draw a simple image rappresenting the data..

    my questions are...
    there is a function like fscanf?

    and then.. what's better for drawing the 2d map? i have tu use Opengl? At the end i have to draw the image pixel by pixel.. i hope there is something easier then openGL :)

  2. jonbonazza

    jonbonazza Well-Known Member

    You need to use an input stream. There are various options here, but the one I would use is a DataInputStream.
  3. alexenduro

    alexenduro Member

    Thanks a lot Jonbonazza! It seems perfect ! And about the drawing, ideas?
  4. alexenduro

    alexenduro Member

    today i was trying to use that but i have got some problems to read string with datainputsream.. how can i do if the file its like

    newyork 1 2
    rome 5 7

    so I know each line begins with a string but i dont know how long it is and i need to put the names in a String value?In c its easy, i simply put something like fscanf(file,"%s %d %d\n",name,x,y); and %s gets the string, no matter how long it is..

    Thanks for help!
  5. jonbonazza

    jonbonazza Well-Known Member

    It doesn't matter how long it is. to read the first line of the above text, you would do this:
    Code (Text):
    2. DataInputStream dis = new DataInputStream(new FileInputStream(new File(filename)));
    3. String city = dis.readUTF();
    4. int param1 = dis.readInt();
    5. int param2 = dis.readInt();
    It will automatically read in the proper number of bytes and convert it to the proper datatype.

    EDIT: Actually, I made a mistake. DataInputStream reads and writes binary files. If you want to read a text file, you will want to just use a FileInputStream.

Share This Page