Search code examples
javaarraysparticles

Particle system Array


so I've been working on getting my boxes to save their position in an array all day and finally thought i came up with something (with a lot of help from you guys) and it just isn't working... can someone please tell me why?

Control class:

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;

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

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        m.drawParticle(pressedX, pressedY);
    }

    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].x = pressedX;
        point[num].y = pressedY;
        }

    public int getID() {
        return ID;
    }


}

Methods class:

    import org.newdawn.slick.Graphics;

public class Methods {

    public Graphics g = new Graphics();

    public int sizeX = 1;
    public int sizeY = 1;

    public void drawParticle(float x, float y){
        g.drawRect(x, y, sizeX, sizeY);
    }

}

Solution

  • While you've initialised the size of the point array, you've not initialised the contents.

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

    Also think in your render method, you need to re-render the graphics (I could be mistaken, I've not used Slick2D before)...

    Withing something like...

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        for (Point p : point) {
            if (p != null) {
                m.drawParticle(p.x, p.y);
            }
        }
        m.drawParticle(pressedX, pressedY);
    }
    

    I'm also curious about you creating you're own Graphics, especially when the render method passes you one, you may want to check into that further and make sure that this is acceptable...