Skip to main content

Creating compound arguments in a custom class

10 replies [Last post]
candlejack
Offline
Joined: 2007-11-21
Points: 0

Hello everyone,

I am toying around with JavaFX and am still rather new to it, so my apologies if this is a stupid question to ask, but i have not found anything concerning this online or in books, so maybe someone in here could give me a clue.

I was trying to create a test class extending Frame that has a few attributes of classes derived from CustomNode. So far so fine, i can initialize them and everything. But then i wanted to reference those attributes in the content attribute of my frameish class, so that it would have looked something like this:

Class File:

class TestFrame extends Frame {
attribute customNode1:MyCustomNode;
attribute customNode2:MyCustomNode;
}

Program File:

TestFrame{

customNode1: MyCustomNode{
}

customNode2: MyCustomNode{
}

stage: Stage {
content: [
customNode1,customNode2
]

}
}

I tried every variant of this i could think of but the compiler just whines about not being able to find the symbols customNode1 and customNode2 within the stage content (bold line).

When i declare those variables as var outside the TestFrame definiton as a separate part of the program file they can be accessed from the stage content and everything renders as it should.

So what i wonder is, is there no way to make a "compound attribute" as i tried in the start? Is that a desired contruct in JavaFX or am i thinking to much in Java at this point? Or is this a compiler issue as the netbeans plugin does not complain about my original attempt?

Any input on this would be very welcome.

Thanks,
Chris

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kram
Offline
Joined: 2007-10-23
Points: 0

Just a thought, in your original code you could bind the content of your Stage element to a sequence of Nodes:

[code]
var nodes:Node[] = [
MyCustomNode{}, MyCustomNode{}
];

stage: Stage {
content: bind nodes;
}
[/code]

Then you wouldnt need "var"s as you just define them in your seqence

candlejack
Offline
Joined: 2007-11-21
Points: 0

But this would again not bind the classes attributes to the content but instead some anonymous instances. Using the attributes in the array only moves the problem from the stage content to the array declaration.

Does anyone know if it is even possible to reference a class attribute in this way or is that considered bad/useless practice?

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi candlejack. How about this code?
I slightly changed cusomNode1 and customNode2 because they override each other.

[code]

import javafx.scene.*;
import javafx.application.*;
import javafx.scene.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.transform.*;
import java.lang.Math;

public class MyCustomNode extends CustomNode{
attribute x:Number = 100;
attribute y:Number = 100;
attribute r:Number = 20;
public function create(): Node {
return Group {
content: [Circle {
centerX: bind x
centerY: bind y
radius: bind r
fill: Color.BLACK
}]
};
}
}

class TestFrame extends Frame {
attribute customNode1: MyCustomNode;
attribute customNode2: MyCustomNode;
postinit { stage = Stage { content: [customNode1,customNode2] }
}
}

TestFrame{
customNode1:MyCustomNode{x:100 y:100 r:30}
customNode2:MyCustomNode{x:200 y:100 r:30}
width: 600
height: 600
visible:true
}

[/code]

candlejack
Offline
Joined: 2007-11-21
Points: 0

Hi shagus, sorry i was on vacation the last days. I just tried your suggestion and it seems to work fine, thanks a lot. I did not come across that postinit function yet, it does not show in the javadoc api. Can you tell me where that is documented?

Cheers,
Chris

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi candlejack. Take a look at James Weaver's JavaFX Blog to get more information about the postinit() function:
http://learnjavafx.typepad.com/weblog/2007/12/compiled-java-1.html

kram
Offline
Joined: 2007-10-23
Points: 0

Can you try putting [b]var[/b] before the
customNode1: MyCustomNode{ }
and
customNode2: MyCustomNode{ }
?

i.e.
var customNode1: MyCustomNode{ }

maybe?? Sorry I dont have netbeans available to me at the moment or I would try it before I post this...

shagus
Offline
Joined: 2007-05-10
Points: 0

[code]import javafx.scene.*;
import javafx.application.*;

public class MyCustomNode extends CustomNode{
public function create():Node {Group {} }
}

class TestFrame extends Frame {
attribute customNode1: MyCustomNode ;
attribute customNode2: MyCustomNode ;
}

TestFrame{
var customNode1 = MyCustomNode{}
var customNode2 = MyCustomNode{}
stage: Stage {
content: [customNode1 ,customNode2 ]
}
}
[/code]

candlejack
Offline
Joined: 2007-11-21
Points: 0

Thx for your replys, yes using var customNode1 = MyCustomNode{} makes the code compile, [b]but...[/b]

Using var declares local variables with the same name as the attributes essentially shadows those attributes. I verified this by removing the attributes from the class definition and compiling the code again, it still works. So using var means you are not using the class attributes but instead those variables defined in the main classes local scope, which is not what the intention was. Or am I confusing something here?

Message was edited by: candlejack

shagus
Offline
Joined: 2007-05-10
Points: 0

How about the following?

[code]

import javafx.scene.*;
import javafx.application.*;

public class MyCustomNode extends CustomNode{
public function create():Node {Group {} }
}

class TestFrame extends Frame {
attribute customNode1: MyCustomNode ;
attribute customNode2: MyCustomNode ;
}

var t: TestFrame = TestFrame{
customNode1: MyCustomNode{}
customNode2: MyCustomNode{}
stage: Stage {
content: [t.customNode1 ,t.customNode2 ]
}
}

[/code]

candlejack
Offline
Joined: 2007-11-21
Points: 0

Hi shagus,

interesting idea. Compiles fine and looks like it should do what I intended, but for some reason the customNodes do not get displayed anymore (i created a simple rectangle to be returned by the create function). When using the var approach they get displayed properly but not when trying your latest suggestion. Any idea why that could be?

Thanks, Chris