From 7d81d8e38863b8bd6bfe6ffbde603beeaa909692 Mon Sep 17 00:00:00 2001 From: hama Date: Sat, 11 Oct 2014 14:27:31 +0200 Subject: [PATCH] asserts replaced by RuntimeExceptions * cloning instead of object assignment --- src/jlection/robotic/RobotModel.java | 4 ++-- src/jlecture/simulation/Barrier.java | 6 ++++-- src/jlecture/simulation/Model.java | 8 +++++--- src/jlecture/simulation/Movable.java | 22 +++++++++++++--------- src/jlecture/simulation/PointRW.java | 4 ++-- src/jlecture/simulation/RectangleRW.java | 18 +++++++++--------- src/jlecture/swing/LineSwing.java | 8 ++++---- src/jlecture/swing/PanelSwing.java | 1 - src/jlecture/swing/RectangleSwing.java | 4 ++-- src/jlecture/swing/SheetSwing.java | 2 +- src/jlecture/swing/WidgetSwing.java | 7 +++---- src/jlecture/swing/XCrossSwing.java | 4 ++-- 12 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/jlection/robotic/RobotModel.java b/src/jlection/robotic/RobotModel.java index d939329..eaa3092 100644 --- a/src/jlection/robotic/RobotModel.java +++ b/src/jlection/robotic/RobotModel.java @@ -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()); diff --git a/src/jlecture/simulation/Barrier.java b/src/jlecture/simulation/Barrier.java index 8330dd4..c1c7cb0 100644 --- a/src/jlecture/simulation/Barrier.java +++ b/src/jlecture/simulation/Barrier.java @@ -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; } diff --git a/src/jlecture/simulation/Model.java b/src/jlecture/simulation/Model.java index b358e00..f604219 100644 --- a/src/jlecture/simulation/Model.java +++ b/src/jlecture/simulation/Model.java @@ -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; } diff --git a/src/jlecture/simulation/Movable.java b/src/jlecture/simulation/Movable.java index f15a156..336d566 100644 --- a/src/jlecture/simulation/Movable.java +++ b/src/jlecture/simulation/Movable.java @@ -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 - * < 0: relative to the starttime (negated)
+ * < 0: relative to the start time (negated)
* 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 diff --git a/src/jlecture/simulation/PointRW.java b/src/jlecture/simulation/PointRW.java index caf09ca..7422622 100644 --- a/src/jlecture/simulation/PointRW.java +++ b/src/jlecture/simulation/PointRW.java @@ -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. diff --git a/src/jlecture/simulation/RectangleRW.java b/src/jlecture/simulation/RectangleRW.java index fe14156..226b6c9 100644 --- a/src/jlecture/simulation/RectangleRW.java +++ b/src/jlecture/simulation/RectangleRW.java @@ -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; } diff --git a/src/jlecture/swing/LineSwing.java b/src/jlecture/swing/LineSwing.java index 0f5d2ef..27b1967 100644 --- a/src/jlecture/swing/LineSwing.java +++ b/src/jlecture/swing/LineSwing.java @@ -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 diff --git a/src/jlecture/swing/PanelSwing.java b/src/jlecture/swing/PanelSwing.java index 5eecece..0afbf93 100644 --- a/src/jlecture/swing/PanelSwing.java +++ b/src/jlecture/swing/PanelSwing.java @@ -59,7 +59,6 @@ class PanelSwing extends JPanel { @Override public void paintComponent(Graphics graphics) { - super.paintComponent(graphics); doDrawing(graphics); } diff --git a/src/jlecture/swing/RectangleSwing.java b/src/jlecture/swing/RectangleSwing.java index ad5195f..8217325 100644 --- a/src/jlecture/swing/RectangleSwing.java +++ b/src/jlecture/swing/RectangleSwing.java @@ -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(); } diff --git a/src/jlecture/swing/SheetSwing.java b/src/jlecture/swing/SheetSwing.java index 0835de9..b49dba2 100644 --- a/src/jlecture/swing/SheetSwing.java +++ b/src/jlecture/swing/SheetSwing.java @@ -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(); } diff --git a/src/jlecture/swing/WidgetSwing.java b/src/jlecture/swing/WidgetSwing.java index f92b107..b7e04f7 100644 --- a/src/jlecture/swing/WidgetSwing.java +++ b/src/jlecture/swing/WidgetSwing.java @@ -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(); } } diff --git a/src/jlecture/swing/XCrossSwing.java b/src/jlecture/swing/XCrossSwing.java index 10dc8d9..88ec74f 100644 --- a/src/jlecture/swing/XCrossSwing.java +++ b/src/jlecture/swing/XCrossSwing.java @@ -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 -- 2.39.5