* Constructor
*
* @param width
+ * horizontal dimension
* @param height
+ * vertical dimension
*/
public DimensionRW(double width, double height) {
this.width = width;
package jlecture.simulation;
+/**
+ * Defines a drawing area.
+ *
+ * @author hm
+ *
+ */
public interface ISheet {
/**
/**
* Returns the center of the widget.
*
- * @return
+ * @return the center of the widget
*/
public PointRW getCenter();
/**
* Implements the model of the simulator.
*
+ * The model contains robots and bars in an area with real world coordinates.
+ * Another component is a drawing area to visualize the robots and bars.
+ *
+ * The model "know" the (modelled) real world, e.g. the position of the bars and
+ * robots.
+ *
+ * The model executes the simulation: the method onTimer() will be called
+ * periodically and does the simulator steps, e.g. it calculates the new
+ * position of the robots.
+ *
+ *
* @author hm
*
*/
/**
* 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(
}
/**
- * Will be called every 100 millisec.
+ * Executes one simulator step.
+ *
+ * Will be called every 100 milliseconds and calculates the changes which
+ * are necessary for one simulation step.
*/
public void onTimer() {
if (isReady)
/**
* Implements a position definition (x and y) with real world coordinates.
*
+ * The real world coordinate system is like mathematical coordinate systems: the
+ * maximum x is right, the maximum y is at the top.
+ *
* @author hm
*
*/
* Constructor.
*
* @param x
+ * the x coordinate (horizontal)
* @param y
+ * the y coordinate (vertical)
*/
public PointRW(double x, double y) {
this.x = x;
* Constructor.
*
* @param x
+ * the x coordinate (horizontal) of the left lower corner
* @param y
+ * the y coordinate (vertical) of the left lower corner
* @param width
+ * the horizontal dimension of the rectangle
* @param height
+ * the vertical dimension of the rectangle
*/
public RectangleRW(double x, double y, double width, double height) {
this.x = x;
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
*
*/
}
/**
- * @return the widget
+ * @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();
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();
}
/**
- * Executes the simulation.
+ * Initializes the model and the graphical view.
*/
public void exec() {
Model model = new Model();
private void doDrawing(Graphics graphics) {
Graphics2D g2d = (Graphics2D) graphics;
- g2d.setColor(this.foreground);
- g2d.setBackground(this.background);
+ g2d.setColor(foreground);
+ g2d.setBackground(background);
- for (IWidget item : this.model.getItems()) {
+ for (IWidget item : model.getItems()) {
Widget item2 = (Widget) item;
item2.draw(g2d);
}
/**
* 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
*
*/
@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;
+ 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();
- this.model.transform(this.contour, this.contourSWT);
- if (this.filled)
- graphics.fillRect(this.contourSWT.x, this.contourSWT.y,
- this.contourSWT.width, this.contourSWT.height);
+ model.transform(contour, contourSWT);
+ if (filled)
+ graphics.fillRect(contourSWT.x, contourSWT.y,
+ contourSWT.width, contourSWT.height);
else
- graphics.drawRect(this.contourSWT.x, this.contourSWT.y,
- this.contourSWT.width, this.contourSWT.height);
+ graphics.drawRect(contourSWT.x, contourSWT.y,
+ contourSWT.width, contourSWT.height);
}
}
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;
- Point position = null;
- Dimension dimension = null;
- String title = null;
- Model model;
- Timer timer;
+ private Point position = null;
+ private Dimension dimension = null;
+ private String title = null;
+ private Model model;
+ private Timer timer;
/**
* Constructor.
this.dimension = dimension;
this.title = title;
this.model = model;
- this.timer = new Timer(100, this);
- this.timer.setInitialDelay(2000);
+ timer = new Timer(100, this);
+ timer.setInitialDelay(2000);
model.setSheet(this);
initUI();
}
public void actionPerformed(ActionEvent e) {
// if (e.getActionCommand().equals(""))
{
- this.model.onTimer();
+ model.onTimer();
repaint();
}
}
*/
private void initUI() {
- setTitle(this.title);
+ setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- add(new PanelSWT(this.model));
+ add(new PanelSWT(model));
- setSize(this.dimension.width, this.dimension.height);
+ setSize(dimension.width, dimension.height);
- if (this.position == null)
+ if (position == null)
// build the window in the center:
setLocationRelativeTo(null);
else
- setLocation(this.position);
- this.timer.start();
+ setLocation(position);
+ timer.start();
}
}
import jlecture.simulation.RectangleRW;
/**
- * Base class of all 2 dimensional drawing items.
+ * 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
*
/**
* Constructor.
*
- * @param sheet
- * the drawing sheet
+ * @param center
+ * the central position of the widget.
*/
public Widget(PointRW center, Model model) {
this.center = center;
*/
@Override
public PointRW getCenter() {
- return this.center;
+ return center;
}
/**
*/
@Override
public RectangleRW getContour() {
- return this.contour;
+ return contour;
}
/**
*/
@Override
public Model getModel() {
- return this.model;
+ return model;
}
- /**
- * @param center
- * the center to set
- */
@Override
public void setCenter(PointRW center) {
this.center.x = center.x;
/**
* Implements a cross similar to a 'X' for SWT. These are the diagonals of a
- * square.
+ * rectangle.
*
* @author hm
*
*/
@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;
+ 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();
- 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);
+ 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);
}
}