Search code examples
javamazelanterna

Java lanterna Terminal put in java.properties file?


I'm quite new to programming (had it for a Semester now) and I'm supposed to use the Lanterna Libary (in my final project) to make a terminal in which I can run later a very simple Maze game with walls and static and dynamic "enemies" as well as some key objects which the player has to pick up. Sadly I have no clue what so ever how this would work. I put already around 10h in researching the matter but the only thing it brought me was frustration.

I found a tutorial on youtube where javax.swing is used to just make a Konsole.add(new map()) phrase which is getting the map from a class which reads the whole thing into java from a .txt. I was hoping to do something like that but as said I have to just the lanterna terminal and read the map in from a java.properties file.

Is it possible to do that ? Or do I have to use a totally different Approach? I looked on the website from lanterna and read throw it a few times but couldn't get really anything out of it that helped me. I also checked any post which included lanterna in it on this website but it just furthered my knowledge just a tiny bit. Any kind of tip or suggestion even if it is just a link to another article would help me.


Solution

  • Well, first of all you have to initialize the lanterna console, this will bring up a new swing window which represents your lanterna console.

    Lanterna

    You can use something like this:

    //Init Lanterna terminal
    Terminal terminal = TerminalFacade.createTerminal();
    //Will bring up the terminal window
    terminal.enterPrivateMode();
    //Optional: you can hide the cursor so it wont blink
    terminal.setCursorVisible(false);
    

    so now you can use the methods terminal.moveCursor(x, y) and terminal.putCharacter(char); to print out your map. And when you terminated your program you should call

    terminal.exitPrivateMode();
    

    Properties file

    So the first thing you have to is read the properties file in.

    String filename = ""; //Filename or filepath to your .properties file
    Properties properties = new Properties();
    try {
        InputStream inputStream = new FileInputStream(filename);
        properties.load(inputStream);
        inputStream.close();
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
    

    To retrieve data from the Properties object you can use the methods properties.propertyNames() and properties.getProperty("PropertyName")

    Enumeration<?> propertyNames = properties.propertyNames();
    while (propertyNames.hasMoreElements()) {
        String name = (String) propertyNames.nextElement();
        String value = properties.getProperty(name);
        System.out.println("Name: "+name+"\tValue: "+value);
    }
    

    this prints out every entry from the .properties file. You now have the process the key-value pairs and probably store them in an array or collection. Then you can write a method to print them out to your Lanterna console and you can easily update them or save them back to a .properties file. Hope this helped a little bit :)