]> gitweb.hamatoma.de Git - robosim/commitdiff
asserts replaced by RuntimeExceptions
authorhama <hama@siduction.net>
Sat, 11 Oct 2014 12:27:31 +0000 (14:27 +0200)
committerhama <hama@siduction.net>
Sat, 11 Oct 2014 22:11:24 +0000 (00:11 +0200)
* cloning instead of object assignment

12 files changed:
src/jlection/robotic/RobotModel.java
src/jlecture/simulation/Barrier.java
src/jlecture/simulation/Model.java
src/jlecture/simulation/Movable.java
src/jlecture/simulation/PointRW.java
src/jlecture/simulation/RectangleRW.java
src/jlecture/swing/LineSwing.java
src/jlecture/swing/PanelSwing.java
src/jlecture/swing/RectangleSwing.java
src/jlecture/swing/SheetSwing.java
src/jlecture/swing/WidgetSwing.java
src/jlecture/swing/XCrossSwing.java

index d93932940137a26233c69f957067d010afc16a5a..eaa3092f441ffb3683bb173939855f13ea47720b 100644 (file)
@@ -38,13 +38,13 @@ public class RobotModel extends Model {
         this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500,
             100), new DimensionRW(600, 50), false, this)));
         this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500,
-            400), new DimensionRW(600, 50), false, this)));
+            400), new DimensionRW(600, 270), false, this)));
         this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500,
             700), new DimensionRW(600, 50), false, this)));
         this.firstIndexMovables = this.things.size();
 
         PointRW destination = new PointRW(0, 0);
-        for (int ix = 0; ix < 20; ix++) {
+        for (int ix = 0; ix < 2; ix++) {
             Movable movable = new Movable(this.sheet.createRectangle(
                 new PointRW(500, 500), new DimensionRW(10, 10), true, this));
             movable.getWidget().setCenter(openPosition());
index 8330dd46799a4c41b1d66d0b980c6a844a82158b..c1c7cb09a3fbb4ca7e085cc418b344a758bb90df 100644 (file)
@@ -30,7 +30,8 @@ public class Barrier extends ThingRW {
             || this.widget.getWidgetType() == WidgetType.XCROSS)
             rc = rectangle.intersects(this.widget.getContour());
         else
-            assert false;
+            throw new RuntimeException(
+                    "Barrier.clashes(RectangleRW): not implemented yet");
         return rc;
     }
 
@@ -40,7 +41,8 @@ public class Barrier extends ThingRW {
         if (this.widget instanceof RectangleRW)
             rc = thing.clashes((RectangleRW) this.widget);
         else
-            assert false;
+            throw new RuntimeException(
+                    "Barrier.clashes(ThingRW): not implemented yet");
         return rc;
     }
 
index b358e00e4bd100637268ffb28a8bc38ea29fff23..f604219b38a9796e0f07f9f64f96973f6bb10a42 100644 (file)
@@ -85,6 +85,8 @@ public abstract class Model {
      */
     protected void checkCollision(Movable movable, int lastIndex) {
         // we use the contour of the assigned widget:
+        if (lastIndex > 3 && movable.getWidget().getCenter().y > 300)
+            lastIndex += 1;
         for (int ix = 0; ix < lastIndex; ix++) {
             ThingRW thing = this.things.get(ix);
             if (movable.clashes(thing)) {
@@ -163,8 +165,8 @@ public abstract class Model {
             if (current != exclude) {
                 RectangleRW contour = current.widget.getContour();
                 if (point.x >= contour.x && point.y >= contour.y
-                        && point.x < contour.x + contour.width
-                        && point.y < contour.y + contour.height) {
+                    && point.x < contour.x + contour.width
+                    && point.y < contour.y + contour.height) {
                     rc = current;
                     break;
                 }
@@ -182,7 +184,7 @@ public abstract class Model {
         do {
             rc.x = this.region.x + this.random.nextDouble() * this.region.width;
             rc.y = this.region.x + this.random.nextDouble()
-                    * this.region.height;
+                * this.region.height;
         } while (occupied(rc, null) != null);
         return rc;
     }
index f15a156539a89db165f9733cb62d3b65659a7c66..336d566e025e1838188815ea5a06aa9b05cd7960 100644 (file)
@@ -54,10 +54,12 @@ public class Movable extends ThingRW {
     public boolean clashes(RectangleRW rectangle) {
         boolean rc = false;
         if (this.widget.getWidgetType() == WidgetType.RECTANGLE
-                || this.widget.getWidgetType() == WidgetType.XCROSS)
+            || this.widget.getWidgetType() == WidgetType.XCROSS)
             rc = this.widget.getContour().intersects(rectangle);
         else
-            assert false;
+            throw new RuntimeException(
+                "Movable.clashes(RectangleRW): not implemented yet");
+        ;
         return rc;
     }
 
@@ -65,10 +67,11 @@ public class Movable extends ThingRW {
     public boolean clashes(ThingRW thing) {
         boolean rc = false;
         if (this.widget.getWidgetType() == WidgetType.RECTANGLE
-                || this.widget.getWidgetType() == WidgetType.XCROSS)
+            || this.widget.getWidgetType() == WidgetType.XCROSS)
             rc = thing.clashes(this.widget.getContour());
         else
-            assert false;
+            throw new RuntimeException(
+                "Movable.clashes(ThingRW): not implemented yet");
         return rc;
     }
 
@@ -78,14 +81,15 @@ public class Movable extends ThingRW {
      * @param point
      *            OUT: the calculated current position
      * @param currentTime
-     *            &lt; 0: relative to the starttime (negated)<br>
+     *            &lt; 0: relative to the start time (negated)<br>
      *            otherwise: the current time in milliseconds
      */
     public void currentPosition(PointRW point, long currentTime) {
-        if (this.getVelocity() == 0.0) {
-            point.x = this.widget.getCenter().x;
-            point.y = this.widget.getCenter().y;
-        } else {
+        if (this.getVelocity() == 0.0)
+            point.clone(this.widget.getCenter());
+        else {
+            if (this.startTime == 0)
+                this.startTime = this.model.getNow();
             if (currentTime < 0)
                 currentTime = this.startTime - currentTime;
             // v = s / t => s = v * t
index caf09ca4e6546169a9d9c25a5eb06f3c5a28575e..7422622b299519aa029e46c26aebf6614e0c04d3 100644 (file)
@@ -16,11 +16,11 @@ public class PointRW {
     /**
      * x coordinate (horizontal)
      */
-    public double x;
+    public double x = 0;
     /**
      * y coordinate (vertical)
      */
-    public double y;
+    public double y = 0;
 
     /**
      * Constructor.
index fe141565f7af9286183b5bb9bf1cf2cda407db0a..226b6c9625e66ac4f21a24ffcd93c82e180ab2df 100644 (file)
@@ -76,7 +76,7 @@ public class RectangleRW {
      */
     public boolean inside(double x, double y) {
         boolean rc = x >= this.x && x < this.x + this.width && y >= this.y
-            && y < this.y + this.height;
+                && y < this.y + this.height;
         return rc;
     }
 
@@ -91,8 +91,8 @@ public class RectangleRW {
      */
     public boolean inside(PointRW point) {
         boolean rc = point.x >= this.x && point.x < this.x + this.width
-            && point.y >= this.y && point.y < this.y + this.height;
-        return rc;
+                && point.y >= this.y && point.y < this.y + this.height;
+                return rc;
     }
 
     /**
@@ -109,12 +109,12 @@ public class RectangleRW {
         // inside of the
         // instance:
         boolean rc = inside(rectangle.x, rectangle.y)
-            || inside(rectangle.x + RectangleRW.NEAR_1 * rectangle.width,
-                rectangle.y)
-            || inside(rectangle.x, rectangle.y + RectangleRW.NEAR_1
-                * rectangle.height)
-            || inside(rectangle.x + RectangleRW.NEAR_1 * rectangle.width,
-                rectangle.y + RectangleRW.NEAR_1 * rectangle.height);
+                || inside(rectangle.x + RectangleRW.NEAR_1 * rectangle.width,
+                    rectangle.y)
+                    || inside(rectangle.x, rectangle.y + RectangleRW.NEAR_1
+                        * rectangle.height)
+                        || inside(rectangle.x + RectangleRW.NEAR_1 * rectangle.width,
+                            rectangle.y + RectangleRW.NEAR_1 * rectangle.height);
         return rc;
     }
 
index 0f5d2ef3053568a3b55685c49f606e72624aebce..27b19676b8fd22afd0bf3fce43ff901cd6d8fc95 100644 (file)
@@ -20,11 +20,11 @@ public class LineSwing extends WidgetSwing {
     /**
      * the first point of the line
      */
-    private PointRW from;
+    private PointRW from = new PointRW(0, 0);
     /**
      * the last point of the line
      */
-    private PointRW to;
+    private PointRW to = new PointRW(0, 0);;
 
     /**
      * Constructor.
@@ -38,8 +38,8 @@ public class LineSwing extends WidgetSwing {
      */
     public LineSwing(PointRW from, PointRW to, Model model) {
         super(from, model);
-        this.from = from;
-        this.to = to;
+        this.from.clone(from);
+        this.to.clone(to);
     }
 
     @Override
index 5eecece217f87ae420f0387fc5e7f9e068ff5391..0afbf939cd68242b9cbfc0c92a4724074c14de42 100644 (file)
@@ -59,7 +59,6 @@ class PanelSwing extends JPanel {
 
     @Override
     public void paintComponent(Graphics graphics) {
-
         super.paintComponent(graphics);
         doDrawing(graphics);
     }
index ad5195f5583ec620c82b2ac770478b92be19b471..82173254e775907eb18c8012b329f9379bbd26be 100644 (file)
@@ -20,7 +20,7 @@ import jlecture.simulation.WidgetType;
  *
  */
 public class RectangleSwing extends WidgetSwing {
-    private DimensionRW dimension = null;
+    private DimensionRW dimension = new DimensionRW(0, 0);
 
     private boolean filled = false;
 
@@ -40,7 +40,7 @@ public class RectangleSwing extends WidgetSwing {
     public RectangleSwing(PointRW center, DimensionRW dimension,
         boolean filled, Model model) {
         super(center, model);
-        this.dimension = dimension;
+        this.dimension.clone(dimension);
         this.filled = filled;
         calculateContour();
     }
index 0835de927a4c0862bada656aed5e0b006268bef7..b49dba25d8ce0e5227f5ee15413f6f4636a75a6f 100644 (file)
@@ -52,7 +52,7 @@ public class SheetSwing extends JFrame implements ISheet, ActionListener {
         this.title = title;
         this.model = model;
         this.timer = new Timer(100, this);
-        this.timer.setInitialDelay(2000);
+        this.timer.setInitialDelay(200);
         model.setSheet(this);
         initUI();
     }
index f92b1077414e07b55a2ba06efef71715bc309278..b7e04f7ad77e258f2ce543931fee955da64b12e1 100644 (file)
@@ -28,7 +28,7 @@ public abstract class WidgetSwing implements IWidget {
      * The center of the item in real world coordinates. This simply allows
      * calculating distance between 2 items.
      */
-    protected PointRW center;
+    protected PointRW center = new PointRW(0, 0);
     /**
      * The smallest rectangle containing all parts of the item (in real world
      * coordinates).
@@ -54,7 +54,7 @@ public abstract class WidgetSwing implements IWidget {
      *            simulator model
      */
     public WidgetSwing(PointRW center, Model model) {
-        this.center = center;
+        this.center.clone(center);
         this.model = model;
         model.getItems().add(this);
     }
@@ -100,8 +100,7 @@ public abstract class WidgetSwing implements IWidget {
 
     @Override
     public void setCenter(PointRW center) {
-        this.center.x = center.x;
-        this.center.y = center.y;
+        this.center.clone(center);
         calculateContour();
     }
 }
index 10dc8d940e079b35e8502d338953dc6f4a69e00a..88ec74f7fc6b39473f75cffbe9e4d724b8426aa3 100644 (file)
@@ -58,10 +58,10 @@ public class XCrossSwing extends WidgetSwing {
         this.model.transform(this.contour, this.contourSwing);
         graphics.drawLine(this.contourSwing.x, this.contourSwing.y,
             this.contourSwing.x + this.contourSwing.width, this.contourSwing.y
-                + this.contourSwing.height);
+            + this.contourSwing.height);
         graphics.drawLine(this.contourSwing.x + this.contourSwing.width,
             this.contourSwing.y, this.contourSwing.x, this.contourSwing.y
-                + this.contourSwing.height);
+            + this.contourSwing.height);
     }
 
     @Override