Results 1 to 2 of 2
  1. #1
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Read text file into array and display it

    Text file
    111
    101
    111
    What I want to do is put the whole text file into an array with 2 keys, where the first key corresponds with the horizontal location and where the second key corresponds with the vertical location in the text file.

    Raw array
    array[0][0] = 1
    array[1][0] = 1
    array[2][0] = 1
    array[0][1] = 1
    array[1][1] = 0
    array[2][1] = 1
    array[0][2] = 1
    array[1][2] = 1
    array[2][2] = 1
    As the text file is meant to be altered, it wouldn't be a good idea to do this manually.

    After having created the array, I'd like to recreate the text file, but now every 0 should become a blue box and every 1 a red one. Right now this thread does indeed sound like a request for a finished system, but the previous information was just to tell you what I'd like to do with it so you'll get to understand the next part better. Or maybe you know an even better way of doing this stuff.
    ---
    The contents of the text file can be found at the beginning of the file. It wouldn't be possible for me to write the contents of the java file, as I don't know how to do this. What I do know will be written in Java and what I don't will be written as a comment.

    Java Code:
    import javax.swing.JFrame;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    
    public class main extends JFrame {
        public main() {
            // Creating frame
            super("Map");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(30, 30);
            setVisible(true);
    
            int y = 0;
            int x = 0;
    
            /* read and open file
            int width = get length of first line
            int height = get amount of lines */
    
            while(y < height) {
                while(x < width) {
                    array[width][height] = // character at this location
                    x++;
                }
                y++;
            }
        }
    
        public void paint(Graphics g) {
            y = 0;
            x = 0;
    
            while(y < height) {
                while(x < width) {
                    g.drawRect(x*10, y*10, 10, 10);
    
                    if(array[width][height] == 1) {
                        g.setColor(Color.BLACK);
                    }
    
                    g.fillRect(x*10, y*10, 400, 400);
    
                    x++;
                }
                y++;
            }
        }
    
        public static void main(String[]args) {
            main body = new main();
        }
    }
    I hope you understand what I'm trying to do. If not, please ask a question.

  2. #2
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Read text file into array and display it

    Just ignore this thread. I'll try figuring this out myself.

Similar Threads

  1. read from text file into array of objects
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 12:40 PM
  2. Replies: 2
    Last Post: 04-01-2011, 03:04 PM
  3. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  4. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  5. Replies: 7
    Last Post: 11-01-2010, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •