]> gitweb.hamatoma.de Git - robosim/commitdiff
Refactoring: dividing Robot/RobotModel from the Movable/Model
authorhama <hama@siduction.net>
Thu, 9 Oct 2014 22:31:00 +0000 (00:31 +0200)
committerhama <hama@siduction.net>
Thu, 9 Oct 2014 22:31:00 +0000 (00:31 +0200)
* renamed Robot to Movable
* Robot is a new sub class of Movable
* RobotModel is a new sub class of Model
* Widget renamed to WidgetSWT
* XCross renamed to XCrossSWT

17 files changed:
src/jlection/robotic/RoboSim.java [new file with mode: 0644]
src/jlection/robotic/Robot.java [new file with mode: 0644]
src/jlection/robotic/RobotModel.java [new file with mode: 0644]
src/jlecture/simulation/Model.java
src/jlecture/simulation/Movable.java [new file with mode: 0644]
src/jlecture/simulation/Robot.java [deleted file]
src/jlecture/simulation/Simulator.java [deleted file]
src/jlecture/swt/PanelSWT.java
src/jlecture/swt/RectangleSWT.java
src/jlecture/swt/SheetSWT.java
src/jlecture/swt/Widget.java [deleted file]
src/jlecture/swt/WidgetSWT.java [new file with mode: 0644]
src/jlecture/swt/XCross.java [deleted file]
src/jlecture/swt/XCrossSWT.java [new file with mode: 0644]
test/jlecture/junit/ModelTest.java [deleted file]
test/jlecture/junit/RobotModelTest.java [new file with mode: 0644]
test/jlecture/junit/WidgetTest.java

diff --git a/src/jlection/robotic/RoboSim.java b/src/jlection/robotic/RoboSim.java
new file mode 100644 (file)
index 0000000..c01c1e4
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ *
+ */
+package jlection.robotic;
+
+import java.awt.Dimension;
+import java.awt.Point;
+
+import javax.swing.SwingUtilities;
+
+import jlecture.simulation.Model;
+import jlecture.swt.SheetSWT;
+
+/**
+ * Defines the main routine of the robot simulator.
+ *
+ * Initializes the model and the graphical view and start the simulation (done
+ * by the model).
+ *
+ * @author hm
+ *
+ */
+public class RoboSim {
+    /**
+     * The main routine.
+     *
+     * @param args
+     *            the program arguments
+     */
+    public static void main(String[] args) {
+        RoboSim simulator = new RoboSim();
+        simulator.exec();
+    }
+
+    /**
+     * Initializes the model and the graphical view.
+     */
+    public void exec() {
+        Model model = new RobotModel();
+        SheetSWT sheet = new SheetSWT(new Point(50, 10), new Dimension(1000,
+            800), "Roboterfeld", model);
+        model.createModel();
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                sheet.setVisible(true);
+            }
+        });
+
+    }
+
+}
diff --git a/src/jlection/robotic/Robot.java b/src/jlection/robotic/Robot.java
new file mode 100644 (file)
index 0000000..f3bd9c5
--- /dev/null
@@ -0,0 +1,21 @@
+package jlection.robotic;
+
+import jlecture.simulation.IWidget;
+import jlecture.simulation.Movable;
+
+/**
+ * Implements a robot.
+ *
+ * A robot is a movable thing which has strategies to handle collisions and find
+ * destinations.
+ *
+ * @author hm
+ *
+ */
+public class Robot extends Movable {
+
+    public Robot(IWidget widget) {
+        super(widget);
+    }
+
+}
diff --git a/src/jlection/robotic/RobotModel.java b/src/jlection/robotic/RobotModel.java
new file mode 100644 (file)
index 0000000..56f505e
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ *
+ */
+package jlection.robotic;
+
+import jlecture.simulation.Bar;
+import jlecture.simulation.DimensionRW;
+import jlecture.simulation.Model;
+import jlecture.simulation.Movable;
+import jlecture.simulation.PointRW;
+
+/**
+ * Implements a robot simulation.
+ *
+ * @author hm
+ *
+ */
+public class RobotModel extends Model {
+    /**
+     * Builds the elements of the simulator.
+     *
+     * Sets the bars and randomly the position of some robots with randomly set
+     * initial states (velocity, destination).
+     */
+    @Override
+    public void createModel() {
+        this.things.add(new Bar(this.sheet.createRectangle(
+            new PointRW(500, 100), new DimensionRW(600, 50), false, this)));
+        this.things.add(new Bar(this.sheet.createRectangle(
+            new PointRW(500, 400), new DimensionRW(600, 50), false, this)));
+        this.things.add(new Bar(this.sheet.createRectangle(
+            new PointRW(500, 700), new DimensionRW(600, 50), false, this)));
+
+        PointRW destination = new PointRW(0, 0);
+        for (int ix = 0; ix < 20; ix++) {
+            Movable robot = new Movable(this.sheet.createRectangle(new PointRW(
+                500, 500), new DimensionRW(10, 10), true, this));
+            robot.getWidget().setCenter(openPosition());
+            destination.x = robot.getWidget().getCenter().x;
+            destination.y = robot.getWidget().getCenter().y;
+            randomDestination(destination);
+            // 100 seconds for the whole width:
+            double velocity = 1 / 100.0 * this.region.width
+                    * (0.5 + this.random.nextDouble() * 0.5);
+            robot.setMove(destination, velocity);
+            this.things.add(robot);
+        }
+        this.isReady = true;
+    }
+
+}
index d16017a875a923eede9805f1c30fcb64dfa8af58..1af27cb0be9f3c0aa283246d044f64456d9369fb 100644 (file)
@@ -25,14 +25,32 @@ import java.util.Random;
  * @author hm
  *
  */
-public class Model {
-    private boolean isReady = false;
+public abstract class Model {
+    /**
+     * <code>true</code>: the model is fully initialized.
+     */
+    protected boolean isReady = false;
+    /**
+     * The list of widgets which should be drawn.
+     */
     protected ArrayList<IWidget> widgets = new ArrayList<IWidget>();
+    /**
+     * The list of things which are relevant to the simulation.
+     */
     protected ArrayList<ThingRW> things = new ArrayList<ThingRW>();
+    /**
+     * The drawing area (graphical view).
+     */
     protected ISheet sheet = null;
+    /**
+     * The simulator area with real world coordinates.
+     */
     protected RectangleRW region = new RectangleRW(0, 0, 1024, 1024 * 2 / 3);
 
-    Random random = new Random(0x4711);
+    /**
+     * A pseude random generator for repeatable results.
+     */
+    protected Random random = new Random(0x4711);
 
     /**
      * Constructor.
@@ -43,33 +61,11 @@ public class Model {
     /**
      * Builds the elements of the simulator.
      *
-     * Sets the bars and randomly the position of some robots with randomly set
-     * initial states (velocity, destination).
-     */
-    public void createModel() {
-        things.add(new Bar(sheet.createRectangle(
-            new PointRW(500, 100), new DimensionRW(600, 50), false, this)));
-        things.add(new Bar(sheet.createRectangle(
-            new PointRW(500, 400), new DimensionRW(600, 50), false, this)));
-        things.add(new Bar(sheet.createRectangle(
-            new PointRW(500, 700), new DimensionRW(600, 50), false, this)));
-
-        PointRW destination = new PointRW(0, 0);
-        for (int ix = 0; ix < 20; ix++) {
-            Robot robot = new Robot(sheet.createRectangle(new PointRW(500,
-                500), new DimensionRW(10, 10), true, this));
-            robot.getWidget().setCenter(openPosition());
-            destination.x = robot.getWidget().getCenter().x;
-            destination.y = robot.getWidget().getCenter().y;
-            randomDestination(destination);
-            // 100 seconds for the whole width:
-            double velocity = 1 / 100.0 * region.width
-                    * (0.5 + random.nextDouble() * 0.5);
-            robot.setMove(destination, velocity);
-            things.add(robot);
-        }
-        isReady = true;
-    }
+     * Creates the simulated things and put it into the variable
+     * <code>things</code>. For the movable things it will initialize the
+     * velocity and destination.
+     */
+    public abstract void createModel();
 
     /**
      * Returns the list of items.
@@ -77,7 +73,7 @@ public class Model {
      * @return the list of items
      */
     public List<IWidget> getItems() {
-        return widgets;
+        return this.widgets;
     }
 
     /**
@@ -86,14 +82,14 @@ public class Model {
      * @return the sheet
      */
     public ISheet getSheet() {
-        return sheet;
+        return this.sheet;
     }
 
     /**
      * @return the widgets
      */
     public ArrayList<IWidget> getWidgets() {
-        return widgets;
+        return this.widgets;
     }
 
     /**
@@ -119,12 +115,12 @@ public class Model {
      */
     public ThingRW occupied(PointRW point, IWidget exclude) {
         ThingRW rc = null;
-        for (ThingRW current : things)
+        for (ThingRW current : this.things)
             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;
                 }
@@ -139,10 +135,10 @@ public class Model {
      * are necessary for one simulation step.
      */
     public void onTimer() {
-        if (isReady)
-            for (ThingRW thing : things)
-                if (thing instanceof Robot) {
-                    Robot robot = (Robot) thing;
+        if (this.isReady)
+            for (ThingRW thing : this.things)
+                if (thing instanceof Movable) {
+                    Movable robot = (Movable) thing;
                     PointRW center = robot.getWidget().getCenter();
                     // moves to the position belonging to the current move:
                     robot.currentPosition(center, System.currentTimeMillis());
@@ -158,9 +154,9 @@ public class Model {
     public PointRW openPosition() {
         PointRW rc = new PointRW(0, 0);
         do {
-            rc.x = region.x + random.nextDouble() * region.width;
-            rc.y = region.x + random.nextDouble()
-                    * region.height;
+            rc.x = this.region.x + this.random.nextDouble() * this.region.width;
+            rc.y = this.region.x + this.random.nextDouble()
+                * this.region.height;
         } while (occupied(rc, null) != null);
         return rc;
     }
@@ -172,18 +168,18 @@ public class Model {
      *            IN/OUT: the point to change
      */
     protected void randomDestination(PointRW destination) {
-        switch (random.nextInt() % 4) {
+        switch (this.random.nextInt() % 4) {
         case 0:
-            destination.x = region.x;
+            destination.x = this.region.x;
             break;
         case 1:
-            destination.x = region.x + region.width;
+            destination.x = this.region.x + this.region.width;
             break;
         case 2:
-            destination.y = region.y;
+            destination.y = this.region.y;
             break;
         default:
-            destination.y = region.y + region.height;
+            destination.y = this.region.y + this.region.height;
             break;
         }
     }
diff --git a/src/jlecture/simulation/Movable.java b/src/jlecture/simulation/Movable.java
new file mode 100644 (file)
index 0000000..050ac03
--- /dev/null
@@ -0,0 +1,118 @@
+/**
+ *
+ */
+package jlecture.simulation;
+
+/**
+ * Implements a movable thing.
+ *
+ * At any time this thing has a current move which is defined by a start
+ * position, a destination position and a velocity.
+ *
+ * A special case is velocity = 0: then the thing is not moving.
+ *
+ * Using this data the simulator can calculate the current position.
+ *
+ * @author hm
+ *
+ */
+public class Movable extends ThingRW {
+    /**
+     * Velocity of the robot in units per second.
+     */
+    private double velocity = 0.0;
+
+    /**
+     * The position of the last direction change.
+     */
+    private PointRW start = new PointRW(0, 0);
+
+    /**
+     * The destination of the currrent move.
+     */
+    private PointRW destination = new PointRW(0, 0);
+    /**
+     * The start time of the current move (in msec).
+     */
+    private long startTime = 0L;
+    protected Model model = null;
+
+    /**
+     * Constructor.
+     *
+     * @param widget
+     *            the widget in the graphical view
+     */
+    public Movable(IWidget widget) {
+        super(widget);
+        this.model = widget.getModel();
+    }
+
+    /**
+     * Calculates the position (for the current move).
+     *
+     * @param point
+     *            OUT: the calculated current position
+     * @param currentTime
+     *            < 0: relative to the starttime (negated)<br>
+     *            otherwise: the current time in milliseconds
+     */
+    public void currentPosition(PointRW point, long currentTime) {
+        ;
+        if (this.velocity == 0.0) {
+            point.x = this.widget.getCenter().x;
+            point.y = this.widget.getCenter().y;
+        } else {
+            if (currentTime < 0)
+                currentTime = this.startTime - currentTime;
+            // v = s / t => s = v * t
+            double time = (currentTime - this.startTime) / 1000.0;
+            double distance = this.velocity * time;
+            // hypotenuse:
+            // c = sqrt(x_target _ x_start)**2 + (y_target _ y_start)**2)
+            // Theory of intersecting lines:
+            // x_diff : distance = (x_target - x_start) / c
+            // y_diff : distance = (y_target - y_start) / c
+            double x_diff = this.destination.x - this.start.x;
+            double y_diff = this.destination.y - this.start.y;
+            double c = Math.sqrt(x_diff * x_diff + y_diff * y_diff);
+            x_diff = distance * x_diff / c;
+            y_diff = distance * y_diff / c;
+            point.x = this.start.x + x_diff;
+            point.y = this.start.y + y_diff;
+        }
+    }
+
+    /**
+     * @return the widget of the robot
+     */
+    public IWidget getWidget() {
+        return this.widget;
+    }
+
+    /**
+     * Sets the current move.
+     *
+     * The start position is the current position (the center of the widget).
+     *
+     * @param destination
+     *            the destination of the move
+     * @param velocity
+     *            the velocity in units per second. A unit is defined by the
+     *            real world coordinates
+     */
+    public void setMove(PointRW destination, double velocity) {
+        this.start = this.widget.getCenter();
+        this.destination = destination;
+        this.startTime = System.currentTimeMillis();
+        this.velocity = velocity;
+    }
+
+    /**
+     * @param widget
+     *            the widget to set
+     */
+    public void setWidget(IWidget widget) {
+        this.widget = widget;
+    }
+}
diff --git a/src/jlecture/simulation/Robot.java b/src/jlecture/simulation/Robot.java
deleted file mode 100644 (file)
index 00deab3..0000000
+++ /dev/null
@@ -1,116 +0,0 @@
-/**
- *
- */
-package jlecture.simulation;
-
-/**
- * Implements a robot.
- *
- * A robot is a thing which can move. The move is defined by a start position, a
- * destination position and a velocity.
- *
- * Using this data the simulator can calculate the current position.
- *
- * @author hm
- *
- */
-public class Robot extends ThingRW {
-    /**
-     * Velocity of the robot in units per second.
-     */
-    private double velocity = 0.0;
-
-    /**
-     * The position of the last direction change.
-     */
-    private PointRW start = new PointRW(0, 0);
-
-    /**
-     * The destination of the currrent move.
-     */
-    private PointRW destination = new PointRW(0, 0);
-    /**
-     * The start time of the current move (in msec).
-     */
-    private long startTime = 0L;
-    private Model model = null;
-
-    /**
-     * Constructor.
-     *
-     * @param widget
-     *            the widget in the graphical view
-     */
-    public Robot(IWidget widget) {
-        super(widget);
-        model = widget.getModel();
-    }
-
-    /**
-     * Calculates the position (for the current move).
-     *
-     * @param point
-     *            OUT: the calculated current position
-     * @param currentTime
-     *            < 0: relative to the starttime (negated)<br>
-     *            otherwise: the current time in milliseconds
-     */
-    public void currentPosition(PointRW point, long currentTime) {
-        ;
-        if (velocity == 0.0) {
-            point.x = widget.getCenter().x;
-            point.y = widget.getCenter().y;
-        } else {
-            if (currentTime < 0)
-                currentTime = startTime - currentTime;
-            // v = s / t => s = v * t
-            double time = (currentTime - startTime) / 1000.0;
-            double distance = velocity * time;
-            // hypotenuse:
-            // c = sqrt(x_target _ x_start)**2 + (y_target _ y_start)**2)
-            // Theory of intersecting lines:
-            // x_diff : distance = (x_target - x_start) / c
-            // y_diff : distance = (y_target - y_start) / c
-            double x_diff = destination.x - start.x;
-            double y_diff = destination.y - start.y;
-            double c = Math.sqrt(x_diff * x_diff + y_diff * y_diff);
-            x_diff = distance * x_diff / c;
-            y_diff = distance * y_diff / c;
-            point.x = start.x + x_diff;
-            point.y = start.y + y_diff;
-        }
-    }
-
-    /**
-     * @return the widget of the robot
-     */
-    public IWidget getWidget() {
-        return widget;
-    }
-
-    /**
-     * Sets the current move.
-     *
-     * The start position is the current position (the center of the widget).
-     *
-     * @param destination
-     *            the destination of the move
-     * @param velocity
-     *            the velocity in units per second. A unit is defined by the
-     *            real world coordinates
-     */
-    public void setMove(PointRW destination, double velocity) {
-        start = widget.getCenter();
-        this.destination = destination;
-        startTime = System.currentTimeMillis();
-        this.velocity = velocity;
-    }
-
-    /**
-     * @param widget
-     *            the widget to set
-     */
-    public void setWidget(IWidget widget) {
-        this.widget = widget;
-    }
-}
diff --git a/src/jlecture/simulation/Simulator.java b/src/jlecture/simulation/Simulator.java
deleted file mode 100644 (file)
index 62788e0..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- *
- */
-package jlecture.simulation;
-
-import java.awt.Dimension;
-import java.awt.Point;
-
-import javax.swing.SwingUtilities;
-
-import jlecture.swt.SheetSWT;
-
-/**
- * Defines the main routine of the simulator.
- *
- * Initializes the model and the graphical view and start the simulation (done
- * by the model).
- *
- * @author hm
- *
- */
-public class Simulator {
-    /**
-     * The main routine.
-     *
-     * @param args
-     *            the program arguments
-     */
-    public static void main(String[] args) {
-        Simulator simulator = new Simulator();
-        simulator.exec();
-    }
-
-    /**
-     * Initializes the model and the graphical view.
-     */
-    public void exec() {
-        Model model = new Model();
-        SheetSWT sheet = new SheetSWT(new Point(50, 10), new Dimension(1000,
-            800), "Roboterfeld", model);
-        model.createModel();
-        SwingUtilities.invokeLater(new Runnable() {
-            @Override
-            public void run() {
-                sheet.setVisible(true);
-            }
-        });
-
-    }
-
-}
index ce2f30bca71e8b43579c6977234ec173d031f853..9d3680ba1efc358ec5ab6675ad6eb813cc7094e8 100644 (file)
@@ -48,11 +48,11 @@ class PanelSWT extends JPanel {
     private void doDrawing(Graphics graphics) {
         Graphics2D g2d = (Graphics2D) graphics;
 
-        g2d.setColor(foreground);
-        g2d.setBackground(background);
+        g2d.setColor(this.foreground);
+        g2d.setBackground(this.background);
 
-        for (IWidget item : model.getItems()) {
-            Widget item2 = (Widget) item;
+        for (IWidget item : this.model.getItems()) {
+            WidgetSWT item2 = (WidgetSWT) item;
             item2.draw(g2d);
         }
     }
index 19bf6a7ca1afa477c2ca1727bdae136bfaca5eec..f75551c3860ca6877fcab6e42191cea8777d8032 100644 (file)
@@ -20,7 +20,7 @@ import jlecture.simulation.PointRW;
  * @author hm
  *
  */
-public class RectangleSWT extends Widget {
+public class RectangleSWT extends WidgetSWT {
     private DimensionRW dimension = null;
 
     private boolean filled = false;
@@ -49,21 +49,21 @@ public class RectangleSWT extends Widget {
 
     @Override
     protected void calculateContour() {
-        contour.x = center.x - dimension.width / 2;
-        contour.y = center.y - dimension.height / 2;
-        contour.width = dimension.width;
-        contour.height = dimension.height;
+        this.contour.x = this.center.x - this.dimension.width / 2;
+        this.contour.y = this.center.y - this.dimension.height / 2;
+        this.contour.width = this.dimension.width;
+        this.contour.height = this.dimension.height;
     }
 
     @Override
     public void draw(Graphics2D graphics) {
         calculateContour();
-        model.transform(contour, contourSWT);
-        if (filled)
-            graphics.fillRect(contourSWT.x, contourSWT.y,
-                contourSWT.width, contourSWT.height);
+        this.model.transform(this.contour, this.contourSWT);
+        if (this.filled)
+            graphics.fillRect(this.contourSWT.x, this.contourSWT.y,
+                this.contourSWT.width, this.contourSWT.height);
         else
-            graphics.drawRect(contourSWT.x, contourSWT.y,
-                contourSWT.width, contourSWT.height);
+            graphics.drawRect(this.contourSWT.x, this.contourSWT.y,
+                this.contourSWT.width, this.contourSWT.height);
     }
 }
index b76fab64d7e56c603a85a4967682235f9ed1ef3b..d8c9ad9429ad1175b66dc61a30d82865bd88c7cf 100644 (file)
@@ -49,8 +49,8 @@ public class SheetSWT extends JFrame implements ISheet, ActionListener {
         this.dimension = dimension;
         this.title = title;
         this.model = model;
-        timer = new Timer(100, this);
-        timer.setInitialDelay(2000);
+        this.timer = new Timer(100, this);
+        this.timer.setInitialDelay(2000);
         model.setSheet(this);
         initUI();
     }
@@ -59,7 +59,7 @@ public class SheetSWT extends JFrame implements ISheet, ActionListener {
     public void actionPerformed(ActionEvent e) {
         // if (e.getActionCommand().equals(""))
         {
-            model.onTimer();
+            this.model.onTimer();
             repaint();
         }
     }
@@ -73,7 +73,7 @@ public class SheetSWT extends JFrame implements ISheet, ActionListener {
     @Override
     public IWidget createXCross(PointRW center, DimensionRW dimension,
         Model model) {
-        return new XCross(center, dimension, model);
+        return new XCrossSWT(center, dimension, model);
     }
 
     /**
@@ -81,18 +81,18 @@ public class SheetSWT extends JFrame implements ISheet, ActionListener {
      */
     private void initUI() {
 
-        setTitle(title);
+        setTitle(this.title);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-        add(new PanelSWT(model));
+        add(new PanelSWT(this.model));
 
-        setSize(dimension.width, dimension.height);
+        setSize(this.dimension.width, this.dimension.height);
 
-        if (position == null)
+        if (this.position == null)
             // build the window in the center:
             setLocationRelativeTo(null);
         else
-            setLocation(position);
-        timer.start();
+            setLocation(this.position);
+        this.timer.start();
     }
 }
diff --git a/src/jlecture/swt/Widget.java b/src/jlecture/swt/Widget.java
deleted file mode 100644 (file)
index 5201112..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/**
- *
- */
-package jlecture.swt;
-
-import java.awt.Graphics2D;
-
-import jlecture.simulation.IWidget;
-import jlecture.simulation.Model;
-import jlecture.simulation.PointRW;
-import jlecture.simulation.RectangleRW;
-
-/**
- * Base class of all drawing items.
- *
- * The widget is characterized by the center: Though it is very simple to
- * calculate the distance of two widgets: the distance of the two centers.
- *
- * Another important property is the contour: this is the smallest rectangle
- * containing all parts of the widget.
- *
- * @author hm
- *
- */
-public abstract class Widget implements IWidget {
-    /**
-     * The center of the item in real world coordinates. This simply allows
-     * calculating distance between 2 items.
-     */
-    protected PointRW center;
-    /**
-     * The smallest rectangle containing all parts of the item.
-     */
-    protected RectangleRW contour = new RectangleRW(0, 0, 0, 0);
-
-    /**
-     * Simulator model.
-     */
-    protected Model model;
-
-    /**
-     * Constructor.
-     *
-     * @param center
-     *            the central position of the widget.
-     */
-    public Widget(PointRW center, Model model) {
-        this.center = center;
-        this.model = model;
-        model.getItems().add(this);
-    }
-
-    /**
-     * Calculates the smallest rectangle containing all parts of the items.
-     */
-    protected abstract void calculateContour();
-
-    /**
-     * Draws the item with the given graphics.
-     *
-     * @param graphics
-     *            properties of the pencil a.s.o
-     */
-    public abstract void draw(Graphics2D graphics);
-
-    /**
-     * @return the center
-     */
-    @Override
-    public PointRW getCenter() {
-        return center;
-    }
-
-    /**
-     * Returns the smallest rectangle containing all parts of the item.
-     *
-     * @return the contour
-     */
-    @Override
-    public RectangleRW getContour() {
-        return contour;
-    }
-
-    /**
-     * @return the model
-     */
-    @Override
-    public Model getModel() {
-        return model;
-    }
-
-    @Override
-    public void setCenter(PointRW center) {
-        this.center.x = center.x;
-        this.center.y = center.y;
-        calculateContour();
-    }
-}
diff --git a/src/jlecture/swt/WidgetSWT.java b/src/jlecture/swt/WidgetSWT.java
new file mode 100644 (file)
index 0000000..b4b6f43
--- /dev/null
@@ -0,0 +1,98 @@
+/**
+ *
+ */
+package jlecture.swt;
+
+import java.awt.Graphics2D;
+
+import jlecture.simulation.IWidget;
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+import jlecture.simulation.RectangleRW;
+
+/**
+ * Base class of all drawing items.
+ *
+ * The widget is characterized by the center: Though it is very simple to
+ * calculate the distance of two widgets: the distance of the two centers.
+ *
+ * Another important property is the contour: this is the smallest rectangle
+ * containing all parts of the widget.
+ *
+ * @author hm
+ *
+ */
+public abstract class WidgetSWT implements IWidget {
+    /**
+     * The center of the item in real world coordinates. This simply allows
+     * calculating distance between 2 items.
+     */
+    protected PointRW center;
+    /**
+     * The smallest rectangle containing all parts of the item.
+     */
+    protected RectangleRW contour = new RectangleRW(0, 0, 0, 0);
+
+    /**
+     * Simulator model.
+     */
+    protected Model model;
+
+    /**
+     * Constructor.
+     *
+     * @param center
+     *            the central position of the widget.
+     */
+    public WidgetSWT(PointRW center, Model model) {
+        this.center = center;
+        this.model = model;
+        model.getItems().add(this);
+    }
+
+    /**
+     * Calculates the smallest rectangle containing all parts of the items.
+     */
+    protected abstract void calculateContour();
+
+    /**
+     * Draws the item with the given graphics.
+     *
+     * @param graphics
+     *            properties of the pencil a.s.o
+     */
+    public abstract void draw(Graphics2D graphics);
+
+    /**
+     * @return the center
+     */
+    @Override
+    public PointRW getCenter() {
+        return this.center;
+    }
+
+    /**
+     * Returns the smallest rectangle containing all parts of the item.
+     *
+     * @return the contour
+     */
+    @Override
+    public RectangleRW getContour() {
+        return this.contour;
+    }
+
+    /**
+     * @return the model
+     */
+    @Override
+    public Model getModel() {
+        return this.model;
+    }
+
+    @Override
+    public void setCenter(PointRW center) {
+        this.center.x = center.x;
+        this.center.y = center.y;
+        calculateContour();
+    }
+}
diff --git a/src/jlecture/swt/XCross.java b/src/jlecture/swt/XCross.java
deleted file mode 100644 (file)
index 9a34943..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- *
- */
-package jlecture.swt;
-
-import java.awt.Graphics2D;
-import java.awt.Rectangle;
-
-import jlecture.simulation.DimensionRW;
-import jlecture.simulation.Model;
-import jlecture.simulation.PointRW;
-import jlecture.simulation.RectangleRW;
-
-/**
- * Implements a cross similar to a 'X' for SWT. These are the diagonals of a
- * rectangle.
- *
- * @author hm
- *
- */
-public class XCross extends Widget {
-    /**
-     * The length of the square (contour).
-     */
-    private DimensionRW dimension = null;
-    private RectangleRW contour = new RectangleRW(0, 0, 0, 0);
-    private Rectangle contourSWT = new Rectangle();
-
-    /**
-     * Constructor.
-     *
-     * @param center
-     *            the center of the X cross
-     * @param dimension
-     *            the width/height
-     * @param model
-     *            the simulation model
-     */
-    public XCross(PointRW center, DimensionRW dimension, Model model) {
-        super(center, model);
-        this.dimension = dimension;
-        calculateContour();
-    }
-
-    /**
-     * Calculates the contour of the X cross.
-     */
-    @Override
-    protected void calculateContour() {
-        contour.x = center.x - dimension.width / 2;
-        contour.y = center.y - dimension.height / 2;
-        contour.width = dimension.width;
-        contour.height = dimension.height;
-    }
-
-    @Override
-    public void draw(Graphics2D graphics) {
-        calculateContour();
-        model.transform(contour, contourSWT);
-        graphics.drawLine(contourSWT.x, contourSWT.y,
-            contourSWT.x + contourSWT.width, contourSWT.y
-            + contourSWT.height);
-        graphics.drawLine(contourSWT.x + contourSWT.width,
-            contourSWT.y, contourSWT.x, contourSWT.y
-            + contourSWT.height);
-    }
-}
diff --git a/src/jlecture/swt/XCrossSWT.java b/src/jlecture/swt/XCrossSWT.java
new file mode 100644 (file)
index 0000000..bb3440f
--- /dev/null
@@ -0,0 +1,67 @@
+/**
+ *
+ */
+package jlecture.swt;
+
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
+
+import jlecture.simulation.DimensionRW;
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+import jlecture.simulation.RectangleRW;
+
+/**
+ * Implements a cross similar to a 'X' for SWT. These are the diagonals of a
+ * rectangle.
+ *
+ * @author hm
+ *
+ */
+public class XCrossSWT extends WidgetSWT {
+    /**
+     * The length of the square (contour).
+     */
+    private DimensionRW dimension = null;
+    private RectangleRW contour = new RectangleRW(0, 0, 0, 0);
+    private Rectangle contourSWT = new Rectangle();
+
+    /**
+     * Constructor.
+     *
+     * @param center
+     *            the center of the X cross
+     * @param dimension
+     *            the width/height
+     * @param model
+     *            the simulation model
+     */
+    public XCrossSWT(PointRW center, DimensionRW dimension, Model model) {
+        super(center, model);
+        this.dimension = dimension;
+        calculateContour();
+    }
+
+    /**
+     * Calculates the contour of the X cross.
+     */
+    @Override
+    protected void calculateContour() {
+        this.contour.x = this.center.x - this.dimension.width / 2;
+        this.contour.y = this.center.y - this.dimension.height / 2;
+        this.contour.width = this.dimension.width;
+        this.contour.height = this.dimension.height;
+    }
+
+    @Override
+    public void draw(Graphics2D graphics) {
+        calculateContour();
+        this.model.transform(this.contour, this.contourSWT);
+        graphics.drawLine(this.contourSWT.x, this.contourSWT.y,
+            this.contourSWT.x + this.contourSWT.width, this.contourSWT.y
+                + this.contourSWT.height);
+        graphics.drawLine(this.contourSWT.x + this.contourSWT.width,
+            this.contourSWT.y, this.contourSWT.x, this.contourSWT.y
+                + this.contourSWT.height);
+    }
+}
diff --git a/test/jlecture/junit/ModelTest.java b/test/jlecture/junit/ModelTest.java
deleted file mode 100644 (file)
index a25fc3b..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-package jlecture.junit;
-
-import java.awt.Dimension;
-import java.awt.Point;
-import java.awt.Rectangle;
-
-import jlecture.simulation.Bar;
-import jlecture.simulation.DimensionRW;
-import jlecture.simulation.ISheet;
-import jlecture.simulation.IWidget;
-import jlecture.simulation.Model;
-import jlecture.simulation.PointRW;
-import jlecture.simulation.RectangleRW;
-import jlecture.simulation.Robot;
-import jlecture.simulation.ThingRW;
-import jlecture.swt.SheetSWT;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class ModelTest extends Model {
-    @Test
-    public void testOccupied() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
-            "test", this);
-        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
-            new DimensionRW(20, 20), true, this);
-        Bar bar = new Bar(rectangle);
-        things.add(bar);
-        Assert.assertNull(this.occupied(new PointRW(89.9, 89.9), null));
-        Assert.assertNull(this.occupied(new PointRW(110.1, 110.1), null));
-        Assert.assertNotNull(this.occupied(new PointRW(91, 91), null));
-        Assert.assertNotNull(this.occupied(new PointRW(91, 109), null));
-        Assert.assertTrue(rectangle == widgets.get(0));
-        ThingRW thing = things.get(0);
-        Assert.assertTrue(bar == thing);
-    }
-
-    @Test
-    public void testOpenPosition() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
-            "test", this);
-        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
-            new DimensionRW(20, 20), true, this);
-        Bar bar = new Bar(rectangle);
-        things.add(bar);
-        for (int ix = 0; ix < 1000; ix++) {
-            PointRW p = openPosition();
-            Assert.assertNull(this.occupied(p, null));
-        }
-    }
-
-    @Test
-    public void testOpenTransform() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
-            "test", this);
-        Rectangle rect = new Rectangle(0, 0, 0, 0);
-        transform(new RectangleRW(1, 2, 3, 4), rect);
-        Assert.assertEquals(1, rect.x);
-        Assert.assertEquals(2, rect.y);
-        Assert.assertEquals(3, rect.width);
-        Assert.assertEquals(4, rect.height);
-    }
-
-    @Test
-    public void testMove() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(1000, 800),
-            "test", this);
-        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
-            new DimensionRW(20, 20), true, this);
-        Robot robot = new Robot(rectangle);
-        robot.setMove(new PointRW(0, 100), 20.0);
-        PointRW point = new PointRW(0, 0);
-        robot.currentPosition(point, -100);
-        Assert.assertEquals(98.0, point.x, 0.01);
-        Assert.assertEquals(100.0, point.y, 0.01);
-        robot.currentPosition(point, -200);
-        Assert.assertEquals(96.0, point.x, 0.01);
-        Assert.assertEquals(100.0, point.y, 0.01);
-
-        robot.setMove(new PointRW(200, 100), 20.0);
-        robot.currentPosition(point, -100);
-        Assert.assertEquals(102.0, point.x, 0.01);
-        Assert.assertEquals(100.0, point.y, 0.01);
-        robot.currentPosition(point, -200);
-        Assert.assertEquals(104.0, point.x, 0.01);
-        Assert.assertEquals(100.0, point.y, 0.01);
-
-        robot.setMove(new PointRW(100, 0), 20.0);
-        robot.currentPosition(point, -100);
-        Assert.assertEquals(100.0, point.x, 0.01);
-        Assert.assertEquals(98.0, point.y, 0.01);
-        robot.currentPosition(point, -200);
-        Assert.assertEquals(100.0, point.x, 0.01);
-        Assert.assertEquals(96.0, point.y, 0.01);
-
-        robot.setMove(new PointRW(100, 200), 20.0);
-        robot.currentPosition(point, -100);
-        Assert.assertEquals(100.0, point.x, 0.01);
-        Assert.assertEquals(102.0, point.y, 0.01);
-        robot.currentPosition(point, -200);
-        Assert.assertEquals(100.0, point.x, 0.01);
-        Assert.assertEquals(104.0, point.y, 0.01);
-    }
-}
diff --git a/test/jlecture/junit/RobotModelTest.java b/test/jlecture/junit/RobotModelTest.java
new file mode 100644 (file)
index 0000000..2532b7f
--- /dev/null
@@ -0,0 +1,105 @@
+package jlecture.junit;
+
+import java.awt.Dimension;
+import java.awt.Point;
+import java.awt.Rectangle;
+
+import jlection.robotic.RobotModel;
+import jlecture.simulation.Bar;
+import jlecture.simulation.DimensionRW;
+import jlecture.simulation.ISheet;
+import jlecture.simulation.IWidget;
+import jlecture.simulation.Movable;
+import jlecture.simulation.PointRW;
+import jlecture.simulation.RectangleRW;
+import jlecture.simulation.ThingRW;
+import jlecture.swt.SheetSWT;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class RobotModelTest extends RobotModel {
+    @Test
+    public void testMove() {
+        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(1000, 800),
+            "test", this);
+        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
+            new DimensionRW(20, 20), true, this);
+        Movable robot = new Movable(rectangle);
+        robot.setMove(new PointRW(0, 100), 20.0);
+        PointRW point = new PointRW(0, 0);
+        robot.currentPosition(point, -100);
+        Assert.assertEquals(98.0, point.x, 0.01);
+        Assert.assertEquals(100.0, point.y, 0.01);
+        robot.currentPosition(point, -200);
+        Assert.assertEquals(96.0, point.x, 0.01);
+        Assert.assertEquals(100.0, point.y, 0.01);
+
+        robot.setMove(new PointRW(200, 100), 20.0);
+        robot.currentPosition(point, -100);
+        Assert.assertEquals(102.0, point.x, 0.01);
+        Assert.assertEquals(100.0, point.y, 0.01);
+        robot.currentPosition(point, -200);
+        Assert.assertEquals(104.0, point.x, 0.01);
+        Assert.assertEquals(100.0, point.y, 0.01);
+
+        robot.setMove(new PointRW(100, 0), 20.0);
+        robot.currentPosition(point, -100);
+        Assert.assertEquals(100.0, point.x, 0.01);
+        Assert.assertEquals(98.0, point.y, 0.01);
+        robot.currentPosition(point, -200);
+        Assert.assertEquals(100.0, point.x, 0.01);
+        Assert.assertEquals(96.0, point.y, 0.01);
+
+        robot.setMove(new PointRW(100, 200), 20.0);
+        robot.currentPosition(point, -100);
+        Assert.assertEquals(100.0, point.x, 0.01);
+        Assert.assertEquals(102.0, point.y, 0.01);
+        robot.currentPosition(point, -200);
+        Assert.assertEquals(100.0, point.x, 0.01);
+        Assert.assertEquals(104.0, point.y, 0.01);
+    }
+
+    @Test
+    public void testOccupied() {
+        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+            "test", this);
+        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
+            new DimensionRW(20, 20), true, this);
+        Bar bar = new Bar(rectangle);
+        this.things.add(bar);
+        Assert.assertNull(this.occupied(new PointRW(89.9, 89.9), null));
+        Assert.assertNull(this.occupied(new PointRW(110.1, 110.1), null));
+        Assert.assertNotNull(this.occupied(new PointRW(91, 91), null));
+        Assert.assertNotNull(this.occupied(new PointRW(91, 109), null));
+        Assert.assertTrue(rectangle == this.widgets.get(0));
+        ThingRW thing = this.things.get(0);
+        Assert.assertTrue(bar == thing);
+    }
+
+    @Test
+    public void testOpenPosition() {
+        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+            "test", this);
+        IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
+            new DimensionRW(20, 20), true, this);
+        Bar bar = new Bar(rectangle);
+        this.things.add(bar);
+        for (int ix = 0; ix < 1000; ix++) {
+            PointRW p = openPosition();
+            Assert.assertNull(this.occupied(p, null));
+        }
+    }
+
+    @Test
+    public void testOpenTransform() {
+        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+            "test", this);
+        Rectangle rect = new Rectangle(0, 0, 0, 0);
+        transform(new RectangleRW(1, 2, 3, 4), rect);
+        Assert.assertEquals(1, rect.x);
+        Assert.assertEquals(2, rect.y);
+        Assert.assertEquals(3, rect.width);
+        Assert.assertEquals(4, rect.height);
+    }
+}
index b1cd072732edd59b7c6a62f15a27e577c3fbcb7a..ccb5f9b45698a0df34153df1b5ae470c2216cdce 100644 (file)
@@ -6,11 +6,11 @@ package jlecture.junit;
 import java.awt.Dimension;
 import java.awt.Point;
 
+import jlection.robotic.RobotModel;
 import jlecture.simulation.Bar;
 import jlecture.simulation.DimensionRW;
 import jlecture.simulation.ISheet;
 import jlecture.simulation.IWidget;
-import jlecture.simulation.Model;
 import jlecture.simulation.PointRW;
 import jlecture.simulation.RectangleRW;
 import jlecture.simulation.ThingRW;
@@ -23,7 +23,7 @@ import org.junit.Test;
  * @author hm
  *
  */
-public class WidgetTest extends Model {
+public class WidgetTest extends RobotModel {
 
     @Test
     public void testBar() {