0

Possible Duplicate:
Checking against indexed arrays

So i have my code, every time the mouse is pressed it draws a new particle at mouseX and mouseY and stores the position in an array so it can update until told to stop when it reaches the bottom of the screen. What i would like to do is check the current position against that of an already 'made' particle and have it stop sooner if it has the same coordinates, to give a stacking effect, can someone please help me?

    import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
    public static final int ID = 1;

    public Methods m = new Methods();
    public Point[] point = new Point[(800 * 600)];

    int pressedX;
    int pressedY;
    int num = 0;
    String Build = "1.1";

    public void init(GameContainer container, StateBasedGame game) throws SlickException{
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        for (int index = 0; index < point.length; index++) {
            Point p = point[index];
            if (p != null) {
                if (p.y >= 598) {
                    m.drawParticle(point[index].x,(point[index].y));
                } else {
                    m.drawParticle(p.x, p.y);
                    p.y++;
                }
            }
        }
        g.drawString("Particle Test", 680, 0);
        g.drawString("Build: " + Build, 680, 15);
        g.drawString("Pixels: " + num, 10, 25);
    }

    public void update(GameContainer container, StateBasedGame game, int delta) {
    }

    public void mousePressed(int button, int x, int y) {
        pressedX = x;
        pressedY = y;
        num = num + 1;
        point[num] = new Point(pressedX, pressedY);
        }

    public int getID() {
        return ID;
    }

}
Community
  • 1
  • 1
user1610541
  • 111
  • 1
  • 11

2 Answers2

2

You can use the coordinates as a hash and store the points in a HashSet, just overload the hashing function of Point with something like x*y

Joseph
  • 21
  • 1
  • can you please explain how i would implement that into my code, or a link to a tutorial? – user1610541 Oct 09 '12 at 00:37
  • You make it stop if the add to the set returns false, quick Google search: http://www.java-tips.org/java-se-tips/java.util/how-to-use-hashset.html – Joseph Oct 09 '12 at 00:47
0

You can write a new method that checks to see if the point is in the array at an index of less than "Index".

public boolean isInArray(int index)
{
    for (int index2 = 0; index2 < index; index2++)
    {
        if( (point[index2].x == point[index].x) &&
            (point[index2].y == point[index].y) )
        {
            return true;    //point(index) is previously in array
        }
    }
    return false;    //point(index) is NOT previously in array
}

Then in your for..next loop, you'll need to use the break statement

if (isInArray(index))
{
    //the break statement causes the program to exit the current loop
    break;
}

However, you might be better served by testing it in your MousePressed method to avoid adding the point to the array in the first place if it's a duplicate.

Good luck!

Roy
  • 974
  • 6
  • 11