1. Check out our companion app, Forums for Android! Download from Google Play

2d image app

Discussion in 'Application Development' started by alexenduro, Oct 12, 2011.

  1. alexenduro

    alexenduro Member
    Thread Starter
    5

    Oct 10, 2011
    6
    0
    5
    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 :)
     

    Advertisement

  2. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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
    Thread Starter
    5

    Oct 10, 2011
    6
    0
    5
    Thanks a lot Jonbonazza! It seems perfect ! And about the drawing, ideas?
     
  4. alexenduro

    alexenduro Member
    Thread Starter
    5

    Oct 10, 2011
    6
    0
    5
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    It doesn't matter how long it is. to read the first line of the above text, you would do this:
    Code (Text):
    1.  
    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();
    6.  
    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

Loading...