From 45e97b996caeaf515fadc5c3d133da03e28268dd Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 19 Oct 2014 11:11:08 +0200 Subject: [PATCH] color, oscillation * widgets have selectable colors * correction if a move "jumps" into a barrier * robots have 2 targets and oszillate from one to the other and back again --- src/jlection/robotic/Robot.java | 147 ++++++++++++++++++++++- src/jlection/robotic/RobotModel.java | 45 ++++--- src/jlecture/simulation/Barrier.java | 14 +-- src/jlecture/simulation/IWidget.java | 25 ++++ src/jlecture/simulation/Model.java | 29 +++++ src/jlecture/simulation/Movable.java | 157 +++++++++++++++++++------ src/jlecture/simulation/PointRW.java | 24 +++- src/jlecture/swing/LineSwing.java | 12 +- src/jlecture/swing/RectangleSwing.java | 21 +++- src/jlecture/swing/WidgetSwing.java | 40 +++++-- src/jlecture/swing/XCrossSwing.java | 22 +++- 11 files changed, 455 insertions(+), 81 deletions(-) diff --git a/src/jlection/robotic/Robot.java b/src/jlection/robotic/Robot.java index 0952358..7e329d2 100644 --- a/src/jlection/robotic/Robot.java +++ b/src/jlection/robotic/Robot.java @@ -1,7 +1,10 @@ package jlection.robotic; +import java.awt.Color; + import jlecture.simulation.IWidget; import jlecture.simulation.Movable; +import jlecture.simulation.PointRW; /** * Implements a robot. @@ -13,9 +16,135 @@ import jlecture.simulation.Movable; * */ public class Robot extends Movable { + /** + * Returns the sign of a given value. + * + * @param x + * value to inspect + * @return 0: x == 0
+ * -1: x < 0
+ * 1: x > 0 + */ + static double sign(final double x) { + double rc; + if (x == 0.0) + rc = 0; + else + rc = x < 0 ? -1 : 1; + return rc; + } + + private final double smallStep; + private final double nearBy; + /** + * The robot walks from target1 to target2 and back. + */ + public PointRW target1 = new PointRW(0, 0); + /** + * The robot walks from target1 to target2 and back. + */ + public PointRW target2 = new PointRW(0, 0); + + /** + * One of target1 or target2. + */ + private PointRW target = this.target1; - public Robot(IWidget widget) { + /** + * Constructor. + * + * @param widget + * the widget used for the view + */ + public Robot(final IWidget widget) { super(widget); + this.smallStep = 5 * Math.max(widget.getContour().width, + widget.getContour().height); + this.nearBy = Math.max(widget.getContour().width, + widget.getContour().height); + } + + /** + * Executes the strategy first backward (few steps) then forward (many + * steps). + * + * The next (intermediate) target is one of that four: The likelihood of the + * for targets is equal (0.25) + * + *
+     * Legend: R(obot) T(arget) p: calculated (intermediate) targets
+     * T ------------- p1
+     * |               |
+     * p0 ------------ R-p2
+     *                 |
+     *                 p3
+     * 
+ * + */ + private void forwardFullBackwardSmall() { + final PointRW p = new PointRW(this.target.x, this.target.y); + final PointRW center = this.widget.getCenter(); + int mode = this.model.getRandom().nextInt(4); + if (p.y == center.y && mode == 0) + mode = 1 + this.model.getRandom().nextInt(3); + else if (p.x == center.x && mode == 1) { + mode = this.model.getRandom().nextInt(3); + if (mode == 1) + mode = 3; + } + switch (mode) { + case 0: + p.y = center.y; + break; + case 1: + p.x = center.x; + break; + case 2: + p.y = center.y; + p.x = sign(center.x - this.target.x) * this.smallStep; + break; + default: + p.x = center.x; + p.y = sign(center.y - this.target.y) * this.smallStep; + break; + } + setMove(p, -1); + } + + /** + * @return the nearBy + */ + public double getNearBy() { + return this.nearBy; + } + + /** + * @return the smallStep + */ + public double getSmallStep() { + return this.smallStep; + } + + /** + * @return the target + */ + public PointRW getTarget() { + return this.target; + } + + /** + * @param useTarget1 + * true: target will be set to target1
+ * false: target will be set to target2 + */ + public void setTarget(final boolean useTarget1) { + if (useTarget1) { + this.widget.setColor(Color.blue); + this.target = this.target1; + } else { + this.widget.setColor(Color.red); + this.target = this.target2; + } } /** @@ -27,6 +156,20 @@ public class Robot extends Movable { */ @Override public void simulationStep() { - + if (nearBy(this.target1)) + setTarget(false); + else if (nearBy(this.target2)) + setTarget(true); + if (this.velocity == 0.0) + forwardFullBackwardSmall(); } + +} + +class Stage { + // a temporary target on the way to the final target + public PointRW destination; + // If the target could not reached one try is left. + // If this number of tries are reached the target will be removed + public int tries; } diff --git a/src/jlection/robotic/RobotModel.java b/src/jlection/robotic/RobotModel.java index 8827408..6968c55 100644 --- a/src/jlection/robotic/RobotModel.java +++ b/src/jlection/robotic/RobotModel.java @@ -3,10 +3,11 @@ */ package jlection.robotic; +import java.awt.Color; + import jlecture.simulation.Barrier; import jlecture.simulation.DimensionRW; import jlecture.simulation.Model; -import jlecture.simulation.Movable; import jlecture.simulation.PointRW; import jlecture.simulation.RectangleRW; @@ -36,31 +37,45 @@ public class RobotModel extends Model { @Override public void createModel() { final int count = 1000; - final int width = 4; - this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500, - 100), new DimensionRW(600, 50), false, this))); - this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500, - 400), new DimensionRW(600, 50), false, this))); - this.things.add(new Barrier(this.sheet.createRectangle(new PointRW(500, - 700), new DimensionRW(600, 50), false, this))); + final int width = 3; + Barrier barrier = new Barrier(this.sheet.createRectangle(new PointRW( + 500, + 100), new DimensionRW(600, 100), false, this)); + barrier.getWidget().setColorBackground(Color.gray); + this.things.add(barrier); + barrier = new Barrier(this.sheet.createRectangle(new PointRW(500, 400), + new DimensionRW(600, 100), false, this)); + barrier.getWidget().setColorBackground(Color.gray); + this.things.add(barrier); + barrier = new Barrier(this.sheet.createRectangle(new PointRW(500, 700), + new DimensionRW(600, 100), false, this)); + barrier.getWidget().setColorBackground(Color.gray); + this.things.add(barrier); this.firstIndexMovables = this.things.size(); final PointRW destination = new PointRW(0, 0); for (int ix = 0; ix < count; ix++) { - final Movable movable = new Movable(this.sheet.createRectangle( + final Robot robot = new Robot(this.sheet.createRectangle( new PointRW(500, 500), new DimensionRW(width, width), true, this)); - movable.getWidget().setCenter(openPosition()); - destination.x = movable.getWidget().getCenter().x; - destination.y = movable.getWidget().getCenter().y; + robot.getWidget().setCenter(openPosition()); + destination.x = robot.getWidget().getCenter().x; + destination.y = robot.getWidget().getCenter().y; randomDestination(destination); final double minSecondsForWidth = 25; final double velocity = 1 / minSecondsForWidth * this.region.width - * 0.5 * (1 + this.random.nextDouble()); - movable.setMove(destination, velocity); - this.things.add(movable); + * 0.5 * (1 + this.random.nextDouble()); + robot.setMove(destination, velocity); + robot.target1.x = this.region.x + 0.02 * this.region.width; + robot.target1.y = this.region.y + 0.02 * this.region.height; + robot.target2.x = this.region.x + 0.98 * this.region.width; + robot.target2.y = this.region.y + 0.98 * this.region.height; + robot.setTarget(this.random.nextBoolean()); + this.things.add(robot); } this.isReady = true; + log(String.format("collisions while setting the %d robots: %d", count, + this.openPositionCollisions)); } } diff --git a/src/jlecture/simulation/Barrier.java b/src/jlecture/simulation/Barrier.java index f8f2960..1d21f6b 100644 --- a/src/jlecture/simulation/Barrier.java +++ b/src/jlecture/simulation/Barrier.java @@ -6,8 +6,8 @@ package jlecture.simulation; /** * Implements a stationary element in the model. * - * Barriers are objects which can not be passed traversed. Movables must go - * arround barriers. + * Barriers are objects which can not be entered or passed. Movables must go + * around barriers. * * @author hm * @@ -19,30 +19,30 @@ public class Barrier extends ThingRW { * @param widget * the widget in the model view */ - public Barrier(IWidget widget) { + public Barrier(final IWidget widget) { super(widget); } @Override - public boolean clashes(RectangleRW rectangle) { + public boolean clashes(final RectangleRW rectangle) { boolean rc = false; if (this.widget.getWidgetType() == WidgetType.RECTANGLE || this.widget.getWidgetType() == WidgetType.XCROSS) rc = rectangle.intersects(this.widget.getContour()); else throw new RuntimeException( - "Barrier.clashes(RectangleRW): not implemented yet"); + "Barrier.clashes(RectangleRW): not implemented yet"); return rc; } @Override - public boolean clashes(ThingRW thing) { + public boolean clashes(final ThingRW thing) { boolean rc = false; if (this.widget instanceof RectangleRW) rc = thing.clashes((RectangleRW) this.widget); else throw new RuntimeException( - "Barrier.clashes(ThingRW): not implemented yet"); + "Barrier.clashes(ThingRW): not implemented yet"); return rc; } diff --git a/src/jlecture/simulation/IWidget.java b/src/jlecture/simulation/IWidget.java index 24e44b1..5a9e8e0 100644 --- a/src/jlecture/simulation/IWidget.java +++ b/src/jlecture/simulation/IWidget.java @@ -1,5 +1,7 @@ package jlecture.simulation; +import java.awt.Color; + /** * Defines a graphical item. * @@ -22,6 +24,13 @@ public interface IWidget { */ public RectangleRW getContour(); + /** + * Returns the maximum of width/height of the widget. + * + * @return the maximum of width and height + */ + public double getMaxDimension(); + /** * Returns the model of the widget. * @@ -41,4 +50,20 @@ public interface IWidget { * the center to set */ public void setCenter(PointRW center); + + /** + * Sets the color of the border. + * + * @param color + * the color to set + */ + public void setColor(Color color); + + /** + * Sets the color inside of the border. + * + * @param color + * the background color to set + */ + public void setColorBackground(Color color); } diff --git a/src/jlecture/simulation/Model.java b/src/jlecture/simulation/Model.java index 8c237a8..f50a862 100644 --- a/src/jlecture/simulation/Model.java +++ b/src/jlecture/simulation/Model.java @@ -62,6 +62,11 @@ public abstract class Model { */ protected long now; + /** + * Counts the number of collisions inside openPosition(). + */ + public int openPositionCollisions = 0; + /** * Constructor. * @@ -89,6 +94,7 @@ public abstract class Model { for (int ix = 0; ix < lastIndex; ix++) { final ThingRW thing = this.things.get(ix); if (movable.clashes(thing)) { + movable.moveOutside(thing.getWidget()); movable.setMove(null, 0); if (thing instanceof Movable) ((Movable) thing).setMove(null, 0); @@ -122,6 +128,13 @@ public abstract class Model { return this.now; } + /** + * @return the random + */ + public Random getRandom() { + return this.random; + } + /** * Returns the sheet. * @@ -182,11 +195,14 @@ public abstract class Model { */ public PointRW openPosition() { final PointRW rc = new PointRW(0, 0); + int count = 0; do { + count++; 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); + this.openPositionCollisions += count - 1; return rc; } @@ -246,6 +262,19 @@ public abstract class Model { final PointRW center = movable.getWidget().getCenter(); // moves to the position belonging to the current move: movable.currentPosition(center, this.now); + // outside of the valid region? + if (center.x < this.region.x) + center.x = this.region.x + + movable.getWidget().getMaxDimension() / 2; + else if (center.x > this.region.x + this.region.width) + center.x = this.region.x + this.region.width + - movable.getWidget().getMaxDimension() / 2; + if (center.y < this.region.y) + center.y = this.region.y + + movable.getWidget().getMaxDimension() / 2; + else if (center.y > this.region.y + this.region.height) + center.y = this.region.y + this.region.height + - movable.getWidget().getMaxDimension() / 2; movable.getWidget().setCenter(center); checkCollision(movable, ix); } diff --git a/src/jlecture/simulation/Movable.java b/src/jlecture/simulation/Movable.java index b994297..885a686 100644 --- a/src/jlecture/simulation/Movable.java +++ b/src/jlecture/simulation/Movable.java @@ -20,7 +20,13 @@ public class Movable extends ThingRW { /** * Velocity of the robot in units per second. */ - private double velocity = 0.0; + protected double maxVelocity = 0.0; + + /** + * Velocity of the robot in units per second. This value switch between 0 + * and maxVelocity. + */ + protected double velocity = 0.0; /** * The position of the last direction change. @@ -30,7 +36,7 @@ public class Movable extends ThingRW { /** * The destination of the currrent move. */ - private final PointRW destination = new PointRW(0, 0); + protected final PointRW destination = new PointRW(0, 0); /** * The start time of the current move (in msec). @@ -45,33 +51,33 @@ public class Movable extends ThingRW { * @param widget * the widget in the graphical view */ - public Movable(IWidget widget) { + public Movable(final IWidget widget) { super(widget); this.model = widget.getModel(); } @Override - public boolean clashes(RectangleRW rectangle) { + public boolean clashes(final RectangleRW rectangle) { boolean rc = false; if (this.widget.getWidgetType() == WidgetType.RECTANGLE - || this.widget.getWidgetType() == WidgetType.XCROSS) + || this.widget.getWidgetType() == WidgetType.XCROSS) rc = this.widget.getContour().intersects(rectangle); else throw new RuntimeException( - "Movable.clashes(RectangleRW): not implemented yet"); + "Movable.clashes(RectangleRW): not implemented yet"); ; return rc; } @Override - public boolean clashes(ThingRW thing) { + public boolean clashes(final ThingRW thing) { boolean rc = false; if (this.widget.getWidgetType() == WidgetType.RECTANGLE - || this.widget.getWidgetType() == WidgetType.XCROSS) + || this.widget.getWidgetType() == WidgetType.XCROSS) rc = thing.clashes(this.widget.getContour()); else throw new RuntimeException( - "Movable.clashes(ThingRW): not implemented yet"); + "Movable.clashes(ThingRW): not implemented yet"); return rc; } @@ -84,41 +90,50 @@ public class Movable extends ThingRW { * < 0: relative to the start time (negated)
* otherwise: the current time in milliseconds */ - public void currentPosition(PointRW point, long currentTime) { + public void currentPosition(final PointRW point, long currentTime) { if (this.getVelocity() == 0.0) point.clone(this.widget.getCenter()); else { - if (this.startTime == 0) - this.startTime = this.model.getNow(); - if (currentTime < 0) - currentTime = this.startTime - currentTime; - // v = s / t => s = v * t - final double time = (currentTime - this.startTime) / 1000.0; - final double distance = this.getVelocity() * 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 final double x_diff = this.destination.x - this.start.x; final double y_diff = this.destination.y - this.start.y; - final double c = Math.sqrt(x_diff * x_diff + y_diff * y_diff); - final double x_diff2 = distance * x_diff / c; - final 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.setVelocity(0); - } - if (Math.abs(y_diff2) > Math.abs(y_diff)) { - point.y = this.destination.y; - this.setVelocity(0); + if (x_diff != 0 || y_diff != 0) { + if (this.startTime == 0) + this.startTime = this.model.getNow(); + if (currentTime < 0) + currentTime = this.startTime - currentTime; + // v = s / t => s = v * t + final double time = (currentTime - this.startTime) / 1000.0; + final double distance = this.getVelocity() * 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 + final double c = Math.sqrt(x_diff * x_diff + y_diff * y_diff); + final double x_diff2 = distance * x_diff / c; + final 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.setVelocity(0); + } + if (Math.abs(y_diff2) > Math.abs(y_diff)) { + point.y = this.destination.y; + this.setVelocity(0); + } } } } + /** + * @return the maxVelocity + */ + public double getMaxVelocity() { + return this.maxVelocity; + } + /** * Returns the velocity. * @@ -136,6 +151,68 @@ public class Movable extends ThingRW { return this.widget; } + /** + * Moves the instance outside of a given widget. + * + * If the speed is so large that one step is enough to "jump" inside a + * barrier this method corrects this mistake. + * + *
+     * Before:
+     *             ------
+     * Start ------|-R--|---- Destination
+     *             ------
+     * After:
+     *             ------
+     * Start -----R|----|---- Destination
+     *             ------
+     * 
+ * + * @param widget + * the move must be corrected if the area of this widget is + * entered + */ + public void moveOutside(final IWidget widget) { + final RectangleRW barrier = widget.getContour(); + final PointRW center = this.widget.getCenter(); + if (this.start.x < barrier.x) + // left border: + center.x = barrier.x - this.widget.getMaxDimension(); + else if (this.start.y < barrier.y) + // upper border: + center.y = barrier.y - this.widget.getMaxDimension(); + else if (this.start.x > barrier.x + barrier.width) + // right border: + center.x = barrier.x + barrier.width + + this.widget.getMaxDimension(); + else if (this.start.y > barrier.y + barrier.height) + // lower border: + center.y = barrier.y + barrier.height + + this.widget.getMaxDimension(); + } + + /** + * Tests whether the instance is near by a given point. + * + * @param point + * point to inspect + * @return true: the distance to point is small
+ * false: otherwise + */ + public boolean nearBy(final PointRW point) { + final boolean rc = this.widget.getCenter().nearBy(point, + this.widget.getMaxDimension()); + return rc; + } + + /** + * @param maxVelocity + * the maxVelocity to set + */ + public void setMaxVelocity(final double maxVelocity) { + this.maxVelocity = maxVelocity; + } + /** * Sets the current move. * @@ -147,11 +224,15 @@ public class Movable extends ThingRW { * the velocity in units per second. A unit is defined by the * real world coordinates */ - public void setMove(PointRW destination, double velocity) { + public void setMove(final PointRW destination, double velocity) { this.start.clone(this.widget.getCenter()); if (destination != null) this.destination.clone(destination); this.startTime = this.model.getNow(); + if (velocity > 0) + this.maxVelocity = velocity; + else if (velocity < 0) + velocity = this.maxVelocity; this.setVelocity(velocity); } @@ -161,7 +242,7 @@ public class Movable extends ThingRW { * @param velocity * the velocity to set */ - public void setVelocity(double velocity) { + public void setVelocity(final double velocity) { this.velocity = velocity; } @@ -169,7 +250,7 @@ public class Movable extends ThingRW { * @param widget * the widget to set */ - public void setWidget(IWidget widget) { + public void setWidget(final IWidget widget) { this.widget = widget; } diff --git a/src/jlecture/simulation/PointRW.java b/src/jlecture/simulation/PointRW.java index 7422622..2cfbb55 100644 --- a/src/jlecture/simulation/PointRW.java +++ b/src/jlecture/simulation/PointRW.java @@ -30,7 +30,7 @@ public class PointRW { * @param y * the y coordinate (vertical) */ - public PointRW(double x, double y) { + public PointRW(final double x, final double y) { this.x = x; this.y = y; } @@ -41,8 +41,28 @@ public class PointRW { * @param source * the source to clone */ - public void clone(PointRW source) { + public void clone(final PointRW source) { this.x = source.x; this.y = source.y; } + + /** + * Checks whether a given point is near by the instance. + * + * @param point + * point to test + * @param maxDistance + * near means the distance is lower than this maximum + * @return true: point is nearer than + * maxDistance false: otherwise + * + */ + public boolean nearBy(final PointRW point, final double maxDistance) { + // we use the Pythagoras, but not the square root (too expansive) + final double diffX = point.x - this.x; + final double diffY = point.y - this.y; + final boolean rc = diffX * diffX + diffY * diffY < maxDistance + * maxDistance; + return rc; + } } diff --git a/src/jlecture/swing/LineSwing.java b/src/jlecture/swing/LineSwing.java index 2093879..741e61e 100644 --- a/src/jlecture/swing/LineSwing.java +++ b/src/jlecture/swing/LineSwing.java @@ -3,6 +3,7 @@ */ package jlecture.swing; +import java.awt.Color; import java.awt.Graphics2D; import jlecture.simulation.Model; @@ -36,7 +37,7 @@ public class LineSwing extends WidgetSwing { * @param model * the simulator model */ - public LineSwing(PointRW from, PointRW to, Model model) { + public LineSwing(final PointRW from, final PointRW to, final Model model) { super(from, model); this.from.clone(from); this.to.clone(to); @@ -52,11 +53,18 @@ public class LineSwing extends WidgetSwing { } @Override - public void draw(Graphics2D graphics) { + public void draw(final Graphics2D graphics) { + final Color safeColor = graphics.getColor(); + graphics.setColor(this.color); graphics.drawLine(this.contourSwing.x, this.contourSwing.y, this.contourSwing.x + this.contourSwing.width, this.contourSwing.y + this.contourSwing.height); + graphics.setColor(safeColor); + } + @Override + public double getMaxDimension() { + return 0; } @Override diff --git a/src/jlecture/swing/RectangleSwing.java b/src/jlecture/swing/RectangleSwing.java index 9dfe6e3..59797d2 100644 --- a/src/jlecture/swing/RectangleSwing.java +++ b/src/jlecture/swing/RectangleSwing.java @@ -3,6 +3,7 @@ */ package jlecture.swing; +import java.awt.Color; import java.awt.Graphics2D; import jlecture.simulation.DimensionRW; @@ -21,7 +22,7 @@ import jlecture.simulation.WidgetType; */ public class RectangleSwing extends WidgetSwing { private final DimensionRW dimension = new DimensionRW(0, 0); - + private double maxDimension = 0; private boolean filled = false; /** @@ -37,12 +38,13 @@ public class RectangleSwing extends WidgetSwing { * @param model * the simulator model */ - public RectangleSwing(PointRW center, DimensionRW dimension, - boolean filled, Model model) { + public RectangleSwing(final PointRW center, final DimensionRW dimension, + final boolean filled, final Model model) { super(center, model); this.dimension.clone(dimension); this.filled = filled; calculateContour(); + this.maxDimension = Math.max(this.contour.width, this.contour.height); } @Override @@ -55,13 +57,24 @@ public class RectangleSwing extends WidgetSwing { } @Override - public void draw(Graphics2D graphics) { + public void draw(final Graphics2D graphics) { + final Color safeColor = graphics.getColor(); + final Color safeBackgroundColor = graphics.getBackground(); + graphics.setColor(this.color); + graphics.setBackground(this.backgroundColor); 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); + graphics.setColor(safeColor); + graphics.setBackground(safeBackgroundColor); + } + + @Override + public double getMaxDimension() { + return this.maxDimension; } @Override diff --git a/src/jlecture/swing/WidgetSwing.java b/src/jlecture/swing/WidgetSwing.java index 059c68c..3f5c9ed 100644 --- a/src/jlecture/swing/WidgetSwing.java +++ b/src/jlecture/swing/WidgetSwing.java @@ -3,6 +3,7 @@ */ package jlecture.swing; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.Rectangle; @@ -29,22 +30,31 @@ public abstract class WidgetSwing implements IWidget { * calculating distance between 2 items. */ protected PointRW center = new PointRW(0, 0); + /** * 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(); + /** + * The color of the border. + */ + protected Color color; + /** + * The color inside of the border. + */ + protected Color backgroundColor; /** * Simulator model. */ protected Model model; - static int counter = 0; /** @@ -55,7 +65,7 @@ public abstract class WidgetSwing implements IWidget { * @param model * simulator model */ - public WidgetSwing(PointRW center, Model model) { + public WidgetSwing(final PointRW center, final Model model) { this.center.clone(center); this.model = model; model.getItems().add(this); @@ -101,13 +111,29 @@ public abstract class WidgetSwing implements IWidget { } @Override - public void setCenter(PointRW center) { + public void setCenter(final PointRW center) { WidgetSwing.counter++; this.center.clone(center); calculateContour(); - if (Math.abs(center.y - this.contour.y) > this.contour.height / 2) - System.out.println(String.format("%d: x/y: %d/%d %d/%d:", - WidgetSwing.counter, (int) center.x, (int) center.y, - (int) this.contour.x, (int) this.contour.y)); + } + + /* + * (non-Javadoc) + * + * @see jlecture.simulation.IWidget#setColor(int) + */ + @Override + public void setColor(final Color color) { + this.color = color; + } + + /* + * (non-Javadoc) + * + * @see jlecture.simulation.IWidget#setColorBackground(int) + */ + @Override + public void setColorBackground(final Color color) { + this.backgroundColor = color; } } diff --git a/src/jlecture/swing/XCrossSwing.java b/src/jlecture/swing/XCrossSwing.java index f9ef0f1..2db72e2 100644 --- a/src/jlecture/swing/XCrossSwing.java +++ b/src/jlecture/swing/XCrossSwing.java @@ -3,6 +3,7 @@ */ package jlecture.swing; +import java.awt.Color; import java.awt.Graphics2D; import jlecture.simulation.DimensionRW; @@ -19,6 +20,7 @@ import jlecture.simulation.WidgetType; * */ public class XCrossSwing extends WidgetSwing { + private double maxDimension = 0; /** * The length of the square (contour). */ @@ -35,10 +37,12 @@ public class XCrossSwing extends WidgetSwing { * @param model * the simulation model */ - public XCrossSwing(PointRW center, DimensionRW dimension, Model model) { + public XCrossSwing(final PointRW center, final DimensionRW dimension, + final Model model) { super(center, model); this.dimension = dimension; calculateContour(); + this.maxDimension = Math.max(this.contour.width, this.contour.height); } /** @@ -53,15 +57,25 @@ public class XCrossSwing extends WidgetSwing { } @Override - public void draw(Graphics2D graphics) { + public void draw(final Graphics2D graphics) { calculateContour(); this.model.transform(this.contour, this.contourSwing); + final Color safeColor = graphics.getColor(); + graphics.setColor(this.color); + graphics.setBackground(this.backgroundColor); graphics.drawLine(this.contourSwing.x, this.contourSwing.y, this.contourSwing.x + this.contourSwing.width, this.contourSwing.y - + this.contourSwing.height); + + this.contourSwing.height); graphics.drawLine(this.contourSwing.x + this.contourSwing.width, this.contourSwing.y, this.contourSwing.x, this.contourSwing.y - + this.contourSwing.height); + + this.contourSwing.height); + graphics.setColor(safeColor); + } + + @Override + public double getMaxDimension() { + // TODO Auto-generated method stub + return this.maxDimension; } @Override -- 2.39.5