From Java/glaciers/TestViewport.
_square.addListener( new SquareListener() {
public void positionChanged() {
updatePosition();
}
});
From Scala/ladybug2d.
model.maze.addListenerByName( updateTransform() )
From Java/Rotation/AngleUnitsSelectionControl (see also ph-scale/ScaleControlPanel line 60)
degrees = new JRadioButton( RotationStrings.getString( "units.degrees" ) );
degrees.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
angleUnitModel.setRadians( false );
}
} );
add( degrees );
angleUnitModel.addListener( new AngleUnitModel.Listener() {
public void changed() {
update();
}
} );
update();
private void update() {
degrees.setSelected( !angleUnitModel.isRadians() );
...
}
From Scala/RemoteControl line 182
add(new MyRadioButton("Position", mode = positionMode, mode == positionMode, this.addListener))
def mode_=(m: RemoteMode) = {
_mode = m
notifyListeners
}
Scala library class that makes this possible
class MyRadioButton(text: String, actionListener: => Unit,
getter: => Boolean,
addListener: (() => Unit) => Unit)
extends RadioButton(text) {...}
example of existing code: Conductivity/ArrowShape line 45.
AbstractVector2D phetvector = direction.getScaledInstance( d ).getAddedInstance( norm.getScaledInstance( d1 ) ); return tipLocation.getAddedInstance( phetvector );
Mock-up Java version.
model.ladybug.setAcceleration((getVelocity(t + dt).minus(getVelocity(t - dt))).dividedBy(dt));
scala, LadybugMotionModel line 148
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.
m.addListenerByName(update())
update
def update()= {
text.setText(new DecimalFormat("0.00").format(m.getTime)+" sec")
}
Scala implementation. Using a control structure ensures everything will happen, and is more concise
val update = defineInvokeAndPass(model.addListenerByName){
text.setText(new DecimalFormat("0.00").format(model.getTime) + " sec")
}
Scala library implementation
def defineInvokeAndPass(m: (=> Unit) => Unit)(block: => Unit): () => Unit = {
block
m(block)
block _
}
Java implementation.
def crossed(line: Line2D.Double, start: Vector2D, end: Vector2D) = {
val intersection = MathUtil.getLineSegmentsIntersection(line, new Line2D.Double(toPoint2D(start),toPoint2D(end))
}
Scala implementation
def crossed(line: Line2D.Double, start: Vector2D, end: Vector2D) = {
val intersection = MathUtil.getLineSegmentsIntersection(line, new Line2D.Double(start, end))
}
Scala library implementation
implicit def vector2DToPoint(vector: Vector2D) = new Point2D.Double(vector.x, vector.y)
Scala implementation
def test()={
...
def tx(pt: Point2D) = {
val intermediate = getWorldTransformStrategy.getTransform.inverseTransform(pt, null)
val model = transform.viewToModel(intermediate.getX, intermediate.getY)
model
}
val out = new Rectangle2D.Double()
out.setFrameFromDiagonal(tx(topLeft).getX, tx(topLeft).getY, tx(bottomRight).getX, tx(bottomRight).getY)
}
Java ternary operator
module.model.isPaused?"Play":"Pause"
Scala if statement, see LadybugClockControlPanel
if (module.model.isPaused) "Play" else "Pause"
Scala implementation in LadybugModel
val tx = for (item <- h) yield new TimeData(item.state.position.x, item.time)
val vx = MotionMath.estimateDerivative(tx.toArray)
Scala implementation in AphidMazeModel
aphids.foreach(handleCollision(_))
Scala implementation in DataPoint
case class DataPoint(time: Double, state: LadybugState)
new MyRadioButton("Record", model.setRecord(true), model.isRecord, model.addListener) {
font = new PhetFont(15, true)
}
if ( gridStrategy instanceof GridStrategy.Relative ) {
( (GridStrategy.Relative) gridStrategy ).setSpacing( spacing );
}
In Scala, atomic casts can be done by pattern matching
myComponent match{
case jc:JComponent => jc.paintImmediately(0,0,jc.getWidth,jc.getHeight)
case _ => Unit
}