From 31a9d7f9d6e8579fb052496131d777a1ef441927 Mon Sep 17 00:00:00 2001 From: hama Date: Fri, 10 Oct 2014 00:31:00 +0200 Subject: [PATCH] Refactoring: dividing Robot/RobotModel from the Movable/Model * 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 --- .../robotic/RoboSim.java} | 11 ++- src/jlection/robotic/Robot.java | 21 +++++ src/jlection/robotic/RobotModel.java | 51 ++++++++++ src/jlecture/simulation/Model.java | 92 +++++++++--------- .../simulation/{Robot.java => Movable.java} | 42 +++++---- src/jlecture/swt/PanelSWT.java | 8 +- src/jlecture/swt/RectangleSWT.java | 22 ++--- src/jlecture/swt/SheetSWT.java | 20 ++-- .../swt/{Widget.java => WidgetSWT.java} | 10 +- .../swt/{XCross.java => XCrossSWT.java} | 26 ++--- .../{ModelTest.java => RobotModelTest.java} | 94 +++++++++---------- test/jlecture/junit/WidgetTest.java | 4 +- 12 files changed, 236 insertions(+), 165 deletions(-) rename src/{jlecture/simulation/Simulator.java => jlection/robotic/RoboSim.java} (79%) create mode 100644 src/jlection/robotic/Robot.java create mode 100644 src/jlection/robotic/RobotModel.java rename src/jlecture/simulation/{Robot.java => Movable.java} (69%) rename src/jlecture/swt/{Widget.java => WidgetSWT.java} (91%) rename src/jlecture/swt/{XCross.java => XCrossSWT.java} (59%) rename test/jlecture/junit/{ModelTest.java => RobotModelTest.java} (91%) diff --git a/src/jlecture/simulation/Simulator.java b/src/jlection/robotic/RoboSim.java similarity index 79% rename from src/jlecture/simulation/Simulator.java rename to src/jlection/robotic/RoboSim.java index 62788e0..c01c1e4 100644 --- a/src/jlecture/simulation/Simulator.java +++ b/src/jlection/robotic/RoboSim.java @@ -1,17 +1,18 @@ /** * */ -package jlecture.simulation; +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 simulator. + * Defines the main routine of the robot simulator. * * Initializes the model and the graphical view and start the simulation (done * by the model). @@ -19,7 +20,7 @@ import jlecture.swt.SheetSWT; * @author hm * */ -public class Simulator { +public class RoboSim { /** * The main routine. * @@ -27,7 +28,7 @@ public class Simulator { * the program arguments */ public static void main(String[] args) { - Simulator simulator = new Simulator(); + RoboSim simulator = new RoboSim(); simulator.exec(); } @@ -35,7 +36,7 @@ public class Simulator { * Initializes the model and the graphical view. */ public void exec() { - Model model = new Model(); + Model model = new RobotModel(); SheetSWT sheet = new SheetSWT(new Point(50, 10), new Dimension(1000, 800), "Roboterfeld", model); model.createModel(); diff --git a/src/jlection/robotic/Robot.java b/src/jlection/robotic/Robot.java new file mode 100644 index 0000000..f3bd9c5 --- /dev/null +++ b/src/jlection/robotic/Robot.java @@ -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 index 0000000..56f505e --- /dev/null +++ b/src/jlection/robotic/RobotModel.java @@ -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; + } + +} diff --git a/src/jlecture/simulation/Model.java b/src/jlecture/simulation/Model.java index d16017a..1af27cb 100644 --- a/src/jlecture/simulation/Model.java +++ b/src/jlecture/simulation/Model.java @@ -25,14 +25,32 @@ import java.util.Random; * @author hm * */ -public class Model { - private boolean isReady = false; +public abstract class Model { + /** + * true: the model is fully initialized. + */ + protected boolean isReady = false; + /** + * The list of widgets which should be drawn. + */ protected ArrayList widgets = new ArrayList(); + /** + * The list of things which are relevant to the simulation. + */ protected ArrayList things = new ArrayList(); + /** + * 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 + * things. 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 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 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/Robot.java b/src/jlecture/simulation/Movable.java similarity index 69% rename from src/jlecture/simulation/Robot.java rename to src/jlecture/simulation/Movable.java index 00deab3..050ac03 100644 --- a/src/jlecture/simulation/Robot.java +++ b/src/jlecture/simulation/Movable.java @@ -4,17 +4,19 @@ package jlecture.simulation; /** - * Implements a robot. + * Implements a movable thing. * - * A robot is a thing which can move. The move is defined by a start position, a - * destination position and a velocity. + * 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 Robot extends ThingRW { +public class Movable extends ThingRW { /** * Velocity of the robot in units per second. */ @@ -33,7 +35,7 @@ public class Robot extends ThingRW { * The start time of the current move (in msec). */ private long startTime = 0L; - private Model model = null; + protected Model model = null; /** * Constructor. @@ -41,9 +43,9 @@ public class Robot extends ThingRW { * @param widget * the widget in the graphical view */ - public Robot(IWidget widget) { + public Movable(IWidget widget) { super(widget); - model = widget.getModel(); + this.model = widget.getModel(); } /** @@ -57,27 +59,27 @@ public class Robot extends ThingRW { */ public void currentPosition(PointRW point, long currentTime) { ; - if (velocity == 0.0) { - point.x = widget.getCenter().x; - point.y = widget.getCenter().y; + if (this.velocity == 0.0) { + point.x = this.widget.getCenter().x; + point.y = this.widget.getCenter().y; } else { if (currentTime < 0) - currentTime = startTime - currentTime; + currentTime = this.startTime - currentTime; // v = s / t => s = v * t - double time = (currentTime - startTime) / 1000.0; - double distance = velocity * time; + 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 = destination.x - start.x; - double y_diff = destination.y - start.y; + 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 = start.x + x_diff; - point.y = start.y + y_diff; + point.x = this.start.x + x_diff; + point.y = this.start.y + y_diff; } } @@ -85,7 +87,7 @@ public class Robot extends ThingRW { * @return the widget of the robot */ public IWidget getWidget() { - return widget; + return this.widget; } /** @@ -100,9 +102,9 @@ public class Robot extends ThingRW { * real world coordinates */ public void setMove(PointRW destination, double velocity) { - start = widget.getCenter(); + this.start = this.widget.getCenter(); this.destination = destination; - startTime = System.currentTimeMillis(); + this.startTime = System.currentTimeMillis(); this.velocity = velocity; } diff --git a/src/jlecture/swt/PanelSWT.java b/src/jlecture/swt/PanelSWT.java index ce2f30b..9d3680b 100644 --- a/src/jlecture/swt/PanelSWT.java +++ b/src/jlecture/swt/PanelSWT.java @@ -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); } } diff --git a/src/jlecture/swt/RectangleSWT.java b/src/jlecture/swt/RectangleSWT.java index 19bf6a7..f75551c 100644 --- a/src/jlecture/swt/RectangleSWT.java +++ b/src/jlecture/swt/RectangleSWT.java @@ -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); } } diff --git a/src/jlecture/swt/SheetSWT.java b/src/jlecture/swt/SheetSWT.java index b76fab6..d8c9ad9 100644 --- a/src/jlecture/swt/SheetSWT.java +++ b/src/jlecture/swt/SheetSWT.java @@ -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/WidgetSWT.java similarity index 91% rename from src/jlecture/swt/Widget.java rename to src/jlecture/swt/WidgetSWT.java index 5201112..b4b6f43 100644 --- a/src/jlecture/swt/Widget.java +++ b/src/jlecture/swt/WidgetSWT.java @@ -22,7 +22,7 @@ import jlecture.simulation.RectangleRW; * @author hm * */ -public abstract class Widget implements IWidget { +public abstract class WidgetSWT implements IWidget { /** * The center of the item in real world coordinates. This simply allows * calculating distance between 2 items. @@ -44,7 +44,7 @@ public abstract class Widget implements IWidget { * @param center * the central position of the widget. */ - public Widget(PointRW center, Model model) { + public WidgetSWT(PointRW center, Model model) { this.center = center; this.model = model; model.getItems().add(this); @@ -68,7 +68,7 @@ public abstract class Widget implements IWidget { */ @Override public PointRW getCenter() { - return center; + return this.center; } /** @@ -78,7 +78,7 @@ public abstract class Widget implements IWidget { */ @Override public RectangleRW getContour() { - return contour; + return this.contour; } /** @@ -86,7 +86,7 @@ public abstract class Widget implements IWidget { */ @Override public Model getModel() { - return model; + return this.model; } @Override diff --git a/src/jlecture/swt/XCross.java b/src/jlecture/swt/XCrossSWT.java similarity index 59% rename from src/jlecture/swt/XCross.java rename to src/jlecture/swt/XCrossSWT.java index 9a34943..bb3440f 100644 --- a/src/jlecture/swt/XCross.java +++ b/src/jlecture/swt/XCrossSWT.java @@ -18,7 +18,7 @@ import jlecture.simulation.RectangleRW; * @author hm * */ -public class XCross extends Widget { +public class XCrossSWT extends WidgetSWT { /** * The length of the square (contour). */ @@ -36,7 +36,7 @@ public class XCross extends Widget { * @param model * the simulation model */ - public XCross(PointRW center, DimensionRW dimension, Model model) { + public XCrossSWT(PointRW center, DimensionRW dimension, Model model) { super(center, model); this.dimension = dimension; calculateContour(); @@ -47,21 +47,21 @@ public class XCross 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); - 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); + 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/RobotModelTest.java similarity index 91% rename from test/jlecture/junit/ModelTest.java rename to test/jlecture/junit/RobotModelTest.java index a25fc3b..2532b7f 100644 --- a/test/jlecture/junit/ModelTest.java +++ b/test/jlecture/junit/RobotModelTest.java @@ -4,71 +4,28 @@ 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.Model; +import jlecture.simulation.Movable; 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); - } - +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); - Robot robot = new Robot(rectangle); + Movable robot = new Movable(rectangle); robot.setMove(new PointRW(0, 100), 20.0); PointRW point = new PointRW(0, 0); robot.currentPosition(point, -100); @@ -102,4 +59,47 @@ public class ModelTest extends Model { 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); + } } diff --git a/test/jlecture/junit/WidgetTest.java b/test/jlecture/junit/WidgetTest.java index b1cd072..ccb5f9b 100644 --- a/test/jlecture/junit/WidgetTest.java +++ b/test/jlecture/junit/WidgetTest.java @@ -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() { -- 2.39.5