--- /dev/null
+/**
+ *
+ */
+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);
+ }
+ });
+
+ }
+
+}
--- /dev/null
+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);
+ }
+
+}
--- /dev/null
+/**
+ *
+ */
+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;
+ }
+
+}
* @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.
/**
* 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.
* @return the list of items
*/
public List<IWidget> getItems() {
- return widgets;
+ return this.widgets;
}
/**
* @return the sheet
*/
public ISheet getSheet() {
- return sheet;
+ return this.sheet;
}
/**
* @return the widgets
*/
public ArrayList<IWidget> getWidgets() {
- return widgets;
+ return this.widgets;
}
/**
*/
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;
}
* 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());
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;
}
* 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;
}
}
--- /dev/null
+/**
+ *
+ */
+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;
+ }
+}
+++ /dev/null
-/**
- *
- */
-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;
- }
-}
+++ /dev/null
-/**
- *
- */
-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);
- }
- });
-
- }
-
-}
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);
}
}
* @author hm
*
*/
-public class RectangleSWT extends Widget {
+public class RectangleSWT extends WidgetSWT {
private DimensionRW dimension = null;
private boolean filled = false;
@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);
}
}
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();
}
public void actionPerformed(ActionEvent e) {
// if (e.getActionCommand().equals(""))
{
- model.onTimer();
+ this.model.onTimer();
repaint();
}
}
@Override
public IWidget createXCross(PointRW center, DimensionRW dimension,
Model model) {
- return new XCross(center, dimension, model);
+ return new XCrossSWT(center, dimension, model);
}
/**
*/
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();
}
}
+++ /dev/null
-/**
- *
- */
-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();
- }
-}
--- /dev/null
+/**
+ *
+ */
+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();
+ }
+}
+++ /dev/null
-/**
- *
- */
-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);
- }
-}
--- /dev/null
+/**
+ *
+ */
+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);
+ }
+}
+++ /dev/null
-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);
- }
-}
--- /dev/null
+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);
+ }
+}
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;
* @author hm
*
*/
-public class WidgetTest extends Model {
+public class WidgetTest extends RobotModel {
@Test
public void testBar() {