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

2017/6/6

Open a URL in browser

unionthunder unionthunder

2017/6/6

#
Does anyone know if it's possible to open a URL in a browser through Greenfoot?
Yehuda Yehuda

2017/6/7

#
unionthunder wrote...
Does anyone know if it's possible to open a URL in a browser through Greenfoot?
I would think it's possible, since it's possible with java and that's what Greenfoot is, the question is How? Maybe this will help: Custom Networking The way it's done in Greenfoot (when you press Help -> Greenfoot Web Site) is by calling a method in bluej.utility.Utility which uses the commands that would be used by your System (i.e. Windows) in this method. Here's the full method (it uses some things from bluej):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Let the given URL be shown in a browser window.
 *
 * @param url the URL or file path to be shown.
 * @return true if the web browser could be started, false otherwise.
 */
@OnThread(Tag.Swing)
public static boolean openWebBrowser(String url)
{
    if (Config.isWinOS()) { // Windows
 
        String cmd;
        // catering for stupid differences in Windows shells...
        if (Config.osname.startsWith("Windows 9") || Config.osname.equals("Windows Me")) // win95/98/Me
            cmd = "command.com";
        else
            // other
            cmd = "cmd.exe";
 
        try {
            // more stupid Windows differences...
            if (Config.osname.startsWith("Windows 98") || Config.osname.equals("Windows Me")) {
                Runtime.getRuntime().exec(new String[]{cmd, "/c", "start", '"' + url + '"'});
            }
            else {
                Runtime.getRuntime().exec(new String[]{cmd, "/c", "start", "\"\"", '"' + url + '"'});
            }
        }
        catch (IOException e) {
            Debug.reportError("could not start web browser. exc: " + e);
            return false;
        }
    }
    else { // Mac, Unix and other
 
        // The string should be either a URL or a file path
        try {
            return openWebBrowser(new URL(url));
        }
        catch (MalformedURLException mfue) {
            return openWebBrowser(new File(url));
        }
 
    }
    return true;
}
I have Windows 10 and sticking the following code into the constructor of my World brought me to this post in the default browser.
1
2
3
4
5
try {
    Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "\"\"", '"' + "https://www.greenfoot.org/topics/59418/0#post_119190" + '"'});
} catch (IOException ex) {
    System.out.println(ex.getMessage());
}
Once you're in Greenfoot you can just access the above method and use it to open a web site, the following one line of code will also bring me to this post.
1
bluej.utility.Utility.openWebBrowser("https://www.greenfoot.org/topics/59418/0#post_119190");
The parameter can be a java.io.File, java.lang.String, or java.net.URL (three different methods).
You need to login to post a reply.