From 1795e03337d57a0de06d33889b30f3c4e1fb4e0d Mon Sep 17 00:00:00 2001 From: astei Date: Tue, 13 Apr 2010 21:27:35 +0000 Subject: [PATCH] Halt rename of classes: resolve a conflict git-svn-id: https://desert.svn.sourceforge.net/svnroot/desert@23 cbff3641-ea5c-438d-a1bd-c9f42921e016 --- src/org/sourceforge/astei/desert/DesertApplet.java | 77 ++++++ .../{ => astei}/desert/DesertElement.java | 12 +- .../astei/desert/DesertPowderElement.java | 44 ++++ .../astei/desert/DesertWaterElement.java | 44 ++++ src/org/sourceforge/astei/desert/DrawingBoard.java | 165 +++++++++++++ src/org/sourceforge/astei/desert/Particle.java | 187 +++++++++++++++ .../sourceforge/astei/desert/ParticleUpdater.java | 257 +++++++++++++++++++++ src/org/sourceforge/desert/DesertElement.java | 10 +- 8 files changed, 785 insertions(+), 11 deletions(-) create mode 100644 src/org/sourceforge/astei/desert/DesertApplet.java copy src/org/sourceforge/{ => astei}/desert/DesertElement.java (89%) create mode 100644 src/org/sourceforge/astei/desert/DesertPowderElement.java create mode 100644 src/org/sourceforge/astei/desert/DesertWaterElement.java create mode 100644 src/org/sourceforge/astei/desert/DrawingBoard.java create mode 100644 src/org/sourceforge/astei/desert/Particle.java create mode 100644 src/org/sourceforge/astei/desert/ParticleUpdater.java diff --git a/src/org/sourceforge/astei/desert/DesertApplet.java b/src/org/sourceforge/astei/desert/DesertApplet.java new file mode 100644 index 0000000..8253e05 --- /dev/null +++ b/src/org/sourceforge/astei/desert/DesertApplet.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.applet.Applet; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.SwingUtilities; +import javax.swing.Timer; + +/** + * + * @author andrew + * @author codistmonk (modifications since 2010-04-13) + */ +public class DesertApplet extends Applet implements ActionListener { + + private final DrawingBoard drawingBoard; + + private final Timer updater; + + public DesertApplet() { + this.drawingBoard = new DrawingBoard(); + this.updater = new Timer(40, this); + } + + @Override + public final void init() { + this.setLayout(new BorderLayout()); + this.add(this.drawingBoard, BorderLayout.CENTER); + + this.updater.start(); + } + + @Override + public final void actionPerformed(final ActionEvent event) { + checkAWT(); + + this.drawingBoard.update(this.updater.getDelay() / 1000.0); + this.drawingBoard.repaint(); + } + + /** + * + * @throws IllegalStateException if this method is not executed by the AWT Event Thread + */ + private static final void checkAWT() { + if (!SwingUtilities.isEventDispatchThread()) { + throw new IllegalStateException("This method must be executed by the AWT Event Thread"); + } + } + +} \ No newline at end of file diff --git a/src/org/sourceforge/desert/DesertElement.java b/src/org/sourceforge/astei/desert/DesertElement.java similarity index 89% copy from src/org/sourceforge/desert/DesertElement.java copy to src/org/sourceforge/astei/desert/DesertElement.java index c66eb6e..934d77a 100644 --- a/src/org/sourceforge/desert/DesertElement.java +++ b/src/org/sourceforge/astei/desert/DesertElement.java @@ -23,7 +23,7 @@ * OTHER DEALINGS IN THE SOFTWARE. * */ -package org.sourceforge.desert; +package org.sourceforge.astei.desert; import java.awt.Color; import java.awt.Button; @@ -41,7 +41,7 @@ public class DesertElement { * @param newColor * @return nothing */ - String setColor(Color newColor) { + public String setColor(Color newColor) { ourColor = newColor; return ""; } @@ -50,7 +50,7 @@ public class DesertElement { * * @return Color from the Element */ - Color getColor() { + public Color getColor() { return ourColor; } /** @@ -59,7 +59,7 @@ public class DesertElement { * @param newElementName * @return new element name */ - String setElementName(String newElementName) { + public String setElementName(String newElementName) { elementName = newElementName; return ""; } @@ -68,7 +68,7 @@ public class DesertElement { * * @return element name as a String */ - String getElementName() { + public String getElementName() { return elementName; } /** @@ -76,7 +76,7 @@ public class DesertElement { * * @return a java.awt.Button for the element */ - Button createButtonForElement() { + public Button createButtonForElement() { Button tmp = new Button(elementName); tmp.setForeground(ourColor); return tmp; diff --git a/src/org/sourceforge/astei/desert/DesertPowderElement.java b/src/org/sourceforge/astei/desert/DesertPowderElement.java new file mode 100644 index 0000000..4384c71 --- /dev/null +++ b/src/org/sourceforge/astei/desert/DesertPowderElement.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.awt.Color; + +/** + * + * @author andrew + */ +public class DesertPowderElement { + DesertElement ele; + public DesertPowderElement() { + ele.setElementName("Powder"); + ele.setColor(new Color(255, 255, 153)); + } + + public DesertElement get() { + return ele; + } +} diff --git a/src/org/sourceforge/astei/desert/DesertWaterElement.java b/src/org/sourceforge/astei/desert/DesertWaterElement.java new file mode 100644 index 0000000..8aaf64e --- /dev/null +++ b/src/org/sourceforge/astei/desert/DesertWaterElement.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.awt.Color; + +/** + * + * @author andrew + */ +public class DesertWaterElement { + DesertElement ele; + public DesertWaterElement() { + ele.setElementName("Water"); + ele.setColor(Color.blue); + } + + public DesertElement get() { + return ele; + } +} diff --git a/src/org/sourceforge/astei/desert/DrawingBoard.java b/src/org/sourceforge/astei/desert/DrawingBoard.java new file mode 100644 index 0000000..62741b3 --- /dev/null +++ b/src/org/sourceforge/astei/desert/DrawingBoard.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.awt.Canvas; +import java.awt.Graphics; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author codistmonk (creation 2010-04-13) + */ +public class DrawingBoard extends Canvas { + + private final List particles; + + private final ParticleUpdater particleUpdater; + + private BufferedImage buffer; + + public DrawingBoard() { + this.particles = new ArrayList(); + this.particleUpdater = new ParticleUpdater(this.particles); + + this.addComponentListener(this.new ResizeHandler()); + + final MouseHandler mouseHandler = this.new MouseHandler(); + + this.addMouseListener(mouseHandler); + this.addMouseMotionListener(mouseHandler); + } + + @Override + public void paint(final Graphics g) { + this.clearBuffer(); + + this.drawParticlesToBuffer(); + + this.drawBuffer(g); + } + + /** + * + * @param deltaTime + *
Should not be null + *
Range: ]0 .. Double.POSITIVE_INFINITY[ + */ + public final void update(final Double deltaTime) { + this.particleUpdater.update(deltaTime); + } + + /** + * + * @param x + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param y + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param speedX + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param speedY + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + final void addParticle(final Double x, final Double y, final Double speedX, final Double speedY) { + this.particles.add(new Particle(x, y, speedX, speedY)); + } + + final void resizeBuffer() { + this.buffer = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_3BYTE_BGR); + this.particleUpdater.setBounds(this.getBounds()); + } + + private final void clearBuffer() { + this.buffer.createGraphics().clearRect(0, 0, this.buffer.getWidth(), this.buffer.getHeight()); + } + + private final void drawParticlesToBuffer() { + final Graphics bufferGraphics = this.buffer.createGraphics(); + + for (final Particle particle : this.particles) { + particle.draw(bufferGraphics); + } + } + + /** + * + * @param graphics + *
Should not be null + */ + private final void drawBuffer(final Graphics graphics) { + graphics.drawImage(this.buffer, 0, 0, this); + } + + /** + * + * @author codistmonk (creation 2010-04-13) + */ + private final class ResizeHandler extends ComponentAdapter { + + @Override + public final void componentResized(final ComponentEvent event) { + DrawingBoard.this.resizeBuffer(); + } + + } + + /** + * + * @author codistmonk (creation 2010-04-13) + */ + private final class MouseHandler extends MouseAdapter { + + @Override + public final void mouseDragged(final MouseEvent event) { + this.addRandomParticle((double) event.getX(), (double) event.getY()); + } + + @Override + public final void mousePressed(final MouseEvent event) { + this.addRandomParticle((double) event.getX(), (double) event.getY()); + } + + private final void addRandomParticle(final Double x, final Double y) { + final Double orientation = Math.random() * 2.0 * Math.PI; + + DrawingBoard.this.addParticle(x, y, Math.cos(orientation) * INITIAL_SPEED, Math.sin(orientation) * INITIAL_SPEED); + } + + } + + private static final Double INITIAL_SPEED = 4.0; + +} diff --git a/src/org/sourceforge/astei/desert/Particle.java b/src/org/sourceforge/astei/desert/Particle.java new file mode 100644 index 0000000..78ccca9 --- /dev/null +++ b/src/org/sourceforge/astei/desert/Particle.java @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.awt.Graphics; + +/** + * TODO it may be interesting to use a physics library + * @author codistmonk (creation 2010-04-13) + */ +public final class Particle { + + private Double radius; + + private Double x; + + private Double y; + + private Double speedX; + + private Double speedY; + + /** + * + * @param x + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param y + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param speedX + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param speedY + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public Particle(final Double x, final Double y, final Double speedX, final Double speedY) { + this.radius = DEFAULT_RADIUS; + this.x = x; + this.y = y; + this.speedX = speedX; + this.speedY = speedY; + } + + /** + * + * @param graphics + *
Should not be null + */ + public final void draw(final Graphics graphics) { + final int x = (int) Math.round(this.getX()); + final int y = (int) Math.round(this.getY()); + graphics.drawLine(x, y, x, y); + } + + @Override + public final String toString() { + return "(" + this.getX() + ", " + this.getY() + ", " + this.getSpeedX() + ", " + this.getSpeedY() + ")"; + } + + /** + * + * @return + *
A non-null value + *
Range: [0.0 .. Double.POSITIVE_INFINITY[ + */ + public final Double getRadius() { + return radius; + } + + /** + * + * @param radius + *
Should not be null + *
Range: [0.0 .. Double.POSITIVE_INFINITY[ + */ + public final void setRadius(final Double radius) { + this.radius = radius; + } + + /** + * + * @return + *
A non-null value + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final Double getSpeedX() { + return this.speedX; + } + + /** + * + * @param speedX + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final void setSpeedX(final Double speedX) { + this.speedX = speedX; + } + + /** + * + * @return + *
A non-null value + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final Double getSpeedY() { + return speedY; + } + + /** + * + * @param speedY + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final void setSpeedY(final Double speedY) { + this.speedY = speedY; + } + + /** + * + * @return + *
A non-null value + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final Double getX() { + return this.x; + } + + /** + * + * @param x + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final void setX(final Double x) { + this.x = x; + } + + /** + * + * @return + *
A non-null value + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final Double getY() { + return this.y; + } + + /** + * + * @param y + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + public final void setY(final Double y) { + this.y = y; + } + + public static final Double DEFAULT_RADIUS = 0.4; + +} diff --git a/src/org/sourceforge/astei/desert/ParticleUpdater.java b/src/org/sourceforge/astei/desert/ParticleUpdater.java new file mode 100644 index 0000000..c5ab7ef --- /dev/null +++ b/src/org/sourceforge/astei/desert/ParticleUpdater.java @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2010 The Desert team + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.sourceforge.astei.desert; + +import java.awt.Rectangle; +import java.util.Iterator; +import java.util.List; + +import static java.lang.Math.*; + +/** + * + * @author codistmonk (creation 2010-04-13) + */ +public class ParticleUpdater { + + private final List particles; + + private Double gravity; + + private Rectangle bounds; + + /** + * + * @param particles + *
Should not be null + *
Reference parameter + */ + public ParticleUpdater(final List particles) { + this.particles = particles; + this.setGravity(DEFAULT_GRAVITY); + } + + /** + * Updates the positions and speeds of all the particles. + * @param deltaTime + *
Should not be null + *
Range: ]0 .. Double.POSITIVE_INFINITY[ + */ + public final void update(final Double deltaTime) { + for (final Particle particle : this.getParticles()) { + // Effect of gravity on speed + particle.setSpeedY(particle.getSpeedY() + this.getGravity() * deltaTime); + + this.moveUntilCollision(particle, deltaTime); + + // Make sure that the particle doesn't get out if bounds are set + this.constrain(particle); + } + } + + /** + * + * @return + *
A possibly null value + *
A reference + */ + public Rectangle getBounds() { + return this.bounds; + } + + /** + * + * @param bounds + *
May be null + *
Reference parameter + */ + public void setBounds(final Rectangle bounds) { + this.bounds = bounds; + } + + /** + * + * @return + *
A non-null value + *
A reference + */ + public List getParticles() { + return this.particles; + } + + /** + * + * @return + *
A non-null value + */ + public final Double getGravity() { + return this.gravity; + } + + /** + * + * @param gravity + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.Positive_INFINITY[ + */ + public final void setGravity(final Double gravity) { + this.gravity = gravity; + } + + /** + * Moves the particle according to its speed and deltaTime, but stop just before colliding + * with another particle. + * When a particle is stopped before a collision, is speed is set to 0.0. + * @param particle + *
Should not be null + * @param deltaTime + *
Should not be null + *
Range: ]0 .. Double.POSITIVE_INFINITY[ + */ + private final void moveUntilCollision(final Particle particle, final Double deltaTime) { + Double deltaX = particle.getSpeedX() * deltaTime; + Double deltaY = particle.getSpeedY() * deltaTime; + final Double pixelCount = Math.ceil(Math.max(deltaX, deltaY)); + + if (pixelCount != 0.0) { + // Instead of moving the particle to its destination in one step, + // we are going to move it in as many steps as there are pixels + deltaX /= pixelCount; + deltaY /= pixelCount; + + Boolean collision = false; + + for (Integer i = 0; i < pixelCount && !collision; ++i) { + move(particle, deltaX, deltaY); + + // The goal is to go as far as possible without overlapping another particle + // Only the particles that have already been moved are tested for collision + final Iterator particleIterator = this.getParticles().iterator(); + Particle otherParticle = next(particleIterator); + + while (particle != otherParticle && otherParticle != null && !(collision = collision(particle, otherParticle))) { + otherParticle = next(particleIterator); + } + } + + if (collision) { + move(particle, -deltaX, -deltaY); + particle.setSpeedX(0.0); + particle.setSpeedY(0.0); + } + } + } + + /** + * Constrains the particle inside the bounding rectangle if it exists. + * @param particle + *
Should not be null + *
Input-output parameter + */ + private final void constrain(final Particle particle) { + if (this.getBounds() != null) { + // Left bound + if (particle.getX() < this.getBounds().getX()) { + particle.setX(this.getBounds().getX()); + particle.setSpeedX(0.0); + } + // Top bound + if (particle.getY() < this.getBounds().getY()) { + particle.setY(this.getBounds().getY()); + particle.setSpeedY(0.0); + } + // Right bound + if (particle.getX() >= this.getBounds().getWidth()) { + particle.setX(this.getBounds().getWidth() - 1.0); + particle.setSpeedX(0.0); + } + // Bottom bound + if (particle.getY() >= this.getBounds().getHeight()) { + particle.setY(this.getBounds().getHeight() - 1); + particle.setSpeedY(0.0); + } + } + } + + public static final Double DEFAULT_GRAVITY = 4.0; + + /** + * + * @param particleIterator + *
Should no be null + *
Input-output parameter + * @return the next available element if there is one, or else null + *
A possibly null value + *
A reference + */ + private static final Particle next(final Iterator particleIterator) { + return particleIterator.hasNext() ? particleIterator.next() : null; + } + + /** + * TODO move this function into a ParticleUtilities class + * @param particle + *
Should not be null + * @param deltaX + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + * @param deltaY + *
Should not be null + *
Range: ]Double.NEGATIVE_INFINITY .. Double.POSITIVE_INFINITY[ + */ + private static final void move(final Particle particle, final Double deltaX, final Double deltaY) { + particle.setX(particle.getX() + deltaX); + particle.setY(particle.getY() + deltaY); + } + + /** + * TODO move this function into a ParticleUtilities class + * @param particle1 + *
Should not be null + * @param particle2 + *
Should not be null + * @return distance(particle1, particle2) <= particle1.getRadius() + particle2.getRadius() + *
A non-null value + */ + private static final Boolean collision(final Particle particle1, final Particle particle2) { + return distance(particle1, particle2) <= particle1.getRadius() + particle2.getRadius(); + } + + /** + * TODO move this function into a ParticleUtilities class + * @param particle1 + *
Should not be null + * @param particle2 + *
Should not be null + * @return max(abs(particle1.getX() - particle2.getX()), abs(particle1.getY() - particle2.getY())) + *
A non-null value + *
Range: [0 .. Double.POSITIVE_INFINITY[ + */ + private static final Double distance(final Particle particle1, final Particle particle2) { + return max(abs(particle1.getX() - particle2.getX()), abs(particle1.getY() - particle2.getY())); + } + +} diff --git a/src/org/sourceforge/desert/DesertElement.java b/src/org/sourceforge/desert/DesertElement.java index c66eb6e..1082437 100644 --- a/src/org/sourceforge/desert/DesertElement.java +++ b/src/org/sourceforge/desert/DesertElement.java @@ -41,7 +41,7 @@ public class DesertElement { * @param newColor * @return nothing */ - String setColor(Color newColor) { + public String setColor(Color newColor) { ourColor = newColor; return ""; } @@ -50,7 +50,7 @@ public class DesertElement { * * @return Color from the Element */ - Color getColor() { + public Color getColor() { return ourColor; } /** @@ -59,7 +59,7 @@ public class DesertElement { * @param newElementName * @return new element name */ - String setElementName(String newElementName) { + public String setElementName(String newElementName) { elementName = newElementName; return ""; } @@ -68,7 +68,7 @@ public class DesertElement { * * @return element name as a String */ - String getElementName() { + public String getElementName() { return elementName; } /** @@ -76,7 +76,7 @@ public class DesertElement { * * @return a java.awt.Button for the element */ - Button createButtonForElement() { + public Button createButtonForElement() { Button tmp = new Button(elementName); tmp.setForeground(ourColor); return tmp; -- 2.11.4.GIT