Search code examples
javaarraysarraylistslick2dparticles

Multiple Rectangle Generation


Possible Duplicate:
Drawing multiple pixels/rectangles

In my code i wrote a method that creates a rectangle at mouseX, mouseY. but all it does is update the position of that rectangle so it follows the mouse, i want it to create a new one at the mouse every time the method runs, can someone please help?

this is my method

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

The main class Control call the drawParticle method;

import java.awt.Point;
import java.awt.geom.Point2D;

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 Graphics g = new Graphics();

    int mouseX;
    int mouseY;


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

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

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

    public void mousePressed(int button, int x, int y) {
        mouseX = x;
        mouseY = y;
    }

    public int getID() {
        return ID;
    }

}

Thanks - Shamus


Solution

  • One way to do this is to create a List as a member variable and add a new Rectangle each time the user clicks the mouse. Then in your render() method, iterate through the List of Rectangles and paint each one.