From: hama Date: Fri, 10 Oct 2014 22:28:02 +0000 (+0200) Subject: Correct move of Movable, refactoring X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=c6be4e94e3a3634f17752ea7c01f1564e6105bbf;p=robosim Correct move of Movable, refactoring * fix: center object replacement instead of cloning has avoided the move * new: Movable stops if destination is reached * renamed: *SWT to *Swing * new: LineSwing * javadoc corrections --- diff --git a/src/jlection/robotic/RoboSim.java b/src/jlection/robotic/RoboSim.java index c01c1e4..db907c7 100644 --- a/src/jlection/robotic/RoboSim.java +++ b/src/jlection/robotic/RoboSim.java @@ -9,7 +9,8 @@ import java.awt.Point; import javax.swing.SwingUtilities; import jlecture.simulation.Model; -import jlecture.swt.SheetSWT; +import jlecture.simulation.RectangleRW; +import jlecture.swing.SheetSwing; /** * Defines the main routine of the robot simulator. @@ -36,9 +37,9 @@ public class RoboSim { * Initializes the model and the graphical view. */ public void exec() { - Model model = new RobotModel(); - SheetSWT sheet = new SheetSWT(new Point(50, 10), new Dimension(1000, - 800), "Roboterfeld", model); + Model model = new RobotModel(new RectangleRW(0, 0, 1000, 800)); + SheetSwing sheet = new SheetSwing(new Point(50, 10), new Dimension( + 1000, 800), "Roboterfeld", model); model.createModel(); SwingUtilities.invokeLater(new Runnable() { @Override diff --git a/src/jlection/robotic/RobotModel.java b/src/jlection/robotic/RobotModel.java index 56f505e..a91e975 100644 --- a/src/jlection/robotic/RobotModel.java +++ b/src/jlection/robotic/RobotModel.java @@ -8,6 +8,7 @@ import jlecture.simulation.DimensionRW; import jlecture.simulation.Model; import jlecture.simulation.Movable; import jlecture.simulation.PointRW; +import jlecture.simulation.RectangleRW; /** * Implements a robot simulation. @@ -16,6 +17,16 @@ import jlecture.simulation.PointRW; * */ public class RobotModel extends Model { + /** + * Constructor. + * + * @param region + * the visible real world coordinates (a rectangle) + */ + public RobotModel(RectangleRW region) { + super(region); + } + /** * Builds the elements of the simulator. * @@ -39,13 +50,14 @@ public class RobotModel extends Model { destination.x = robot.getWidget().getCenter().x; destination.y = robot.getWidget().getCenter().y; randomDestination(destination); - // 100 seconds for the whole width: - double velocity = 1 / 100.0 * this.region.width - * (0.5 + this.random.nextDouble() * 0.5); + double minSecondsForWidth = 25; + double velocity = 1 / minSecondsForWidth * this.region.width * 0.5 + * (1 + this.random.nextDouble()); robot.setMove(destination, velocity); this.things.add(robot); } this.isReady = true; + log("model completed"); } } diff --git a/src/jlecture/simulation/DimensionRW.java b/src/jlecture/simulation/DimensionRW.java index eee8b88..6c38cfc 100644 --- a/src/jlecture/simulation/DimensionRW.java +++ b/src/jlecture/simulation/DimensionRW.java @@ -37,4 +37,15 @@ public class DimensionRW extends Dimension { this.width = width; this.height = height; } + + /** + * Takes the values from another dimension. + * + * @param source + * the source to clone + */ + public void clone(DimensionRW source) { + this.width = source.width; + this.height = source.height; + } } diff --git a/src/jlecture/simulation/ISheet.java b/src/jlecture/simulation/ISheet.java index 335e896..7f4e0de 100644 --- a/src/jlecture/simulation/ISheet.java +++ b/src/jlecture/simulation/ISheet.java @@ -15,6 +15,9 @@ public interface ISheet { * the center of the rectangle * @param dimension * the width/height of the widget + * @param filled + * true: the rectangle will be filled
+ * otherwise: different colors of frame and area * @param model * the simulator model * @return a X cross widget diff --git a/src/jlecture/simulation/Model.java b/src/jlecture/simulation/Model.java index 1af27cb..9edabf5 100644 --- a/src/jlecture/simulation/Model.java +++ b/src/jlecture/simulation/Model.java @@ -45,17 +45,27 @@ public abstract class Model { /** * The simulator area with real world coordinates. */ - protected RectangleRW region = new RectangleRW(0, 0, 1024, 1024 * 2 / 3); + protected RectangleRW region = null; /** * A pseude random generator for repeatable results. */ protected Random random = new Random(0x4711); + private long sumTime = 0; + + private long count = 0; + + private long max = 0; + /** * Constructor. + * + * @param region + * the visible real world coordinates (a rectangle) */ - public Model() { + public Model(RectangleRW region) { + this.region = region; } /** @@ -96,6 +106,7 @@ public abstract class Model { * Logs a message. * * @param message + * message to log */ public void log(String message) { System.out.println(message); @@ -135,15 +146,28 @@ public abstract class Model { * are necessary for one simulation step. */ public void onTimer() { - if (this.isReady) - for (ThingRW thing : this.things) - if (thing instanceof Movable) { - Movable robot = (Movable) thing; - PointRW center = robot.getWidget().getCenter(); - // moves to the position belonging to the current move: - robot.currentPosition(center, System.currentTimeMillis()); - robot.getWidget().setCenter(center); - } + if (this.isReady) { + long start = System.currentTimeMillis(); + // count++; + // for (int n = 0; n < 1; n++) + { + for (ThingRW thing : this.things) + if (thing instanceof Movable) { + Movable robot = (Movable) thing; + PointRW center = robot.getWidget().getCenter(); + // moves to the position belonging to the current move: + robot.currentPosition(center, start); + robot.getWidget().setCenter(center); + } + } + // long diff = System.currentTimeMillis() - start; + // sumTime += diff; + // if (max < diff) + // max = diff; + // if (count % 1 == 0) + // log(String.format("Count: %d Duration: %f Max: %d", count, + // (double) sumTime / count, max)); + } } /** diff --git a/src/jlecture/simulation/Movable.java b/src/jlecture/simulation/Movable.java index 050ac03..3cd14c6 100644 --- a/src/jlecture/simulation/Movable.java +++ b/src/jlecture/simulation/Movable.java @@ -54,11 +54,10 @@ public class Movable extends ThingRW { * @param point * OUT: the calculated current position * @param currentTime - * < 0: relative to the starttime (negated)
+ * < 0: relative to the starttime (negated)
* otherwise: the current time in milliseconds */ public void currentPosition(PointRW point, long currentTime) { - ; if (this.velocity == 0.0) { point.x = this.widget.getCenter().x; point.y = this.widget.getCenter().y; @@ -71,15 +70,24 @@ public class Movable extends ThingRW { // hypotenuse: // c = sqrt(x_target _ x_start)**2 + (y_target _ y_start)**2) // Theory of intersecting lines: - // x_diff : distance = (x_target - x_start) / c - // y_diff : distance = (y_target - y_start) / c + // x_diff / distance = (x_target - x_start) / c + // y_diff / distance = (y_target - y_start) / c double x_diff = this.destination.x - this.start.x; double y_diff = this.destination.y - this.start.y; double c = Math.sqrt(x_diff * x_diff + y_diff * y_diff); - x_diff = distance * x_diff / c; - y_diff = distance * y_diff / c; - point.x = this.start.x + x_diff; - point.y = this.start.y + y_diff; + double x_diff2 = distance * x_diff / c; + double y_diff2 = distance * y_diff / c; + point.x = this.start.x + x_diff2; + point.y = this.start.y + y_diff2; + // Stop if the destination has reached: + if (Math.abs(x_diff2) > Math.abs(x_diff)) { + point.x = this.destination.x; + this.velocity = 0; + } + if (Math.abs(y_diff2) > Math.abs(y_diff)) { + point.y = this.destination.y; + this.velocity = 0; + } } } @@ -102,8 +110,8 @@ public class Movable extends ThingRW { * real world coordinates */ public void setMove(PointRW destination, double velocity) { - this.start = this.widget.getCenter(); - this.destination = destination; + this.start.clone(this.widget.getCenter()); + this.destination.clone(destination); this.startTime = System.currentTimeMillis(); this.velocity = velocity; } diff --git a/src/jlecture/simulation/PointRW.java b/src/jlecture/simulation/PointRW.java index 7289d22..caf09ca 100644 --- a/src/jlecture/simulation/PointRW.java +++ b/src/jlecture/simulation/PointRW.java @@ -34,4 +34,15 @@ public class PointRW { this.x = x; this.y = y; } + + /** + * Takes the values from another point. + * + * @param source + * the source to clone + */ + public void clone(PointRW source) { + this.x = source.x; + this.y = source.y; + } } diff --git a/src/jlecture/simulation/RectangleRW.java b/src/jlecture/simulation/RectangleRW.java index cb2434a..1e16e2e 100644 --- a/src/jlecture/simulation/RectangleRW.java +++ b/src/jlecture/simulation/RectangleRW.java @@ -47,4 +47,17 @@ public class RectangleRW { this.height = height; } + /** + * Takes the values from another rectangle. + * + * @param source + * the source to clone + */ + public void clone(RectangleRW source) { + this.x = source.x; + this.y = source.y; + this.width = source.width; + this.height = source.height; + } + } diff --git a/src/jlecture/swing/LineSwing.java b/src/jlecture/swing/LineSwing.java new file mode 100644 index 0000000..a7f303c --- /dev/null +++ b/src/jlecture/swing/LineSwing.java @@ -0,0 +1,61 @@ +/** + * + */ +package jlecture.swing; + +import java.awt.Graphics2D; + +import jlecture.simulation.Model; +import jlecture.simulation.PointRW; + +/** + * Implements a line in Swing. + * + * + * @author hm + * + */ +public class LineSwing extends WidgetSwing { + /** + * the first point of the line + */ + private PointRW from; + /** + * the last point of the line + */ + private PointRW to; + + /** + * Constructor. + * + * @param from + * the first point of the line + * @param to + * the last point of the line + * @param model + * the simulator model + */ + public LineSwing(PointRW from, PointRW to, Model model) { + super(from, model); + this.from = from; + this.to = to; + } + + @Override + protected void calculateContour() { + this.contour.x = Math.min(this.from.x, this.to.x); + this.contour.y = Math.min(this.from.y, this.to.y); + this.contour.width = Math.abs(this.from.x - this.to.x); + this.contour.height = Math.abs(this.from.y - this.to.y); + this.model.transform(this.contour, this.contourSwing); + } + + @Override + public void draw(Graphics2D graphics) { + graphics.drawLine(this.contourSwing.x, this.contourSwing.y, + this.contourSwing.x + this.contourSwing.width, this.contourSwing.y + + this.contourSwing.height); + + } + +} diff --git a/src/jlecture/swing/PanelSwing.java b/src/jlecture/swing/PanelSwing.java new file mode 100644 index 0000000..5eecece --- /dev/null +++ b/src/jlecture/swing/PanelSwing.java @@ -0,0 +1,66 @@ +/** + * + */ +package jlecture.swing; + +/** + * @author hm + * + */ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; + +import javax.swing.JPanel; + +import jlecture.simulation.IWidget; +import jlecture.simulation.Model; + +/** + * Implements a panel drawing the simulator model view. + * + * @author hm + * + */ +class PanelSwing extends JPanel { + Model model; + + private static final long serialVersionUID = 1L; + private Color foreground = Color.black; + private Color background = Color.white; + + /** + * Constructor. + * + * @param model + * the simulator model + */ + public PanelSwing(Model model) { + this.model = model; + } + + /** + * Draws the model view. + * + * @param graphics + * the drawing parameter like pencil + */ + private void doDrawing(Graphics graphics) { + Graphics2D g2d = (Graphics2D) graphics; + + g2d.setColor(this.foreground); + g2d.setBackground(this.background); + + for (IWidget item : this.model.getItems()) { + WidgetSwing item2 = (WidgetSwing) item; + item2.draw(g2d); + } + } + + @Override + public void paintComponent(Graphics graphics) { + + super.paintComponent(graphics); + doDrawing(graphics); + } +} diff --git a/src/jlecture/swing/RectangleSwing.java b/src/jlecture/swing/RectangleSwing.java new file mode 100644 index 0000000..f603be0 --- /dev/null +++ b/src/jlecture/swing/RectangleSwing.java @@ -0,0 +1,65 @@ +/** + * + */ +package jlecture.swing; + +import java.awt.Graphics2D; + +import jlecture.simulation.DimensionRW; +import jlecture.simulation.Model; +import jlecture.simulation.PointRW; + +/** + * Implements a rectangle with real world coordinates. + * + * Note: The rectangle has real world coordinates but it will be shown with + * pixel coordinates. The translation is done internally (using the model). + * + * @author hm + * + */ +public class RectangleSwing extends WidgetSwing { + private DimensionRW dimension = null; + + private boolean filled = false; + + /** + * Constructor. + * + * @param center + * the center of the rectangle + * @param dimension + * the width/height of the rectangle + * @param filled + * true: the border has the maximum size
+ * false: distinction of border and inner area + * @param model + * the simulator model + */ + public RectangleSwing(PointRW center, DimensionRW dimension, + boolean filled, Model model) { + super(center, model); + this.dimension = dimension; + this.filled = filled; + calculateContour(); + } + + @Override + protected void calculateContour() { + this.contour.x = this.center.x - this.dimension.width / 2; + this.contour.y = this.center.y - this.dimension.height / 2; + this.contour.width = this.dimension.width; + this.contour.height = this.dimension.height; + this.model.transform(this.contour, this.contourSwing); + } + + @Override + public void draw(Graphics2D graphics) { + if (this.filled) + graphics.fillRect(this.contourSwing.x, this.contourSwing.y, + this.contourSwing.width, this.contourSwing.height); + else + graphics.drawRect(this.contourSwing.x, this.contourSwing.y, + this.contourSwing.width, this.contourSwing.height); + } +} diff --git a/src/jlecture/swing/SheetSwing.java b/src/jlecture/swing/SheetSwing.java new file mode 100644 index 0000000..e040caf --- /dev/null +++ b/src/jlecture/swing/SheetSwing.java @@ -0,0 +1,100 @@ +package jlecture.swing; + +import java.awt.Dimension; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JFrame; +import javax.swing.Timer; + +import jlecture.simulation.DimensionRW; +import jlecture.simulation.ISheet; +import jlecture.simulation.IWidget; +import jlecture.simulation.Model; +import jlecture.simulation.PointRW; + +/** + * Defines a windows driven by Swing. + * + * This window contains a panel with the view of the simulated world. + * + * Note: this class uses pixel coordinates: the maximum x is right, the maximum + * y is at the bottom. + * + * @author hm + * + */ +public class SheetSwing extends JFrame implements ISheet, ActionListener { + private static final long serialVersionUID = 1L; + private Point position = null; + private Dimension dimension = null; + private String title = null; + private Model model; + private Timer timer; + + /** + * Constructor. + * + * @param position + * the top left corner of the window + * @param dimension + * the width and height of the window + * @param title + * the window's title + * @param model + * simulator model + */ + public SheetSwing(Point position, Dimension dimension, String title, + Model model) { + this.position = position; + this.dimension = dimension; + this.title = title; + this.model = model; + this.timer = new Timer(100, this); + this.timer.setInitialDelay(2000); + model.setSheet(this); + initUI(); + } + + @Override + public void actionPerformed(ActionEvent e) { + // if (e.getActionCommand().equals("")) + { + this.model.onTimer(); + repaint(); + } + } + + @Override + public IWidget createRectangle(PointRW center, DimensionRW dimension, + boolean filled, Model model) { + return new RectangleSwing(center, dimension, filled, model); + } + + @Override + public IWidget createXCross(PointRW center, DimensionRW dimension, + Model model) { + return new XCrossSwing(center, dimension, model); + } + + /** + * Initializes the user interface. + */ + private void initUI() { + + setTitle(this.title); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + add(new PanelSwing(this.model)); + + setSize(this.dimension.width, this.dimension.height); + + if (this.position == null) + // build the window in the center: + setLocationRelativeTo(null); + else + setLocation(this.position); + this.timer.start(); + } +} diff --git a/src/jlecture/swing/WidgetSwing.java b/src/jlecture/swing/WidgetSwing.java new file mode 100644 index 0000000..f92b107 --- /dev/null +++ b/src/jlecture/swing/WidgetSwing.java @@ -0,0 +1,107 @@ +/** + * + */ +package jlecture.swing; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +import jlecture.simulation.IWidget; +import jlecture.simulation.Model; +import jlecture.simulation.PointRW; +import jlecture.simulation.RectangleRW; + +/** + * Base class of all drawing items. + * + * The widget is characterized by the center: Though it is very simple to + * calculate the distance of two widgets: the distance of the two centers. + * + * Another important property is the contour: this is the smallest rectangle + * containing all parts of the widget. + * + * @author hm + * + */ +public abstract class WidgetSwing implements IWidget { + /** + * The center of the item in real world coordinates. This simply allows + * calculating distance between 2 items. + */ + protected PointRW center; + /** + * The smallest rectangle containing all parts of the item (in real world + * coordinates). + */ + protected RectangleRW contour = new RectangleRW(0, 0, 0, 0); + /** + * The smallest rectangle containing all parts of the item (in pixel + * coordinates). + */ + protected Rectangle contourSwing = new Rectangle(); + + /** + * Simulator model. + */ + protected Model model; + + /** + * Constructor. + * + * @param center + * the central position of the widget. + * @param model + * simulator model + */ + public WidgetSwing(PointRW center, Model model) { + this.center = center; + this.model = model; + model.getItems().add(this); + } + + /** + * Calculates the smallest rectangle containing all parts of the items. + */ + protected abstract void calculateContour(); + + /** + * Draws the item with the given graphics. + * + * @param graphics + * properties of the pencil a.s.o + */ + public abstract void draw(Graphics2D graphics); + + /** + * @return the center + */ + @Override + public PointRW getCenter() { + return this.center; + } + + /** + * Returns the smallest rectangle containing all parts of the item. + * + * @return the contour + */ + @Override + public RectangleRW getContour() { + return this.contour; + } + + /** + * @return the model + */ + @Override + public Model getModel() { + return this.model; + } + + @Override + public void setCenter(PointRW center) { + this.center.x = center.x; + this.center.y = center.y; + calculateContour(); + } +} diff --git a/src/jlecture/swing/XCrossSwing.java b/src/jlecture/swing/XCrossSwing.java new file mode 100644 index 0000000..b43e598 --- /dev/null +++ b/src/jlecture/swing/XCrossSwing.java @@ -0,0 +1,65 @@ +/** + * + */ +package jlecture.swing; + +import java.awt.Graphics2D; + +import jlecture.simulation.DimensionRW; +import jlecture.simulation.Model; +import jlecture.simulation.PointRW; +import jlecture.simulation.RectangleRW; + +/** + * Implements a cross similar to a 'X' for SWT. These are the diagonals of a + * rectangle. + * + * @author hm + * + */ +public class XCrossSwing extends WidgetSwing { + /** + * The length of the square (contour). + */ + private DimensionRW dimension = null; + private RectangleRW contour = new RectangleRW(0, 0, 0, 0); + + /** + * Constructor. + * + * @param center + * the center of the X cross + * @param dimension + * the width/height + * @param model + * the simulation model + */ + public XCrossSwing(PointRW center, DimensionRW dimension, Model model) { + super(center, model); + this.dimension = dimension; + calculateContour(); + } + + /** + * Calculates the contour of the X cross. + */ + @Override + protected void calculateContour() { + this.contour.x = this.center.x - this.dimension.width / 2; + this.contour.y = this.center.y - this.dimension.height / 2; + this.contour.width = this.dimension.width; + this.contour.height = this.dimension.height; + } + + @Override + public void draw(Graphics2D graphics) { + calculateContour(); + this.model.transform(this.contour, this.contourSwing); + graphics.drawLine(this.contourSwing.x, this.contourSwing.y, + this.contourSwing.x + this.contourSwing.width, this.contourSwing.y + + this.contourSwing.height); + graphics.drawLine(this.contourSwing.x + this.contourSwing.width, + this.contourSwing.y, this.contourSwing.x, this.contourSwing.y + + this.contourSwing.height); + } +} diff --git a/src/jlecture/swt/PanelSWT.java b/src/jlecture/swt/PanelSWT.java deleted file mode 100644 index 9d3680b..0000000 --- a/src/jlecture/swt/PanelSWT.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * - */ -package jlecture.swt; - -/** - * @author hm - * - */ -import java.awt.Color; -import java.awt.Graphics; -import java.awt.Graphics2D; - -import javax.swing.JPanel; - -import jlecture.simulation.IWidget; -import jlecture.simulation.Model; - -/** - * Implements a panel drawing the simulator model view. - * - * @author hm - * - */ -class PanelSWT extends JPanel { - Model model; - - private static final long serialVersionUID = 1L; - private Color foreground = Color.black; - private Color background = Color.white; - - /** - * Constructor. - * - * @param model - * the simulator model - */ - public PanelSWT(Model model) { - this.model = model; - } - - /** - * Draws the model view. - * - * @param graphics - * the drawing parameter like pencil - */ - private void doDrawing(Graphics graphics) { - Graphics2D g2d = (Graphics2D) graphics; - - g2d.setColor(this.foreground); - g2d.setBackground(this.background); - - for (IWidget item : this.model.getItems()) { - WidgetSWT item2 = (WidgetSWT) item; - item2.draw(g2d); - } - } - - @Override - public void paintComponent(Graphics graphics) { - - super.paintComponent(graphics); - doDrawing(graphics); - } -} diff --git a/src/jlecture/swt/RectangleSWT.java b/src/jlecture/swt/RectangleSWT.java deleted file mode 100644 index f75551c..0000000 --- a/src/jlecture/swt/RectangleSWT.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * - */ -package jlecture.swt; - -import java.awt.Graphics2D; -import java.awt.Rectangle; - -import jlecture.simulation.DimensionRW; -import jlecture.simulation.Model; -import jlecture.simulation.PointRW; - -/** - * Implements a rectangle with real world coordinates. - * - * @Note: The rectangle has real world coordinates but it will be shown with - * pixel coordinates. The translation is done internally (using the - * model). - * - * @author hm - * - */ -public class RectangleSWT extends WidgetSWT { - private DimensionRW dimension = null; - - private boolean filled = false; - private Rectangle contourSWT = new Rectangle(); - - /** - * Constructor. - * - * @param center - * the center of the rectangle - * @param dimension - * the width/height of the rectangle - * @param filled - * true: the border has the maximum size
- * false: distinction of border and inner area - * @param model - * the simulator model - */ - public RectangleSWT(PointRW center, DimensionRW dimension, boolean filled, - Model model) { - super(center, model); - this.dimension = dimension; - this.filled = filled; - calculateContour(); - } - - @Override - protected void calculateContour() { - this.contour.x = this.center.x - this.dimension.width / 2; - this.contour.y = this.center.y - this.dimension.height / 2; - this.contour.width = this.dimension.width; - this.contour.height = this.dimension.height; - } - - @Override - public void draw(Graphics2D graphics) { - calculateContour(); - this.model.transform(this.contour, this.contourSWT); - if (this.filled) - graphics.fillRect(this.contourSWT.x, this.contourSWT.y, - this.contourSWT.width, this.contourSWT.height); - else - graphics.drawRect(this.contourSWT.x, this.contourSWT.y, - this.contourSWT.width, this.contourSWT.height); - } -} diff --git a/src/jlecture/swt/SheetSWT.java b/src/jlecture/swt/SheetSWT.java deleted file mode 100644 index d8c9ad9..0000000 --- a/src/jlecture/swt/SheetSWT.java +++ /dev/null @@ -1,98 +0,0 @@ -package jlecture.swt; - -import java.awt.Dimension; -import java.awt.Point; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JFrame; -import javax.swing.Timer; - -import jlecture.simulation.DimensionRW; -import jlecture.simulation.ISheet; -import jlecture.simulation.IWidget; -import jlecture.simulation.Model; -import jlecture.simulation.PointRW; - -/** - * Defines a windows driven by SWT. - * - * This window contains a panel with the view of the simulated world. - * - * @Note: this class uses pixel coordinates: the maximum x is right, the maximum - * y is at the bottom. - * - * @author hm - * - */ -public class SheetSWT extends JFrame implements ISheet, ActionListener { - private static final long serialVersionUID = 1L; - private Point position = null; - private Dimension dimension = null; - private String title = null; - private Model model; - private Timer timer; - - /** - * Constructor. - * - * @param position - * the top left corner of the window - * @param dimension - * the width and height of the window - * @param title - * the window's title - */ - public SheetSWT(Point position, Dimension dimension, String title, - Model model) { - this.position = position; - this.dimension = dimension; - this.title = title; - this.model = model; - this.timer = new Timer(100, this); - this.timer.setInitialDelay(2000); - model.setSheet(this); - initUI(); - } - - @Override - public void actionPerformed(ActionEvent e) { - // if (e.getActionCommand().equals("")) - { - this.model.onTimer(); - repaint(); - } - } - - @Override - public IWidget createRectangle(PointRW center, DimensionRW dimension, - boolean filled, Model model) { - return new RectangleSWT(center, dimension, filled, model); - } - - @Override - public IWidget createXCross(PointRW center, DimensionRW dimension, - Model model) { - return new XCrossSWT(center, dimension, model); - } - - /** - * Initializes the user interface. - */ - private void initUI() { - - setTitle(this.title); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - add(new PanelSWT(this.model)); - - setSize(this.dimension.width, this.dimension.height); - - if (this.position == null) - // build the window in the center: - setLocationRelativeTo(null); - else - setLocation(this.position); - this.timer.start(); - } -} diff --git a/src/jlecture/swt/WidgetSWT.java b/src/jlecture/swt/WidgetSWT.java deleted file mode 100644 index b4b6f43..0000000 --- a/src/jlecture/swt/WidgetSWT.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * - */ -package jlecture.swt; - -import java.awt.Graphics2D; - -import jlecture.simulation.IWidget; -import jlecture.simulation.Model; -import jlecture.simulation.PointRW; -import jlecture.simulation.RectangleRW; - -/** - * Base class of all drawing items. - * - * The widget is characterized by the center: Though it is very simple to - * calculate the distance of two widgets: the distance of the two centers. - * - * Another important property is the contour: this is the smallest rectangle - * containing all parts of the widget. - * - * @author hm - * - */ -public abstract class WidgetSWT implements IWidget { - /** - * The center of the item in real world coordinates. This simply allows - * calculating distance between 2 items. - */ - protected PointRW center; - /** - * The smallest rectangle containing all parts of the item. - */ - protected RectangleRW contour = new RectangleRW(0, 0, 0, 0); - - /** - * Simulator model. - */ - protected Model model; - - /** - * Constructor. - * - * @param center - * the central position of the widget. - */ - public WidgetSWT(PointRW center, Model model) { - this.center = center; - this.model = model; - model.getItems().add(this); - } - - /** - * Calculates the smallest rectangle containing all parts of the items. - */ - protected abstract void calculateContour(); - - /** - * Draws the item with the given graphics. - * - * @param graphics - * properties of the pencil a.s.o - */ - public abstract void draw(Graphics2D graphics); - - /** - * @return the center - */ - @Override - public PointRW getCenter() { - return this.center; - } - - /** - * Returns the smallest rectangle containing all parts of the item. - * - * @return the contour - */ - @Override - public RectangleRW getContour() { - return this.contour; - } - - /** - * @return the model - */ - @Override - public Model getModel() { - return this.model; - } - - @Override - public void setCenter(PointRW center) { - this.center.x = center.x; - this.center.y = center.y; - calculateContour(); - } -} diff --git a/src/jlecture/swt/XCrossSWT.java b/src/jlecture/swt/XCrossSWT.java deleted file mode 100644 index bb3440f..0000000 --- a/src/jlecture/swt/XCrossSWT.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * - */ -package jlecture.swt; - -import java.awt.Graphics2D; -import java.awt.Rectangle; - -import jlecture.simulation.DimensionRW; -import jlecture.simulation.Model; -import jlecture.simulation.PointRW; -import jlecture.simulation.RectangleRW; - -/** - * Implements a cross similar to a 'X' for SWT. These are the diagonals of a - * rectangle. - * - * @author hm - * - */ -public class XCrossSWT extends WidgetSWT { - /** - * The length of the square (contour). - */ - private DimensionRW dimension = null; - private RectangleRW contour = new RectangleRW(0, 0, 0, 0); - private Rectangle contourSWT = new Rectangle(); - - /** - * Constructor. - * - * @param center - * the center of the X cross - * @param dimension - * the width/height - * @param model - * the simulation model - */ - public XCrossSWT(PointRW center, DimensionRW dimension, Model model) { - super(center, model); - this.dimension = dimension; - calculateContour(); - } - - /** - * Calculates the contour of the X cross. - */ - @Override - protected void calculateContour() { - this.contour.x = this.center.x - this.dimension.width / 2; - this.contour.y = this.center.y - this.dimension.height / 2; - this.contour.width = this.dimension.width; - this.contour.height = this.dimension.height; - } - - @Override - public void draw(Graphics2D graphics) { - calculateContour(); - this.model.transform(this.contour, this.contourSWT); - graphics.drawLine(this.contourSWT.x, this.contourSWT.y, - this.contourSWT.x + this.contourSWT.width, this.contourSWT.y - + this.contourSWT.height); - graphics.drawLine(this.contourSWT.x + this.contourSWT.width, - this.contourSWT.y, this.contourSWT.x, this.contourSWT.y - + this.contourSWT.height); - } -} diff --git a/test/jlecture/junit/RobotModelTest.java b/test/jlecture/junit/RobotModelTest.java index 2532b7f..aaba9a2 100644 --- a/test/jlecture/junit/RobotModelTest.java +++ b/test/jlecture/junit/RobotModelTest.java @@ -13,16 +13,20 @@ import jlecture.simulation.Movable; import jlecture.simulation.PointRW; import jlecture.simulation.RectangleRW; import jlecture.simulation.ThingRW; -import jlecture.swt.SheetSWT; +import jlecture.swing.SheetSwing; import org.junit.Assert; import org.junit.Test; public class RobotModelTest extends RobotModel { + public RobotModelTest() { + super(new RectangleRW(0, 0, 1000, 800)); + } + @Test public void testMove() { - ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(1000, 800), - "test", this); + ISheet sheet = new SheetSwing(new Point(0, 0), + new Dimension(1000, 800), "test", this); IWidget rectangle = sheet.createRectangle(new PointRW(100, 100), new DimensionRW(20, 20), true, this); Movable robot = new Movable(rectangle); @@ -62,7 +66,7 @@ public class RobotModelTest extends RobotModel { @Test public void testOccupied() { - ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100), + ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100), "test", this); IWidget rectangle = sheet.createRectangle(new PointRW(100, 100), new DimensionRW(20, 20), true, this); @@ -79,7 +83,7 @@ public class RobotModelTest extends RobotModel { @Test public void testOpenPosition() { - ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100), + ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100), "test", this); IWidget rectangle = sheet.createRectangle(new PointRW(100, 100), new DimensionRW(20, 20), true, this); @@ -93,7 +97,7 @@ public class RobotModelTest extends RobotModel { @Test public void testOpenTransform() { - ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100), + ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100), "test", this); Rectangle rect = new Rectangle(0, 0, 0, 0); transform(new RectangleRW(1, 2, 3, 4), rect); diff --git a/test/jlecture/junit/WidgetTest.java b/test/jlecture/junit/WidgetTest.java index ccb5f9b..31ce8f1 100644 --- a/test/jlecture/junit/WidgetTest.java +++ b/test/jlecture/junit/WidgetTest.java @@ -14,7 +14,7 @@ import jlecture.simulation.IWidget; import jlecture.simulation.PointRW; import jlecture.simulation.RectangleRW; import jlecture.simulation.ThingRW; -import jlecture.swt.SheetSWT; +import jlecture.swing.SheetSwing; import org.junit.Assert; import org.junit.Test; @@ -25,9 +25,13 @@ import org.junit.Test; */ public class WidgetTest extends RobotModel { + public WidgetTest() { + super(new RectangleRW(0, 0, 1000, 800)); + } + @Test public void testBar() { - ISheet sheet = new SheetSWT(new Point(0, 0), new Dimension(200, 100), + ISheet sheet = new SheetSwing(new Point(0, 0), new Dimension(200, 100), "test", this); IWidget rectangle = sheet.createRectangle(new PointRW(100, 50), new DimensionRW(20, 20), true, this);