- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[Help] Better handling conversion
Fri, 2011-07-22, 11:17
Hi evererybody
I need help of the community on scala conversion !
I've started some projects using JavaFx2, combined with scala it's
powerfull, when i'm all writing from scratch it's ok but to get the
job get done quick i've to used some of the fx2 features.
My problems came from :
1: Converting scala structure to java collection
2: The HUGE explicit convertion
Cannot suggest me a better way to resolve this ?
Cheers,
Code :
NumberAxis xAxis = new NumberAxis("X-Axis", 1.0d, 9.0d, 2.0d);
xAxis.setTickLength(12.0f);
NumberAxis yAxis = new NumberAxis("Y-Axis", 0.0d, 10.0d,
2.0d);
ObservableList areaChartData =
FXCollections.observableArrayList(
new AreaChart.Series("Series
1",FXCollections.observableArrayList(
new AreaChart.Data(0,4),
...
))
);
AreaChart chart = new AreaChart(xAxis, yAxis, areaChartData);
----------------------------------------------------------------
val xAxis = new NumberAxis("X-Axis", 0d, 2048.0d,
200.0d).asInstanceOf[Axis[Any]]
xAxis.setTickLength(12.0f)
val yAxis = new NumberAxis("Y-Axis", 0.0d, 100.0d,
10.0d).asInstanceOf[Axis[Any]]
val elements = new java.util.ArrayList[XYChart.Data[Double,
Double]]
var i = 0.
for (point <- points) {
val xy = new XYChart.Data(i, point.y)
elements.add(xy)
i += 1
}
val areaChartData = FXCollections.observableArrayList(
new XYChart.Series[Any, Any]("Series 1",
FXCollections.observableArrayList(
elements.asInstanceOf[java.util.ArrayList[XYChart.Data[Any,
Any]]])))
val chart = new AreaChart(xAxis, yAxis, areaChartData)
Fri, 2011-07-22, 13:47
#2
Re: Better handling conversion
Salut Philippe
Thanks for the response,
I've spend sometimes now using javafx2 and it's the first time i've to
use that much explicit conversion !
new AreaChart(xAxis, yAxis, areaChartData) expect Axis[Any] and
NumbersAxis event if it extends Axis
does not provide so i've to enter this infernal cast.
I think i need to better understand what's going there. Maybe it more
related to how the javafx2 API is designed.
Any others advices ?
On 22 juil, 13:37, Philippe Lhoste wrote:
> On 22/07/2011 12:17, Robert Felker wrote:
>
>
>
>
>
>
>
>
>
> > val xAxis = new NumberAxis("X-Axis", 0d, 2048.0d,
> > 200.0d).asInstanceOf[Axis[Any]]
> > xAxis.setTickLength(12.0f)
> > val yAxis = new NumberAxis("Y-Axis", 0.0d, 100.0d,
> > 10.0d).asInstanceOf[Axis[Any]]
>
> > val elements = new java.util.ArrayList[XYChart.Data[Double,
> > Double]]
> > var i = 0.
> > for (point<- points) {
> > val xy = new XYChart.Data(i, point.y)
> > elements.add(xy)
> > i += 1
> > }
>
> > val areaChartData = FXCollections.observableArrayList(
> > new XYChart.Series[Any, Any]("Series 1",
> > FXCollections.observableArrayList(
> > elements.asInstanceOf[java.util.ArrayList[XYChart.Data[Any,
> > Any]]])))
> > val chart = new AreaChart(xAxis, yAxis, areaChartData)
>
> Perhaps you can rely more on Scala's type inference?
> Also you have put a cast on 'elements' where you use it, but 'elements' is already typed,
> is this cast really necessary? (I see you go from Double to Any)
>
> I just started to adapt the JavaFX examples to Scala (there is a recent thread on this),
> using some code found in the list as a starting point. I have hit a type issue, indeed
> (seems it wasn't there in previous betas) needing an explicit cast where I felt it wasn't
> really necessary [1]. Perhaps you have a similar issue on the Graph API, I don't know
> (yet; I plan to convert ChartsSampler examples too, but I don't even find a way to run
> them, they don't have a JNLP).
>
> [1] I got no real answer on this Java <-> Scala interoperability, reproduced on a simple
> standalone example. I plan to open a bug (even if it is just me failing there) to get an
> official answer... :-)
>
> Sorry if I can't help you more, but I am not good enough at Scala to help you without a
> standalone, runable source...
>
> --
> Philippe Lhoste
Fri, 2011-07-22, 14:17
#3
Re: Better handling conversion
On 22/07/2011 14:37, Robert Felker wrote:
> I think i need to better understand what's going there. Maybe it more
> related to how the javafx2 API is designed.
I fear so... So constructs are obviously made to be Java-friendly (less verbose) but not
necessarily Scala-friendly (perhaps showing corner-case interoperability issues).
On 22/07/2011 12:17, Robert Felker wrote:
> val xAxis = new NumberAxis("X-Axis", 0d, 2048.0d,
> 200.0d).asInstanceOf[Axis[Any]]
> xAxis.setTickLength(12.0f)
> val yAxis = new NumberAxis("Y-Axis", 0.0d, 100.0d,
> 10.0d).asInstanceOf[Axis[Any]]
>
> val elements = new java.util.ArrayList[XYChart.Data[Double,
> Double]]
> var i = 0.
> for (point<- points) {
> val xy = new XYChart.Data(i, point.y)
> elements.add(xy)
> i += 1
> }
>
> val areaChartData = FXCollections.observableArrayList(
> new XYChart.Series[Any, Any]("Series 1",
> FXCollections.observableArrayList(
> elements.asInstanceOf[java.util.ArrayList[XYChart.Data[Any,
> Any]]])))
> val chart = new AreaChart(xAxis, yAxis, areaChartData)
Perhaps you can rely more on Scala's type inference?
Also you have put a cast on 'elements' where you use it, but 'elements' is already typed,
is this cast really necessary? (I see you go from Double to Any)
I just started to adapt the JavaFX examples to Scala (there is a recent thread on this),
using some code found in the list as a starting point. I have hit a type issue, indeed
(seems it wasn't there in previous betas) needing an explicit cast where I felt it wasn't
really necessary [1]. Perhaps you have a similar issue on the Graph API, I don't know
(yet; I plan to convert ChartsSampler examples too, but I don't even find a way to run
them, they don't have a JNLP).
[1] I got no real answer on this Java <-> Scala interoperability, reproduced on a simple
standalone example. I plan to open a bug (even if it is just me failing there) to get an
official answer... :-)
Sorry if I can't help you more, but I am not good enough at Scala to help you without a
standalone, runable source...