From e87fcc40f9a2e8624d3260abf8180596de79b152 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 20 Dec 2009 17:53:45 +0000 Subject: Made code fully compatible with scala 2.8. --- src/graphyx/Graphyx.scala | 1 - src/graphyx/graphics/GraphicalAABB.scala | 2 +- src/graphyx/graphics/GraphicalCircle.scala | 2 +- src/graphyx/graphics/GraphicalRectangle.scala | 2 +- src/graphyx/graphics/GraphicalRegularPolygon.scala | 2 +- src/graphyx/graphics/Parser.scala | 20 ++++++++++---------- src/graphyx/gui/InfoPanel.scala | 6 ++++-- src/graphyx/gui/MainFrame.scala | 3 ++- src/graphyx/gui/WorldPanel.scala | 2 +- src/graphyx/tests/Cannon.scala | 2 +- src/sims/collision/GridDetector.scala | 10 +++++----- src/sims/collision/Pair.scala | 7 +++++-- src/sims/dynamics/joints/ForceJoint.scala | 2 +- src/sims/geometry/ConvexPolygon.scala | 4 ++-- 14 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/graphyx/Graphyx.scala b/src/graphyx/Graphyx.scala index 192b897..5720566 100644 --- a/src/graphyx/Graphyx.scala +++ b/src/graphyx/Graphyx.scala @@ -13,7 +13,6 @@ import sims.geometry._ import sims.dynamics._ object Graphyx{ - val tests: List[graphyx.tests.Test] = List( CompositeShape, Joints1, diff --git a/src/graphyx/graphics/GraphicalAABB.scala b/src/graphyx/graphics/GraphicalAABB.scala index 3d8999c..cb3694b 100644 --- a/src/graphyx/graphics/GraphicalAABB.scala +++ b/src/graphyx/graphics/GraphicalAABB.scala @@ -7,7 +7,7 @@ package graphyx.graphics import sims.collision._ -case class GraphicalAABB(real: AABB) extends AABB(real.minVertex, real.maxVertex) with GraphicalObject { +class GraphicalAABB(val real: AABB) extends AABB(real.minVertex, real.maxVertex) with GraphicalObject { override def draw() = { g.setColor(java.awt.Color.BLACK) g.drawRect((minVertex.x * scale * ppm).toInt, diff --git a/src/graphyx/graphics/GraphicalCircle.scala b/src/graphyx/graphics/GraphicalCircle.scala index 31f057d..ffbd7a6 100644 --- a/src/graphyx/graphics/GraphicalCircle.scala +++ b/src/graphyx/graphics/GraphicalCircle.scala @@ -9,7 +9,7 @@ package graphyx.graphics import sims._ import geometry._ import dynamics._ -case class GraphicalCircle(real: Circle) extends Circle(real.radius, real.density) with GraphicalShape{ +class GraphicalCircle(val real: Circle) extends Circle(real.radius, real.density) with GraphicalShape{ override def draw() = { //val b = Math.min(density / 100 * 255, 255) //g.setColor(new java.awt.Color(0,0,255, b.toInt)) diff --git a/src/graphyx/graphics/GraphicalRectangle.scala b/src/graphyx/graphics/GraphicalRectangle.scala index ae65c5e..05dfa76 100644 --- a/src/graphyx/graphics/GraphicalRectangle.scala +++ b/src/graphyx/graphics/GraphicalRectangle.scala @@ -8,7 +8,7 @@ package graphyx.graphics import sims._ import sims.dynamics._ -case class GraphicalRectangle(real: Rectangle) extends Rectangle(real.halfWidth, real.halfHeight, real.density) with GraphicalShape { +class GraphicalRectangle(val real: Rectangle) extends Rectangle(real.halfWidth, real.halfHeight, real.density) with GraphicalShape { override def draw() = { g.setColor(java.awt.Color.red) fillPolygon(vertices) diff --git a/src/graphyx/graphics/GraphicalRegularPolygon.scala b/src/graphyx/graphics/GraphicalRegularPolygon.scala index a3906c3..fff8a9e 100644 --- a/src/graphyx/graphics/GraphicalRegularPolygon.scala +++ b/src/graphyx/graphics/GraphicalRegularPolygon.scala @@ -9,7 +9,7 @@ package graphyx.graphics import sims._ import geometry._ import dynamics._ -case class GraphicalRegularPolygon(real: RegularPolygon) extends RegularPolygon(real.n, real.radius, real.density) with GraphicalShape{ +class GraphicalRegularPolygon(val real: RegularPolygon) extends RegularPolygon(real.n, real.radius, real.density) with GraphicalShape{ override def draw() = { g.setColor(java.awt.Color.orange) fillPolygon(vertices) diff --git a/src/graphyx/graphics/Parser.scala b/src/graphyx/graphics/Parser.scala index 9468d16..785d985 100644 --- a/src/graphyx/graphics/Parser.scala +++ b/src/graphyx/graphics/Parser.scala @@ -14,22 +14,22 @@ object Parser { val throwOnUnknown = false def toGraphical(real: Shape) = real match { - case c: Circle => GraphicalCircle(c) - case r: Rectangle => GraphicalRectangle(r) - case p: RegularPolygon => GraphicalRegularPolygon(p) + case c: Circle => new GraphicalCircle(c) + case r: Rectangle => new GraphicalRectangle(r) + case p: RegularPolygon => new GraphicalRegularPolygon(p) case _ => throw new IllegalArgumentException("Cannot cast '" + real.getClass + "' to a graphical object.") } def toGraphical(real: Joint) = real match { - case j: DistanceJoint => GraphicalDistanceJoint(j) - case j: SpringJoint => GraphicalSpringJoint(j) - case j: RevoluteJoint => GraphicalRevoluteJoint(j) + case j: DistanceJoint => new GraphicalDistanceJoint(j) + case j: SpringJoint => new GraphicalSpringJoint(j) + case j: RevoluteJoint => new GraphicalRevoluteJoint(j) case j: Joint => if (!throwOnUnknown) new GraphicalJoint{override val real = j; def draw = ()} else throw new IllegalArgumentException("Cannot cast '" + real.getClass + "' to a graphical object.") } - def toGraphical(real: Collision) = GraphicalCollision(real) - def toGraphical(real: Pair) = GraphicalPair(real) - def toGraphical(real: AABB) = GraphicalAABB(real) - def toGraphical(real: Body) = GraphicalBody(real) + def toGraphical(real: Collision) = new GraphicalCollision(real) + def toGraphical(real: Pair) = new GraphicalPair(real) + def toGraphical(real: AABB) = new GraphicalAABB(real) + def toGraphical(real: Body) = new GraphicalBody(real) } diff --git a/src/graphyx/gui/InfoPanel.scala b/src/graphyx/gui/InfoPanel.scala index 6e58ba3..d9dad43 100644 --- a/src/graphyx/gui/InfoPanel.scala +++ b/src/graphyx/gui/InfoPanel.scala @@ -7,9 +7,11 @@ package graphyx.gui import scala.swing._ +import scala.swing._ + class InfoPanel(container: Container) extends BoxPanel(Orientation.Vertical){ - preferredSize = (200, 50) - + preferredSize = new java.awt.Dimension(200, 50) + val out = new TextArea out.editable = false contents += out diff --git a/src/graphyx/gui/MainFrame.scala b/src/graphyx/gui/MainFrame.scala index 6b1f2fe..4fdc9a2 100644 --- a/src/graphyx/gui/MainFrame.scala +++ b/src/graphyx/gui/MainFrame.scala @@ -13,7 +13,8 @@ import scala.swing._ class MainFrame(container: Container) extends Frame{ super.background = java.awt.Color.WHITE title = "graphyx" - preferredSize = (1000,800) + preferredSize = new java.awt.Dimension(1000,800) + reactions += { case event.WindowClosing(w) => Graphyx.exit() } diff --git a/src/graphyx/gui/WorldPanel.scala b/src/graphyx/gui/WorldPanel.scala index 7f9fbee..ad14726 100644 --- a/src/graphyx/gui/WorldPanel.scala +++ b/src/graphyx/gui/WorldPanel.scala @@ -128,7 +128,7 @@ class WorldPanel(container: Container) extends BoxPanel(Orientation.Vertical){ grabbedBody = None } - listenTo(Mouse.clicks, Mouse.moves, Mouse.wheel) + listenTo(mouse.clicks, mouse.moves, mouse.wheel) reactions += { case MousePressed(c,p,x,y,b) => { mousePressed = true; startPoint = p; endPoint = p diff --git a/src/graphyx/tests/Cannon.scala b/src/graphyx/tests/Cannon.scala index 7738103..01ce4e5 100644 --- a/src/graphyx/tests/Cannon.scala +++ b/src/graphyx/tests/Cannon.scala @@ -35,7 +35,7 @@ object Cannon extends Test{ ) {fixed = (i == 0)} } world += cannon - world ++= List.flatten(stack) + world ++= stack.flatten } diff --git a/src/sims/collision/GridDetector.scala b/src/sims/collision/GridDetector.scala index abc40f2..9a71c6d 100644 --- a/src/sims/collision/GridDetector.scala +++ b/src/sims/collision/GridDetector.scala @@ -12,14 +12,14 @@ import scala.collection._ import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.HashMap -/**A conrete implementation of Detector. GridDetector divides the world into a grid +/**A concrete implementation of Detector. GridDetector divides the world into a grid * for faster collision detection.*/ class GridDetector(override val world: World) extends Detector { /**Array of collision detection methods. These methods return true if two shapes are colliding.*/ val detectionMethods = new ArrayBuffer[PartialFunction[(Shape, Shape), Boolean]] detectionMethods += { - case (c1: Circle, c2: Circle) => { //Kollision wenn Distanz <= Summe der Radien + case (c1: Circle, c2: Circle) => { //collision if distance <= sum of radiuses val d = (c1.pos - c2.pos).length val rSum = c1.radius + c2.radius d - rSum <= 0 @@ -31,12 +31,12 @@ class GridDetector(override val world: World) extends Detector { axes.forall((a: Vector2D) => p1.project(a) overlaps p2.project(a)) } - case (p: ConvexPolygon, c: Circle) => { //Distanz von Zentrum zu Seiten oder Eckpunkten + case (p: ConvexPolygon, c: Circle) => { //distance form center to sides or vertices val distances = for (s <- p.sides) yield (s distance c.pos) distances.exists(_ - c.radius <= 0) || (p contains c.pos) } - case (c: Circle, p: ConvexPolygon) => { //Distanz von Zentrum zu Seiten oder Eckpunkten + case (c: Circle, p: ConvexPolygon) => { //distance form center to sides or vertices val distances = for (s <- p.sides) yield (s distance c.pos) distances.exists(_ - c.radius <= 0) || (p contains c.pos) } @@ -97,7 +97,7 @@ class GridDetector(override val world: World) extends Detector { } for(s <- world.shapes) addToGrid(s) var ps: List[Pair] = Nil - for(cell <- grid.values) { + for(cell <- grid.valuesIterator) { ps = ps ::: (for (s1: Shape <- cell; s2: Shape <- cell; if (s1 ne s2); if (s1.body ne s2.body); diff --git a/src/sims/collision/Pair.scala b/src/sims/collision/Pair.scala index e4ae947..a01fb00 100644 --- a/src/sims/collision/Pair.scala +++ b/src/sims/collision/Pair.scala @@ -9,8 +9,7 @@ package sims.collision import sims.dynamics._ /**Pair of shapes.*/ -case class Pair(s1: Shape, s2: Shape) extends Tuple2(s1, s2){ - def this(t: Tuple2[Shape, Shape]) = this(t._1, t._2) +case class Pair(s1: Shape, s2: Shape){ override def equals(other: Any) = { //overriden to prevent removal during "GridDetector.getPairs" other match { @@ -19,3 +18,7 @@ case class Pair(s1: Shape, s2: Shape) extends Tuple2(s1, s2){ } } } + +object Pair { + implicit def pair2Tuple(x: Pair) = (x.s1, x.s2) +} diff --git a/src/sims/dynamics/joints/ForceJoint.scala b/src/sims/dynamics/joints/ForceJoint.scala index 1eed850..2074ee4 100644 --- a/src/sims/dynamics/joints/ForceJoint.scala +++ b/src/sims/dynamics/joints/ForceJoint.scala @@ -9,6 +9,6 @@ package sims.dynamics.joints /**A joint which can apply a force to its anchor bodies, thus adding or removing energy to the system.*/ trait ForceJoint { - /**Applies a force on the achor bodies.*/ + /**Applies a force on the anchor bodies.*/ def applyForce(): Unit } diff --git a/src/sims/geometry/ConvexPolygon.scala b/src/sims/geometry/ConvexPolygon.scala index cb4a429..0b05aaa 100644 --- a/src/sims/geometry/ConvexPolygon.scala +++ b/src/sims/geometry/ConvexPolygon.scala @@ -36,8 +36,8 @@ trait ConvexPolygon { def AABB = { val xs = vertices map (_.x) val ys = vertices map (_.y) - new AABB(Vector2D(Iterable.min(xs), Iterable.min(ys)), - Vector2D(Iterable.max(xs), Iterable.max(ys))) + new AABB(Vector2D(xs.min, ys.min), + Vector2D(xs.max, ys.max)) } /**Checks if the point point is contained in this polygon. -- cgit v1.2.3