Skip to main content

Help understanding the Stage object and it's content property.

3 replies [Last post]
elberry
Offline
Joined: 2004-03-22
Points: 0

Hi all,
I'm trying to understand why this doesn't work:

<br />
/*<br />
 * Main.fx<br />
 *<br />
 * Created on Nov 22, 2008, 12:18:01 PM<br />
 */</p>
<p>package ssns;</p>
<p>import javafx.application.Frame;<br />
import javafx.application.Stage;<br />
import java.util.HashMap;<br />
import javafx.scene.Node;<br />
import javafx.scene.image.ImageView;<br />
import javafx.scene.image.Image;<br />
import javafx.ui.Canvas;<br />
import java.lang.*;</p>
<p>/**<br />
 * @author elberry<br />
 */</p>
<p>var mapData = [<br />
    "WWWWWWWWWWWWWWWWWWWWWWWWWWW",<br />
    "WWWWWWWWWWWWWWWWWWWWWWWWWWW",<br />
    "WSSSSSWWSSSSSSSSSSSSSSSSSWW",<br />
    "WSGGGSWWSGGGGGGGGGGGGGGGSWW",<br />
    "WSGGGSWWSSSSSSSGGSSSSSGGSWW",<br />
    "WSGGGSWWWWWSDDSGGSDDDSGGSWW",<br />
    "WSGGGSWWWWWSDDSGGSDDDSGGSWW",<br />
    "WSGGGSWWWWWSDDSGGSDDDSGGSWW",<br />
    "WSGGGSSSSSSSSSSGGSSSSSGGSWW",<br />
    "WSGGGGGGGGGGGGGGGGGGGGGGSWW",<br />
    "WSSSSSSSSSSSSSSSSSSSSSSSSWW",<br />
    "WWWWWWWWWWWWWWWWWWWWWWWWWWW",<br />
    "WWWWWWWWWWWWWWWWWWWWWWWWWWW"<br />
];</p>
<p>var mapImages = new HashMap();<br />
mapImages.put("W", Image {url: "".getClass().getResource("/images/planet_cute/Water Block.png").toString()});<br />
mapImages.put("S", Image {url: "".getClass().getResource("/images/planet_cute/Stone Block.png").toString()});<br />
mapImages.put("G", Image {url: "".getClass().getResource("/images/planet_cute/Grass Block.png").toString()});<br />
mapImages.put("D", Image {url: "".getClass().getResource("/images/planet_cute/Dirt Block.png").toString()});</p>
<p>var mapNodes: ImageView[];<br />
for(row in mapData) {<br />
    for(tileType in row) {<br />
        var image = ImageView {<br />
            image: mapImages.get(tileType) as Image<br />
        }<br />
        insert image into mapNodes;<br />
    }<br />
}</p>
<p>Frame {<br />
    title: "Tile Map"<br />
    width: 800<br />
    height: 600<br />
    closeAction: function() {<br />
        java.lang.System.exit( 0 );<br />
    }<br />
    visible: true</p>
<p>    stage: Stage {<br />
        content: [<br />
            mapNodes<br />
        ]<br />
    }<br />
}<br />

When I run this code, I get no errors, but I get a completely blank frame.

I tried switching out the content array for just the mapNodes to no avail.

<br />
    stage: Stage {<br />
        content: mapNodes<br />
    }<br />

However, if I add just a single ImageView to the content array I can see it.

<br />
    stage: Stage {<br />
        content: [<br />
            ImageView {<br />
                image: Image {<br />
                    url: "".getClass().getResource("/images/planet_cute/Water Block.png").toString()<br />
                }<br />
            }<br />
        ]<br />
    }<br />

As a side note, anyone know why the decision to make the "url" property take a file path, and not a URL?

Any help on this would be greatly appreciated.

Thanks,
Eric

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dgrieve
Offline
Joined: 2008-06-16
Points: 0

This still isn't really doing what you think. Sequences are one dimensional. Your mapData sequence is getting flattened to a one dimensional sequence of strings. So your inner loop is not needed.

elberry
Offline
Joined: 2004-03-22
Points: 0

Ah.. Good to know.

I actually modified it again so I could keep the map data in the original format.

I now use a split("") to split the string into another array.

I wish there was a nice simple syntax for looping through the characters of a string - but doesn't appear to be one.

Also wish there was a simpler syntax for map creation.

::sigh:: patience, patience.

Thanks for the pointer though, much appreciated.

Cheers!

elberry
Offline
Joined: 2004-03-22
Points: 0

I resolved the issue. The for loops were not doing what I thought they were doing. The second loop didn't loop through each character in the String. To solve the issue I changed the mapData sequence into a sequence of sequences.

[code]
/*
* Main.fx
*
* Created on Nov 22, 2008, 12:18:01 PM
*/

package ssns;

import javafx.application.Frame;
import javafx.application.Stage;
import java.util.HashMap;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.ui.Canvas;
import java.lang.*;

/**
* @author elberry
*/

var mapData = [
["W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W"],
["W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W"],
["W","S","S","S","S","S","W","W","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","W","W"],
["W","S","G","G","G","S","W","W","S","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","S","W","W"],
["W","S","G","G","G","S","W","W","S","S","S","S","S","S","S","G","G","S","S","S","S","S","G","G","S","W","W"],
["W","S","G","G","G","S","W","W","W","W","W","S","D","D","S","G","G","S","D","D","D","S","G","G","S","W","W"],
["W","S","G","G","G","S","W","W","W","W","W","S","D","D","S","G","G","S","D","D","D","S","G","G","S","W","W"],
["W","S","G","G","G","S","W","W","W","W","W","S","D","D","S","G","G","S","D","D","D","S","G","G","S","W","W"],
["W","S","G","G","G","S","S","S","S","S","S","S","S","S","S","G","G","S","S","S","S","S","G","G","S","W","W"],
["W","S","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","S","W","W"],
["W","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","W","W"],
["W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W"],
["W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W","W"]
];

var mapImages = new HashMap();
mapImages.put("W", Image {url: "".getClass().getResource("/images/planet_cute/Water Block.png").toString()});
mapImages.put("S", Image {url: "".getClass().getResource("/images/planet_cute/Stone Block.png").toString()});
mapImages.put("G", Image {url: "".getClass().getResource("/images/planet_cute/Grass Block.png").toString()});
mapImages.put("D", Image {url: "".getClass().getResource("/images/planet_cute/Dirt Block.png").toString()});

var mapNodes: ImageView[];
for(row in mapData) {
for(tileType in row) {
var imageView = ImageView {
image: mapImages.get(tileType) as Image
}
insert imageView into mapNodes;
}
}

Frame {
title: "Tile Map"
width: 800
height: 600
closeAction: function() {
java.lang.System.exit( 0 );
}
visible: true

stage: Stage {
content: [
mapNodes
]
}
}
[/code]