Add an ArrayList to another ArrayList


Hi i start to write and i have my first issues :slight_smile:

First i forgot to say, im using Java with Eclipse.

So here what i wrote for now :

1: my class Game (where i launch world)

public class Game extends PApplet{
	public static void main(String[] args){
	public void settings(){
	World world;
	public void setup(){
		world = new World(this);
	public void draw(){
		world.makeVegetalPassif(20, 20);

then the World :

public class World {
	PApplet parent;
	public int worldW = 2000;
	public int worldH = 2000;
	Player player;
	Prairie prairie;
	ArrayList<VegetalPassif> vegepas = new ArrayList<VegetalPassif>();
	public World(PApplet p){
		parent = p;
		float px = worldW/2;
		float py = worldH/2;
		PVector playerVector = new PVector(px,py); 
		player = new Player(parent,playerVector);
		prairie = new Prairie(parent);
	public void makeVegetalPassif(int nbrGrass2, int nbrMushroom){
		for(int i = 0; i < nbrGrass2 + nbrMushroom; i++){
			PVector next = findSuitablePosition(100);
			if(next == null){
			if(i < nbrGrass2){
				vegepas.add(new Grass2(parent,next));
				vegepas.add(new Mushroom(parent,next));
	private PVector findSuitablePosition(int maxTime){
		PVector pp;
		boolean found;
		int time = parent.millis();
			found = true;
                        ////////////here i make a change since i use a bigger world than the screen///////////
			pp = new PVector(parent.random(worldW-1750 , worldW-250), parent.random(worldH-1750 , worldH-250));
			for(VegetalPassif vegepassif : vegepas){
				if(PVector.dist(vegepassif.pos, pp) < 32){
					found = false;
		}while(!found && parent.millis()- time <= maxTime);
		return found ? pp : null;
	public void render(){
		parent.background(0); ///fond noir = le nΓ©ant
		//////fct pas correctement, a refaire avec une "camera"
		parent.translate(-player.position.x+worldW-parent.width-200, -player.position.y+worldH-parent.height-650);
		//////le fond blanc//////
		parent.fill(255); ///le monde en lui meme
		parent.rect(0, 0, worldW, worldH);
		for(VegetalPassif vegepassif : vegepas){
		parent.text("player x " + player.position.x + " " + "player y " + player.position.y, 10, 10);

VegetalPassif (its like Obstacle)

public class VegetalPassif {
	PApplet p;
	public PVector pos = new PVector();
	int color;
	int diam; ///diametre
	VegetalPassif(PApplet parent, PVector pos){
		p = parent;
	public void render(){
	    p.translate(pos.x, pos.y);
	    p.ellipse(0, 0, diam, diam);

Grass2 (the β€œ2” is because i dont want to erase the class i wrote before):

public class Grass2 extends VegetalPassif {
	public Grass2(PApplet p, PVector pos){
		diam = 32;
		color = 006633;

and Mushroom:

public class Mushroom extends VegetalPassif {
	public Mushroom(PApplet p, PVector pos){
		diam = 32;
		color = 666600;

i didnt change the player and prairie.
i use a world bigger than my screen to have more space (i should told you that im sorry).
the player for now is used only to see my world.

So, nothing appeard on the screen except my player and the prairie and its super laggy.


i found that its not render but the world is stuck in a loop.
So i have two issues:

first: makeVegetalPassif and findSuitablePosition are stuck in a loop
second: nothing is render exept player and prairie


The draw method is executed ~60fps so the first line world.makeVegetalPassif(20, 20); is also executed 60 times a second. So you are adding 1200 Grass2 and 1200 Mushroom a second there will not be suitable positions for all but the findSuitablePosition method allows 100ms per attempt, a maximum of 4 minutes per frame!


ho i didnt know that. how do i manage that ?


Do not create lots of objects in the draw loop. Perhaps do it when a key is typed so someother event.