From Java/glaciers/TestViewport.
1.
_square.addListener(
new
SquareListener() {
2.
public
void
positionChanged() {
3.
updatePosition();
4.
}
5.
});
From Scala/ladybug2d.
1.
model.maze.addListenerByName( updateTransform() )
From Java/Rotation/AngleUnitsSelectionControl (see also ph-scale/ScaleControlPanel line 60)
01.
degrees =
new
JRadioButton( RotationStrings.getString(
"units.degrees"
) );
02.
degrees.addActionListener(
new
ActionListener() {
03.
public
void
actionPerformed( ActionEvent e ) {
04.
angleUnitModel.setRadians(
false
);
05.
}
06.
} );
07.
add( degrees );
08.
09.
angleUnitModel.addListener(
new
AngleUnitModel.Listener() {
10.
public
void
changed() {
11.
update();
12.
}
13.
} );
14.
update();
15.
16.
private
void
update() {
17.
degrees.setSelected( !angleUnitModel.isRadians() );
18.
...
19.
}
From Scala/RemoteControl line 182
1.
add(
new
MyRadioButton(
"Position"
, mode
=
positionMode, mode
==
positionMode,
this
.addListener))
2.
def
mode
_=
(m
:
RemoteMode)
=
{
3.
_
mode
=
m
4.
notifyListeners
5.
}
Scala library class that makes this possible
1.
class
MyRadioButton(text
:
String, actionListener
:
=
> Unit,
2.
getter
:
=
> Boolean,
3.
addListener
:
(()
=
> Unit)
=
> Unit)
4.
extends
RadioButton(text) {...}
example of existing code: Conductivity/ArrowShape line 45.
1.
AbstractVector2D phetvector = direction.getScaledInstance( d ).getAddedInstance( norm.getScaledInstance( d1 ) );
2.
return
tipLocation.getAddedInstance( phetvector );
Mock-up Java version.
1.
model.ladybug.setAcceleration((getVelocity(t + dt).minus(getVelocity(t - dt))).dividedBy(dt));
scala, LadybugMotionModel line 148
1.
model.ladybug.setAcceleration((getVelocity(t + dt) - getVelocity(t - dt)) / dt)
Doing this the usual way, we are prone to forget: (a) to call the first update or (b) to attach listener to the model.
1.
m.addListenerByName(update())
2.
update
3.
def
update()
=
{
4.
text.setText(
new
DecimalFormat(
"0.00"
).format(m.getTime)+
" sec"
)
5.
}
Scala implementation. Using a control structure ensures everything will happen, and is more concise
1.
val
update
=
defineInvokeAndPass(model.addListenerByName){
2.
text.setText(
new
DecimalFormat(
"0.00"
).format(model.getTime) +
" sec"
)
3.
}
Scala library implementation
1.
def
defineInvokeAndPass(m
:
(
=
> Unit)
=
> Unit)(block
:
=
> Unit)
:
()
=
> Unit
=
{
2.
block
3.
m(block)
4.
block
_
5.
}
Java implementation.
1.
def
crossed(line
:
Line
2
D.Double, start
:
Vector
2
D, end
:
Vector
2
D)
=
{
2.
val
intersection
=
MathUtil.getLineSegmentsIntersection(line,
new
Line
2
D.Double(toPoint
2
D(start),toPoint
2
D(end))
3.
}
Scala implementation
1.
def
crossed(line
:
Line
2
D.Double, start
:
Vector
2
D, end
:
Vector
2
D)
=
{
2.
val
intersection
=
MathUtil.getLineSegmentsIntersection(line,
new
Line
2
D.Double(start, end))
3.
}
Scala library implementation
1.
implicit
def
vector
2
DToPoint(vector
:
Vector
2
D)
=
new
Point
2
D.Double(vector.x, vector.y)
Scala implementation
01.
def
test()
=
{
02.
...
03.
def
tx(pt
:
Point
2
D)
=
{
04.
val
intermediate
=
getWorldTransformStrategy.getTransform.inverseTransform(pt,
null
)
05.
val
model
=
transform.viewToModel(intermediate.getX, intermediate.getY)
06.
model
07.
}
08.
val
out
=
new
Rectangle
2
D.Double()
09.
out.setFrameFromDiagonal(tx(topLeft).getX, tx(topLeft).getY, tx(bottomRight).getX, tx(bottomRight).getY)
10.
}
Java ternary operator
1.
module.model.isPaused?
"Play"
:
"Pause"
Scala if statement, see LadybugClockControlPanel
1.
if
(module.model.isPaused)
"Play"
else
"Pause"
Scala implementation in LadybugModel
1.
val
tx
=
for
(item <- h)
yield
new
TimeData(item.state.position.x, item.time)
2.
val
vx
=
MotionMath.estimateDerivative(tx.toArray)
Scala implementation in AphidMazeModel
1.
aphids.foreach(handleCollision(
_
))
Scala implementation in DataPoint
1.
case
class
DataPoint(time
:
Double, state
:
LadybugState)
1.
new
MyRadioButton(
"Record"
, model.setRecord(
true
), model.isRecord, model.addListener) {
2.
font
=
new
PhetFont(
15
,
true
)
3.
}
1.
if
( gridStrategy
instanceof
GridStrategy.Relative ) {
2.
( (GridStrategy.Relative) gridStrategy ).setSpacing( spacing );
3.
}
In Scala, atomic casts can be done by pattern matching
1.
myComponent
match
{
2.
case
jc
:
JComponent
=
> jc.paintImmediately(
0
,
0
,jc.getWidth,jc.getHeight)
3.
case
_
=
> Unit
4.
}