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

2014/8/22

How do I include an external file in my game when I share it?

Griffon Griffon

2014/8/22

#
In a class im taking in collage they're teaching me how to make .html files. In one of the games I made and uploaded (http://www.greenfoot.org/scenarios/11288) I had an in game help, but it wasn't the best, so today I made an html file and found how to open it whenever I clicked on the button and it worked. I uploaded the new version but nothing happened . Apparently it didn't include the .html file when it was uploaded. Is there a way to fix this?
danpost danpost

2014/8/23

#
Did you place the html file inside your project folder and refer to its location there before uploading?
Griffon Griffon

2014/8/23

#
I called the game conquest and I put it in the folder named that (I assume that is the project folder), but how do I refer to its location like you said? EDIT: this is the code I used to open the html file (if that helps)
1
2
3
4
try{
     File htmlFile = new File("Conquest Help.html");
     Desktop.getDesktop().browse(htmlFile.toURI());}
catch(Exception e){}
and the html file uses images from the game. example:
1
<img src="images/Ax.png" height="25" width="25" alt="Pickaxe Icon"/>
danpost danpost

2014/8/23

#
You need to refer to it by its URL. I know there is a discussion with the needed code somewhere, but have been unable to locate it. You could look at my TextFile Viewing World Class scenario code. It opens up text files and reads them in on the site.
Griffon Griffon

2014/8/23

#
Your TextFile world class is interesting, I might try that instead. Also either I'm not understanding what your saying or your not understanding what I'm saying. The html file I'm referring to is a .txt file with html code. I changed the .txt to .html so it opens in a web page and looks the way I designed it to look. It's not a public website and requires no internet to run, just a web browser, so it's an actual file on my computer. It's URL address is C:\ whatever.
danpost danpost

2014/8/23

#
Uploaded scenario cannot access files on your local computer. It (the file) needs to be uploaded to a server (or uploaded with your project) and then its URL will be used to address it.
davmac davmac

2014/8/23

#
It's URL address is C:\ whatever.
But that's a file on your computer. If it's bundled inside the scenario, it won't have that address when someone else views it on their computer.
Griffon Griffon

2014/8/23

#
danpost wrote...
Uploaded scenario cannot access files on your local computer. It (the file) needs to be uploaded to a server (or uploaded with your project) and then its URL will be used to address it.
So I should include the full c:/ address? Anything else?
davmac wrote...
But that's a file on your computer. If it's bundled inside the scenario, it won't have that address when someone else views it on their computer.
This is exactly what I was trying to say.
davmac davmac

2014/8/23

#
You need to refer to it by the URL it has when it is uploaded. For various reasons this isn't really possible (the file will be inside a jar file, and the browser doesn't know how to look inside a jar file).
Griffon Griffon

2014/8/24

#
So it's not possible...
danpost danpost

2014/8/24

#
Griffon wrote...
today I made an html file and found how to open it whenever I clicked on the button and it worked.
Please post the code you used. I would like to see it and experiment with it.
Griffon Griffon

2014/8/24

#
Well I just made a button that when you click on it, it opens up the file. I posted the code I used to open it above, but you also might need:
1
2
import java.io.File;
import java.awt.Desktop;
and I uploaded the html file to dropbox at: https://www.dropbox.com/s/wwv9on35ei4c2ls/Conquest%20Help.html?dl=0
danpost danpost

2014/8/24

#
Griffon wrote...
Well I just made a button that when you click on it, it opens up the file. I posted the code I used to open it above, but you also might need: < Code Omitted > and I uploaded the html file to dropbox at: https://www.dropbox.com/s/wwv9on35ei4c2ls/Conquest%20Help.html?dl=0
Oh, ok. Thanks. I guess no experimentation is required, then.
Griffon Griffon

2014/8/27

#
I FINALLY figured it out on my own. I discovered the JEditorPane. It's a class that can read html code and since it's a java file the program will open it after you share it with Greenfoot! Although none of the hyperlinks or pictures work, that doesn't matter as much. The only problem is that it didn't scroll, but passing it to a JScrollPane gave it a scroll bar. Example:
1
2
3
JEditorPane editorPane = new JEditorPane("text/html","<p>html code goes here</p>");
editorPane.setEditable(false); //make it uneditable
JScrollPane scrollPane = new JScrollPane(editorPane); //makes the page able to scroll
I had to set up a java interface file to put that code in and instead of:
1
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I had to set it as:
1
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
otherwise the whole program closed when you hit the X on that window. Although it wasn't what I was wanting it still works as a suitable solution.
You need to login to post a reply.