]> gitweb.hamatoma.de Git - robosim/commitdiff
Correct move of Movable, refactoring
authorhama <hama@siduction.net>
Fri, 10 Oct 2014 22:28:02 +0000 (00:28 +0200)
committerhama <hama@siduction.net>
Fri, 10 Oct 2014 22:39:20 +0000 (00:39 +0200)
* fix: center object replacement instead of cloning has avoided the move
* new: Movable stops if destination is reached
* renamed: *SWT to *Swing
* new: LineSwing
* javadoc corrections

21 files changed:
src/jlection/robotic/RoboSim.java
src/jlection/robotic/RobotModel.java
src/jlecture/simulation/DimensionRW.java
src/jlecture/simulation/ISheet.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 [new file with mode: 0644]
src/jlecture/swing/PanelSwing.java [new file with mode: 0644]
src/jlecture/swing/RectangleSwing.java [new file with mode: 0644]
src/jlecture/swing/SheetSwing.java [new file with mode: 0644]
src/jlecture/swing/WidgetSwing.java [new file with mode: 0644]
src/jlecture/swing/XCrossSwing.java [new file with mode: 0644]
src/jlecture/swt/PanelSWT.java [deleted file]
src/jlecture/swt/RectangleSWT.java [deleted file]
src/jlecture/swt/SheetSWT.java [deleted file]
src/jlecture/swt/WidgetSWT.java [deleted file]
src/jlecture/swt/XCrossSWT.java [deleted file]
test/jlecture/junit/RobotModelTest.java
test/jlecture/junit/WidgetTest.java

index c01c1e40abb1b7cb04431b07b2a8a720a43ec313..db907c73e7bebe6f1debd7c88863ee3b76d81fb8 100644 (file)
@@ -9,7 +9,8 @@ import java.awt.Point;
 import javax.swing.SwingUtilities;
 
 import jlecture.simulation.Model;
-import jlecture.swt.SheetSWT;
+import jlecture.simulation.RectangleRW;
+import jlecture.swing.SheetSwing;
 
 /**
  * Defines the main routine of the robot simulator.
@@ -36,9 +37,9 @@ public class RoboSim {
      * 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 model = new RobotModel(new RectangleRW(0, 0, 1000, 800));
+        SheetSwing sheet = new SheetSwing(new Point(50, 10), new Dimension(
+            1000, 800), "Roboterfeld", model);
         model.createModel();
         SwingUtilities.invokeLater(new Runnable() {
             @Override
index 56f505e44a624da4bdc42ff8afdc6db63915b8db..a91e97541b89ebbb9779fdb3b6510c63b697ad70 100644 (file)
@@ -8,6 +8,7 @@ import jlecture.simulation.DimensionRW;
 import jlecture.simulation.Model;
 import jlecture.simulation.Movable;
 import jlecture.simulation.PointRW;
+import jlecture.simulation.RectangleRW;
 
 /**
  * Implements a robot simulation.
@@ -16,6 +17,16 @@ import jlecture.simulation.PointRW;
  *
  */
 public class RobotModel extends Model {
+    /**
+     * Constructor.
+     *
+     * @param region
+     *            the visible real world coordinates (a rectangle)
+     */
+    public RobotModel(RectangleRW region) {
+        super(region);
+    }
+
     /**
      * Builds the elements of the simulator.
      *
@@ -39,13 +50,14 @@ public class RobotModel extends Model {
             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);
+            double minSecondsForWidth = 25;
+            double velocity = 1 / minSecondsForWidth * this.region.width * 0.5
+                * (1 + this.random.nextDouble());
             robot.setMove(destination, velocity);
             this.things.add(robot);
         }
         this.isReady = true;
+        log("model completed");
     }
 
 }
index eee8b88eeb6dd5f37dfc5e232ff8a0a89ce29282..6c38cfcf909a3ac4b87f369137a736d6a416f8e2 100644 (file)
@@ -37,4 +37,15 @@ public class DimensionRW extends Dimension {
         this.width = width;
         this.height = height;
     }
+
+    /**
+     * Takes the values from another dimension.
+     *
+     * @param source
+     *            the source to clone
+     */
+    public void clone(DimensionRW source) {
+        this.width = source.width;
+        this.height = source.height;
+    }
 }
index 335e8967ac4159583b3e5041e9f825ede08ff7c7..7f4e0def5bb647ad1554c174b05f9be10aad64e7 100644 (file)
@@ -15,6 +15,9 @@ public interface ISheet {
      *            the center of the rectangle
      * @param dimension
      *            the width/height of the widget
+     * @param filled
+     *            <code>true</code>: the rectangle will be filled<br>
+     *            otherwise: different colors of frame and area
      * @param model
      *            the simulator model
      * @return a X cross widget
index 1af27cb0be9f3c0aa283246d044f64456d9369fb..9edabf5a3bf79d4e43f3d47f5b9810cde931e240 100644 (file)
@@ -45,17 +45,27 @@ public abstract class Model {
     /**
      * The simulator area with real world coordinates.
      */
-    protected RectangleRW region = new RectangleRW(0, 0, 1024, 1024 * 2 / 3);
+    protected RectangleRW region = null;
 
     /**
      * A pseude random generator for repeatable results.
      */
     protected Random random = new Random(0x4711);
 
+    private long sumTime = 0;
+
+    private long count = 0;
+
+    private long max = 0;
+
     /**
      * Constructor.
+     *
+     * @param region
+     *            the visible real world coordinates (a rectangle)
      */
-    public Model() {
+    public Model(RectangleRW region) {
+        this.region = region;
     }
 
     /**
@@ -96,6 +106,7 @@ public abstract class Model {
      * Logs a message.
      *
      * @param message
+     *            message to log
      */
     public void log(String message) {
         System.out.println(message);
@@ -135,15 +146,28 @@ public abstract class Model {
      * are necessary for one simulation step.
      */
     public void onTimer() {
-        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());
-                    robot.getWidget().setCenter(center);
-                }
+        if (this.isReady) {
+            long start = System.currentTimeMillis();
+            // count++;
+            // for (int n = 0; n < 1; n++)
+            {
+                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, start);
+                        robot.getWidget().setCenter(center);
+                    }
+            }
+            // long diff = System.currentTimeMillis() - start;
+            // sumTime += diff;
+            // if (max < diff)
+            // max = diff;
+            // if (count % 1 == 0)
+            // log(String.format("Count: %d Duration: %f Max: %d", count,
+            // (double) sumTime / count, max));
+        }
     }
 
     /**
index 050ac03c47d4fd7d4cb20d28df3e48b6ae6aea7b..3cd14c640f30134d3e66ad60657af9d764a84149 100644 (file)
@@ -54,11 +54,10 @@ public class Movable extends ThingRW {
      * @param point
      *            OUT: the calculated current position
      * @param currentTime
-     *            < 0: relative to the starttime (negated)<br>
+     *            &lt; 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;
@@ -71,15 +70,24 @@ public class Movable extends ThingRW {
             // 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
+            // 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;
+            double x_diff2 = distance * x_diff / c;
+            double y_diff2 = distance * y_diff / c;
+            point.x = this.start.x + x_diff2;
+            point.y = this.start.y + y_diff2;
+            // Stop if the destination has reached:
+            if (Math.abs(x_diff2) > Math.abs(x_diff)) {
+                point.x = this.destination.x;
+                this.velocity = 0;
+            }
+            if (Math.abs(y_diff2) > Math.abs(y_diff)) {
+                point.y = this.destination.y;
+                this.velocity = 0;
+            }
         }
     }
 
@@ -102,8 +110,8 @@ public class Movable extends ThingRW {
      *            real world coordinates
      */
     public void setMove(PointRW destination, double velocity) {
-        this.start = this.widget.getCenter();
-        this.destination = destination;
+        this.start.clone(this.widget.getCenter());
+        this.destination.clone(destination);
         this.startTime = System.currentTimeMillis();
         this.velocity = velocity;
     }
index 7289d223e351b149f698c9f8b68e362bb23cf824..caf09ca4e6546169a9d9c25a5eb06f3c5a28575e 100644 (file)
@@ -34,4 +34,15 @@ public class PointRW {
         this.x = x;
         this.y = y;
     }
+
+    /**
+     * Takes the values from another point.
+     *
+     * @param source
+     *            the source to clone
+     */
+    public void clone(PointRW source) {
+        this.x = source.x;
+        this.y = source.y;
+    }
 }
index cb2434a365c7064df5e35cf7d3f4329701210ffe..1e16e2ebe14772773274aa6cc236db315e7c4a94 100644 (file)
@@ -47,4 +47,17 @@ public class RectangleRW {
         this.height = height;
     }
 
+    /**
+     * Takes the values from another rectangle.
+     *
+     * @param source
+     *            the source to clone
+     */
+    public void clone(RectangleRW source) {
+        this.x = source.x;
+        this.y = source.y;
+        this.width = source.width;
+        this.height = source.height;
+    }
+
 }
diff --git a/src/jlecture/swing/LineSwing.java b/src/jlecture/swing/LineSwing.java
new file mode 100644 (file)
index 0000000..a7f303c
--- /dev/null
@@ -0,0 +1,61 @@
+/**
+ *
+ */
+package jlecture.swing;
+
+import java.awt.Graphics2D;
+
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+
+/**
+ * Implements a line in Swing.
+ *
+ *
+ * @author hm
+ *
+ */
+public class LineSwing extends WidgetSwing {
+    /**
+     * the first point of the line
+     */
+    private PointRW from;
+    /**
+     * the last point of the line
+     */
+    private PointRW to;
+
+    /**
+     * Constructor.
+     *
+     * @param from
+     *            the first point of the line
+     * @param to
+     *            the last point of the line
+     * @param model
+     *            the simulator model
+     */
+    public LineSwing(PointRW from, PointRW to, Model model) {
+        super(from, model);
+        this.from = from;
+        this.to = to;
+    }
+
+    @Override
+    protected void calculateContour() {
+        this.contour.x = Math.min(this.from.x, this.to.x);
+        this.contour.y = Math.min(this.from.y, this.to.y);
+        this.contour.width = Math.abs(this.from.x - this.to.x);
+        this.contour.height = Math.abs(this.from.y - this.to.y);
+        this.model.transform(this.contour, this.contourSwing);
+    }
+
+    @Override
+    public void draw(Graphics2D graphics) {
+        graphics.drawLine(this.contourSwing.x, this.contourSwing.y,
+            this.contourSwing.x + this.contourSwing.width, this.contourSwing.y
+            + this.contourSwing.height);
+
+    }
+
+}
diff --git a/src/jlecture/swing/PanelSwing.java b/src/jlecture/swing/PanelSwing.java
new file mode 100644 (file)
index 0000000..5eecece
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ *
+ */
+package jlecture.swing;
+
+/**
+ * @author hm
+ *
+ */
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+
+import javax.swing.JPanel;
+
+import jlecture.simulation.IWidget;
+import jlecture.simulation.Model;
+
+/**
+ * Implements a panel drawing the simulator model view.
+ *
+ * @author hm
+ *
+ */
+class PanelSwing extends JPanel {
+    Model model;
+
+    private static final long serialVersionUID = 1L;
+    private Color foreground = Color.black;
+    private Color background = Color.white;
+
+    /**
+     * Constructor.
+     *
+     * @param model
+     *            the simulator model
+     */
+    public PanelSwing(Model model) {
+        this.model = model;
+    }
+
+    /**
+     * Draws the model view.
+     *
+     * @param graphics
+     *            the drawing parameter like pencil
+     */
+    private void doDrawing(Graphics graphics) {
+        Graphics2D g2d = (Graphics2D) graphics;
+
+        g2d.setColor(this.foreground);
+        g2d.setBackground(this.background);
+
+        for (IWidget item : this.model.getItems()) {
+            WidgetSwing item2 = (WidgetSwing) item;
+            item2.draw(g2d);
+        }
+    }
+
+    @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
new file mode 100644 (file)
index 0000000..f603be0
--- /dev/null
@@ -0,0 +1,65 @@
+/**
+ *
+ */
+package jlecture.swing;
+
+import java.awt.Graphics2D;
+
+import jlecture.simulation.DimensionRW;
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+
+/**
+ * Implements a rectangle with real world coordinates.
+ *
+ * Note: The rectangle has real world coordinates but it will be shown with
+ * pixel coordinates. The translation is done internally (using the model).
+ *
+ * @author hm
+ *
+ */
+public class RectangleSwing extends WidgetSwing {
+    private DimensionRW dimension = null;
+
+    private boolean filled = false;
+
+    /**
+     * Constructor.
+     *
+     * @param center
+     *            the center of the rectangle
+     * @param dimension
+     *            the width/height of the rectangle
+     * @param filled
+     *            true: the border has the maximum size<br>
+     *            false: distinction of border and inner area
+     * @param model
+     *            the simulator model
+     */
+    public RectangleSwing(PointRW center, DimensionRW dimension,
+        boolean filled, Model model) {
+        super(center, model);
+        this.dimension = dimension;
+        this.filled = filled;
+        calculateContour();
+    }
+
+    @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;
+        this.model.transform(this.contour, this.contourSwing);
+    }
+
+    @Override
+    public void draw(Graphics2D graphics) {
+        if (this.filled)
+            graphics.fillRect(this.contourSwing.x, this.contourSwing.y,
+                this.contourSwing.width, this.contourSwing.height);
+        else
+            graphics.drawRect(this.contourSwing.x, this.contourSwing.y,
+                this.contourSwing.width, this.contourSwing.height);
+    }
+}
diff --git a/src/jlecture/swing/SheetSwing.java b/src/jlecture/swing/SheetSwing.java
new file mode 100644 (file)
index 0000000..e040caf
--- /dev/null
@@ -0,0 +1,100 @@
+package jlecture.swing;
+
+import java.awt.Dimension;
+import java.awt.Point;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JFrame;
+import javax.swing.Timer;
+
+import jlecture.simulation.DimensionRW;
+import jlecture.simulation.ISheet;
+import jlecture.simulation.IWidget;
+import jlecture.simulation.Model;
+import jlecture.simulation.PointRW;
+
+/**
+ * Defines a windows driven by Swing.
+ *
+ * This window contains a panel with the view of the simulated world.
+ *
+ * Note: this class uses pixel coordinates: the maximum x is right, the maximum
+ * y is at the bottom.
+ *
+ * @author hm
+ *
+ */
+public class SheetSwing extends JFrame implements ISheet, ActionListener {
+    private static final long serialVersionUID = 1L;
+    private Point position = null;
+    private Dimension dimension = null;
+    private String title = null;
+    private Model model;
+    private Timer timer;
+
+    /**
+     * Constructor.
+     *
+     * @param position
+     *            the top left corner of the window
+     * @param dimension
+     *            the width and height of the window
+     * @param title
+     *            the window's title
+     * @param model
+     *            simulator model
+     */
+    public SheetSwing(Point position, Dimension dimension, String title,
+        Model model) {
+        this.position = position;
+        this.dimension = dimension;
+        this.title = title;
+        this.model = model;
+        this.timer = new Timer(100, this);
+        this.timer.setInitialDelay(2000);
+        model.setSheet(this);
+        initUI();
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        // if (e.getActionCommand().equals(""))
+        {
+            this.model.onTimer();
+            repaint();
+        }
+    }
+
+    @Override
+    public IWidget createRectangle(PointRW center, DimensionRW dimension,
+        boolean filled, Model model) {
+        return new RectangleSwing(center, dimension, filled, model);
+    }
+
+    @Override
+    public IWidget createXCross(PointRW center, DimensionRW dimension,
+        Model model) {
+        return new XCrossSwing(center, dimension, model);
+    }
+
+    /**
+     * Initializes the user interface.
+     */
+    private void initUI() {
+
+        setTitle(this.title);
+        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        add(new PanelSwing(this.model));
+
+        setSize(this.dimension.width, this.dimension.height);
+
+        if (this.position == null)
+            // build the window in the center:
+            setLocationRelativeTo(null);
+        else
+            setLocation(this.position);
+        this.timer.start();
+    }
+}
diff --git a/src/jlecture/swing/WidgetSwing.java b/src/jlecture/swing/WidgetSwing.java
new file mode 100644 (file)
index 0000000..f92b107
--- /dev/null
@@ -0,0 +1,107 @@
+/**
+ *
+ */
+package jlecture.swing;
+
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
+
+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 WidgetSwing 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 (in real world
+     * coordinates).
+     */
+    protected RectangleRW contour = new RectangleRW(0, 0, 0, 0);
+    /**
+     * The smallest rectangle containing all parts of the item (in pixel
+     * coordinates).
+     */
+    protected Rectangle contourSwing = new Rectangle();
+
+    /**
+     * Simulator model.
+     */
+    protected Model model;
+
+    /**
+     * Constructor.
+     *
+     * @param center
+     *            the central position of the widget.
+     * @param model
+     *            simulator model
+     */
+    public WidgetSwing(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/swing/XCrossSwing.java b/src/jlecture/swing/XCrossSwing.java
new file mode 100644 (file)
index 0000000..b43e598
--- /dev/null
@@ -0,0 +1,65 @@
+/**
+ *
+ */
+package jlecture.swing;
+
+import java.awt.Graphics2D;
+
+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 XCrossSwing extends WidgetSwing {
+    /**
+     * The length of the square (contour).
+     */
+    private DimensionRW dimension = null;
+    private RectangleRW contour = new RectangleRW(0, 0, 0, 0);
+
+    /**
+     * Constructor.
+     *
+     * @param center
+     *            the center of the X cross
+     * @param dimension
+     *            the width/height
+     * @param model
+     *            the simulation model
+     */
+    public XCrossSwing(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.contourSwing);
+        graphics.drawLine(this.contourSwing.x, this.contourSwing.y,
+            this.contourSwing.x + this.contourSwing.width, this.contourSwing.y
+            + this.contourSwing.height);
+        graphics.drawLine(this.contourSwing.x + this.contourSwing.width,
+            this.contourSwing.y, this.contourSwing.x, this.contourSwing.y
+            + this.contourSwing.height);
+    }
+}
diff --git a/src/jlecture/swt/PanelSWT.java b/src/jlecture/swt/PanelSWT.java
deleted file mode 100644 (file)
index 9d3680b..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- *
- */
-package jlecture.swt;
-
-/**
- * @author hm
- *
- */
-import java.awt.Color;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-
-import javax.swing.JPanel;
-
-import jlecture.simulation.IWidget;
-import jlecture.simulation.Model;
-
-/**
- * Implements a panel drawing the simulator model view.
- *
- * @author hm
- *
- */
-class PanelSWT extends JPanel {
-    Model model;
-
-    private static final long serialVersionUID = 1L;
-    private Color foreground = Color.black;
-    private Color background = Color.white;
-
-    /**
-     * Constructor.
-     *
-     * @param model
-     *            the simulator model
-     */
-    public PanelSWT(Model model) {
-        this.model = model;
-    }
-
-    /**
-     * Draws the model view.
-     *
-     * @param graphics
-     *            the drawing parameter like pencil
-     */
-    private void doDrawing(Graphics graphics) {
-        Graphics2D g2d = (Graphics2D) graphics;
-
-        g2d.setColor(this.foreground);
-        g2d.setBackground(this.background);
-
-        for (IWidget item : this.model.getItems()) {
-            WidgetSWT item2 = (WidgetSWT) item;
-            item2.draw(g2d);
-        }
-    }
-
-    @Override
-    public void paintComponent(Graphics graphics) {
-
-        super.paintComponent(graphics);
-        doDrawing(graphics);
-    }
-}
diff --git a/src/jlecture/swt/RectangleSWT.java b/src/jlecture/swt/RectangleSWT.java
deleted file mode 100644 (file)
index f75551c..0000000
+++ /dev/null
@@ -1,69 +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;
-
-/**
- * Implements a rectangle with real world coordinates.
- *
- * @Note: The rectangle has real world coordinates but it will be shown with
- *        pixel coordinates. The translation is done internally (using the
- *        model).
- *
- * @author hm
- *
- */
-public class RectangleSWT extends WidgetSWT {
-    private DimensionRW dimension = null;
-
-    private boolean filled = false;
-    private Rectangle contourSWT = new Rectangle();
-
-    /**
-     * Constructor.
-     *
-     * @param center
-     *            the center of the rectangle
-     * @param dimension
-     *            the width/height of the rectangle
-     * @param filled
-     *            true: the border has the maximum size<br>
-     *            false: distinction of border and inner area
-     * @param model
-     *            the simulator model
-     */
-    public RectangleSWT(PointRW center, DimensionRW dimension, boolean filled,
-        Model model) {
-        super(center, model);
-        this.dimension = dimension;
-        this.filled = filled;
-        calculateContour();
-    }
-
-    @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);
-        if (this.filled)
-            graphics.fillRect(this.contourSWT.x, this.contourSWT.y,
-                this.contourSWT.width, this.contourSWT.height);
-        else
-            graphics.drawRect(this.contourSWT.x, this.contourSWT.y,
-                this.contourSWT.width, this.contourSWT.height);
-    }
-}
diff --git a/src/jlecture/swt/SheetSWT.java b/src/jlecture/swt/SheetSWT.java
deleted file mode 100644 (file)
index d8c9ad9..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-package jlecture.swt;
-
-import java.awt.Dimension;
-import java.awt.Point;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-
-import javax.swing.JFrame;
-import javax.swing.Timer;
-
-import jlecture.simulation.DimensionRW;
-import jlecture.simulation.ISheet;
-import jlecture.simulation.IWidget;
-import jlecture.simulation.Model;
-import jlecture.simulation.PointRW;
-
-/**
- * Defines a windows driven by SWT.
- *
- * This window contains a panel with the view of the simulated world.
- *
- * @Note: this class uses pixel coordinates: the maximum x is right, the maximum
- *        y is at the bottom.
- *
- * @author hm
- *
- */
-public class SheetSWT extends JFrame implements ISheet, ActionListener {
-    private static final long serialVersionUID = 1L;
-    private Point position = null;
-    private Dimension dimension = null;
-    private String title = null;
-    private Model model;
-    private Timer timer;
-
-    /**
-     * Constructor.
-     *
-     * @param position
-     *            the top left corner of the window
-     * @param dimension
-     *            the width and height of the window
-     * @param title
-     *            the window's title
-     */
-    public SheetSWT(Point position, Dimension dimension, String title,
-        Model model) {
-        this.position = position;
-        this.dimension = dimension;
-        this.title = title;
-        this.model = model;
-        this.timer = new Timer(100, this);
-        this.timer.setInitialDelay(2000);
-        model.setSheet(this);
-        initUI();
-    }
-
-    @Override
-    public void actionPerformed(ActionEvent e) {
-        // if (e.getActionCommand().equals(""))
-        {
-            this.model.onTimer();
-            repaint();
-        }
-    }
-
-    @Override
-    public IWidget createRectangle(PointRW center, DimensionRW dimension,
-        boolean filled, Model model) {
-        return new RectangleSWT(center, dimension, filled, model);
-    }
-
-    @Override
-    public IWidget createXCross(PointRW center, DimensionRW dimension,
-        Model model) {
-        return new XCrossSWT(center, dimension, model);
-    }
-
-    /**
-     * Initializes the user interface.
-     */
-    private void initUI() {
-
-        setTitle(this.title);
-        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-        add(new PanelSWT(this.model));
-
-        setSize(this.dimension.width, this.dimension.height);
-
-        if (this.position == null)
-            // build the window in the center:
-            setLocationRelativeTo(null);
-        else
-            setLocation(this.position);
-        this.timer.start();
-    }
-}
diff --git a/src/jlecture/swt/WidgetSWT.java b/src/jlecture/swt/WidgetSWT.java
deleted file mode 100644 (file)
index b4b6f43..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 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/XCrossSWT.java b/src/jlecture/swt/XCrossSWT.java
deleted file mode 100644 (file)
index bb3440f..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 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);
-    }
-}
index 2532b7f81bb4aaf6c2ea2aa001fbebee2f782fe1..aaba9a26f1c1f5c2dd265af647d6e8e639a4ec34 100644 (file)
@@ -13,16 +13,20 @@ import jlecture.simulation.Movable;
 import jlecture.simulation.PointRW;
 import jlecture.simulation.RectangleRW;
 import jlecture.simulation.ThingRW;
-import jlecture.swt.SheetSWT;
+import jlecture.swing.SheetSwing;
 
 import org.junit.Assert;
 import org.junit.Test;
 
 public class RobotModelTest extends RobotModel {
+    public RobotModelTest() {
+        super(new RectangleRW(0, 0, 1000, 800));
+    }
+
     @Test
     public void testMove() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(1000, 800),
-            "test", this);
+        ISheet sheet = new SheetSwing(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);
@@ -62,7 +66,7 @@ public class RobotModelTest extends RobotModel {
 
     @Test
     public void testOccupied() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+        ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100),
             "test", this);
         IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
             new DimensionRW(20, 20), true, this);
@@ -79,7 +83,7 @@ public class RobotModelTest extends RobotModel {
 
     @Test
     public void testOpenPosition() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+        ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100),
             "test", this);
         IWidget rectangle = sheet.createRectangle(new PointRW(100, 100),
             new DimensionRW(20, 20), true, this);
@@ -93,7 +97,7 @@ public class RobotModelTest extends RobotModel {
 
     @Test
     public void testOpenTransform() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+        ISheet sheet = new SheetSwing(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);
index ccb5f9b45698a0df34153df1b5ae470c2216cdce..31ce8f1d471b6e803f1fd74abda3171ce9ba0161 100644 (file)
@@ -14,7 +14,7 @@ import jlecture.simulation.IWidget;
 import jlecture.simulation.PointRW;
 import jlecture.simulation.RectangleRW;
 import jlecture.simulation.ThingRW;
-import jlecture.swt.SheetSWT;
+import jlecture.swing.SheetSwing;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -25,9 +25,13 @@ import org.junit.Test;
  */
 public class WidgetTest extends RobotModel {
 
+    public WidgetTest() {
+        super(new RectangleRW(0, 0, 1000, 800));
+    }
+
     @Test
     public void testBar() {
-        ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100),
+        ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100),
             "test", this);
         IWidget rectangle = sheet.createRectangle(new PointRW(100, 50),
             new DimensionRW(20, 20), true, this);