This site requires JavaScript, please enable it in your browser!
Greenfoot back
Xevinaly
Xevinaly wrote ...

2016/9/15

Using .txt files with the Greenfoot sight

Xevinaly Xevinaly

2016/9/15

#
Does anyone know a way to allow java security to allow the applets to the site to use .txt files. I didn't notice before since I was using Chrome but apparently any program using .txt files will give a Null Pointer Exception. This includes all of my games and I was hoping I would be able to make them playable on the site.
Super_Hippo Super_Hippo

2016/9/15

#
Is there a reason to use a text file? You could write it into the code instead. Edit: Okay, I see the reason. It might be quite crazy when writing all that into the code, but I think that it is the only chance to make it work on the site.
danpost danpost

2016/9/15

#
Xevinaly wrote...
Does anyone know a way to allow java security to allow the applets to the site to use .txt files. I didn't notice before since I was using Chrome but apparently any program using .txt files will give a Null Pointer Exception. This includes all of my games and I was hoping I would be able to make them playable on the site.
Can you show an example of how you are using the txt files? You should be able to use them as long as you do not try to change the contents of the files (you cannot write to them on the site -- but they should be readable if placed within the scenario folder).
Xevinaly Xevinaly

2016/9/15

#
Hmmm, ok I have one place where it writes but it should be an easy code write in. The other spots are used for a custom image class that takes a text file as input. Ill try and fix the first file use and see if it works.
Xevinaly Xevinaly

2016/9/15

#
Ok, never mind. I've been trying to think of a work around but can't find one so I'll explain in more detail. In the game as levels progress the color scheme changes ie. blue and yellow setting on level one, red and orange setting on level two. The problem is that I am using a custom image class I made to extend GreenfootImage. This class takes a text file as input with numbers and dashes and turns it into an image ie. the image below would like like a white rocket. - - 0 0 - - - - 0 0 - - 0 0 - - 0 0 0 0 - - 0 0 The class sets colors using a switch block included below.
if(ID.equals("-"))
            return new Color(0, 0, 0, 0);
        switch((Integer.parseInt(ID) + colorscheme)%8)
        {
            case 0: return Color.WHITE;
            case 1: return Color.RED;
            case 2: return Color.YELLOW;
            case 3: return Color.BLUE;
            case 4: return Color.ORANGE;
            case 5: return Color.PINK;
            case 6: return Color.CYAN;
            case 7: return Color.MAGENTA;
            case 8: return Color.GREEN;
            default: return new Color(0, 0, 0, 0);
        }
To make the color scheme change easy I just need to add a number to the original files number to obtain a different color for the pixel. However, since the class is never added to the world I can't get the variable from any other class and it cannot inherently tell when a level has changed. Therefore I write the needed numebr to a text file and have the class read the file to get the number and just have each image reset itself to use the new color. ie. the pixel represented by 0 from the above image in round one is White and if the colorscheme int is 3 for round three the total is 4 making the pixel now Orange. The main problem is I have no way to change the color scheme without using the r/w text file as the image class cannot interact with anything in the world. I suppose I could create a single instance of a separate class that is passed to all images but I'm not sutre if that would be a very viable or proper solution.
danpost danpost

2016/9/15

#
I think it would be easy enough to just use world class field for the color array. Then, the color scheme could be coded as an array of paired int values representing indecis of that color array. Something like this:
public class MyWorld extends World
{
    private static final Color[] colors =
    {
        Color.WHITE;
        Color.RED,
        Color.YELLOW,
        Color.BLUE,
        Color.ORANGE,
        Color.PINK,
        Color.CYAN,
        Color.MAGENTA,
        Color.GREEN
    };

    private int[][] schemes =
    {
        { 3, 2 }, // for level 1 (level value is 0)
        { 1, 4 }, // for level 2 (level value is 1)
    };
I am not even sure if you need that image class. All you really need is a static method in the same MyWorld class that takes the filename and uses the current level value to create the appropriate image and return it:
public static GreenfootImage getImage(String filename)
{
    GreenfootImage image;
    // build 'image'
    return image;
}
It can be called by any of your actor classes from their constructors:
public Rocket()
{
    setImage(MyWorld.getImage("rocket.txt"));
}
Line 3, the 'setImage' line, can also be coded elsewhere in the class to update the colors of the actor.
You need to login to post a reply.