Skip to main content

How to access the variable?

3 replies [Last post]
cewan
Offline
Joined: 2007-12-10
Points: 0

I have been playing around with a drawing program for JavaFX, inspired by Scribble on the wiki.

Here's the code for it:

import javafx.ui.*;
import javafx.ui.canvas.*;

var polyline: Polyline;
var currentColor: Color = red;

class ColorButton extends CompositeNode {
attribute color: Color;
attribute y: Number;
}

function ColorButton.composeNode() =
Group {
content: [Rect {
x: 830
y: y
width: 100
height: 50
fill: color
stroke: black
strokeWidth: 3
onMouseClicked: operation (e: CanvasMouseEvent) {
currentColor = color;
}
}]
};

Group {
var: self
content: [Rect {
fill: white
stroke: black
strokeWidth: 3
x: 5
y: 5
width : 800
height: 600

onMousePressed: operation (e: CanvasMouseEvent) {
if (e.button == 1) {
polyline = Polyline{
points : [e.x, e.y]
stroke : currentColor
strokeWidth: 7};
insert polyline as last into self.content ;
}
if (e.button == 3) {
polyline = Polyline{
points : [e.x, e.y]
stroke : white
strokeWidth: 15};
insert polyline as last into self.content ;
}

}
onMouseDragged: operation (e: CanvasMouseEvent) {
if ( e.x < 800 and e.y < 600) {
insert [e.x, e.y] as last into polyline.points;
}
}
}, ColorButton {
y: 10
color: red
}, ColorButton {
y: 70
color: blue
}, ColorButton {
y: 130
color: green
}, ColorButton {
y: 190
color: yellow
}, ColorButton {
y: 250
color: orange
}, ColorButton {
y: 310
color: gray
}, ColorButton {
y: 370
color: black
}, ColorButton {
y: 430
color: red
}, Text {
content: "Current"
font: Font {
faceName: "Sans Serif"
style: BOLD
size: 24
}
x: 830
y: 520

}, Rect {
x: 830
y: 550
width: 100
height: 50
fill: bind currentColor
stroke: black
strokeWidth: 3
}
]
}

The thing that is not working in this code is the "currentColor = color;" on line 23. If I remove that line the code works, but then the colorchoosing does not work, of course...
What must I write to access the currentColor variable from inside the Button operation?

Thanks!

PS. Anyone know how to add code to a post so that it looks nice? Like a code tag of some sort...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexsch
Offline
Joined: 2007-07-12
Points: 0

Global variable should be used with its type to distinguish it from a local one:
[code] currentColor:Color = color [/code]

cewan
Offline
Joined: 2007-12-10
Points: 0

Thanks! That worked fine as soon as I understood that I had to change to "currentColor:Color" both in the button operation and in the polyline creation.

cewan
Offline
Joined: 2007-12-10
Points: 0

Hmm, still one problem. The rectangle showing the current color doesn't update even though it has:

fill: bind currentColor:Color

Why doesn't this work?