根据论文Deprecating the Observer Pattern with Scala.React,我试图在论文中设置一个简单的示例,但它引发了异常Exception in thread "main" java.lang.AssertionError: assertion failed: This method must be run on its domain scala.react.NilDebug@1502c065
相关问题是Running a simple Scala.React expression。
如何设置所有内容以使用scala react库的强大功能?
除了库scala-react之外,我还使用了以下示例:
object MyFirstReact extends App {
object MyDomain extends scala.react.Domain {
protected val scheduler: Scheduler = new ManualScheduler
protected val engine: Engine = new Engine
}
import MyDomain._
case class MouseEvent(position: (Int, Int))
class Path(var positions: Seq[(Int, Int)]) {
def this(pos: (Int, Int)) = this(Seq(pos))
def lineTo(pos: (Int, Int)) { positions = positions :+ pos }
def close { positions = positions :+ positions.head }
}
val mouseDown: Events[MouseEvent] = Events.once(MouseEvent((0, 0)))
val mouseMove: Events[MouseEvent] = Events.once(MouseEvent((1, 1)))
val mouseUp: Events[MouseEvent] = Events.once(MouseEvent((2, 2)))
def draw(path: Path) { /* ... */ }
Reactor.loop { self =>
// step 1
val path = new Path((self await mouseDown).position)
self.loopUntil(mouseUp) { // step 2
val m = self awaitNext mouseMove
path.lineTo(m.position)
draw(path)
}
path.close // step 3
draw(path)
}
}
答案 0 :(得分:1)
你得到了异常,因为Reactor.loop必须用schedule {...}块包装。但修复这还不够(我得到了一个java.lang.StackOverflowError)。
无论如何,我使用scala-swing编写了一个完整的工作示例,在该示例中,您可以使用与之相同的反应器代码绘制一条线。我把包含scala-react的sbt-buildable副本包含在github中:https://github.com/zsoltdonca/scala-react-line-drawing
package zsd
import scala.swing._
import java.awt.event.{MouseMotionAdapter, MouseEvent, MouseAdapter}
import java.awt.{Color, Point}
import scala.react.Domain
object MyDomain extends Domain {
val scheduler = new SwingScheduler()
val engine = new Engine
}
import MyDomain._
object ScalaReactLineDrawing extends SimpleSwingApplication with Observing {
override def main(args: Array[String]) {
schedule { startup(args) }
start() // starts the scala-react engine
}
override def top: Frame = new MainFrame() {
contents = new FlowPanel() {
val mouseDown = EventSource[Point]
val mouseMove = EventSource[Point]
val mouseUp = EventSource[Point]
val mainProgramFlow = Reactor.loop {
self =>
// step 1
val path = new Path(self await mouseDown)
self.loopUntil(mouseUp) {
// step 2
val m = self awaitNext mouseMove
path.lineTo(m)
draw(path)
}
path.close() // step 3
draw(path)
}
peer.addMouseListener(new MouseAdapter {
override def mousePressed(e: MouseEvent): Unit = mouseDown << e.getPoint
override def mouseReleased(e: MouseEvent): Unit = mouseUp << e.getPoint
})
peer.addMouseMotionListener(new MouseMotionAdapter {
override def mouseDragged(e: MouseEvent): Unit = mouseMove << e.getPoint
})
class Path(var positions: Seq[Point]) {
def this(pos: Point) = this(Seq(pos))
def lineTo(pos: Point) {
positions = positions :+ pos
}
def close() {
positions = positions :+ positions.head
}
}
var pathDrawn = new Path(new Point(0, 0))
def draw(path: Path) {
pathDrawn = path
repaint()
}
override protected def paintComponent(g: swing.Graphics2D): Unit = {
super.paintComponent(g)
val xPoints = pathDrawn.positions.map(pos => pos.x).toArray
val yPoints = pathDrawn.positions.map(pos => pos.y).toArray
g.setColor(Color.BLACK)
g.drawPolyline(xPoints, yPoints, pathDrawn.positions.length)
}
}
preferredSize = new Dimension(400, 300)
pack()
}
}