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.
* 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
import jlecture.simulation.Model;
import jlecture.simulation.Movable;
import jlecture.simulation.PointRW;
+import jlecture.simulation.RectangleRW;
/**
* Implements a robot simulation.
*
*/
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.
*
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");
}
}
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;
+ }
}
* 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
/**
* 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;
}
/**
* Logs a message.
*
* @param message
+ * message to log
*/
public void log(String message) {
System.out.println(message);
* 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));
+ }
}
/**
* @param point
* OUT: the calculated current position
* @param currentTime
- * < 0: relative to the starttime (negated)<br>
+ * < 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;
// 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;
+ }
}
}
* 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;
}
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;
+ }
}
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;
+ }
+
}
--- /dev/null
+/**
+ *
+ */
+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);
+
+ }
+
+}
--- /dev/null
+/**
+ *
+ */
+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);
+ }
+}
--- /dev/null
+/**
+ *
+ */
+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);
+ }
+}
--- /dev/null
+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();
+ }
+}
--- /dev/null
+/**
+ *
+ */
+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();
+ }
+}
--- /dev/null
+/**
+ *
+ */
+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);
+ }
+}
+++ /dev/null
-/**
- *
- */
-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);
- }
-}
+++ /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;
-
-/**
- * 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);
- }
-}
+++ /dev/null
-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();
- }
-}
+++ /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 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);
- }
-}
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);
@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);
@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);
@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);
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 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);