package de.tlahmann.gdg;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.data.JSONArray;
import processing.data.JSONObject;

public class Painter extends PApplet {

	private final int WIDTH = 800;
	private final int HEIGHT = 800;
	
	ArrayList<Particle> particles;
	Region regions;
	long lastSpawn = 0;

	JSONArray file;
	int colorModel = 1;
	int backgroundColor;
	int[] particleColors;
	int fillRegion;

	public void setup() {
		file = loadJSONArray("./src/de/tlahmann/gdg/data/colors.json");

		JSONObject JColors = file.getJSONObject(colorModel);

		JSONObject colorBG = JColors.getJSONObject("background");
		JSONArray colorP = JColors.getJSONArray("particles");
		backgroundColor = color(colorBG.getInt("r"), colorBG.getInt("g"), colorBG.getInt("b"), colorBG.getInt("a"));
		fillRegion = JColors.getInt("fill");
		particleColors = new int[colorP.size()];
		for (int i = 0; i < colorP.size(); i++) {
			particleColors[i] = color(colorP.getJSONObject(i).getInt("r"), colorP.getJSONObject(i).getInt("g"),
					colorP.getJSONObject(i).getInt("b"), colorP.getJSONObject(i).getInt("a"));
		}

		init();
	}

	void init() {
		particles = new ArrayList<Particle>();
		for (int i = 0; i < 10; i++) {
			newParticle(0, 0);
		}
		regions = new Region(this);
	}

	public void settings() {
		size(WIDTH, HEIGHT);
	}

	public void draw() {
		background(backgroundColor);

		// run the Regions before the points to be drawn properly
		if (particles.size() > 1) {
			runRegions();
		}

		removeDead();
		runParticles();
	}

	public void mouseDragged() {
		if (lastSpawn + 10 > millis()) {
			return;
		}
		newParticle(mouseX, mouseY);
		lastSpawn = millis();
	}

	public void mouseClicked() {
		newParticle(mouseX, mouseY);
	}

	public void keyPressed() {
		if (key == 'r' || key == 'R') {
			init();
		}
	}

	void newParticle(int x, int y) {
		particles.add(new Particle(this, x, y, particleColors[(int) random(0, particleColors.length)], fillRegion));
	}

	void removeDead() {
		// Cycle through the ArrayList backwards, because we are deleting while
		// iterating
		for (int i = particles.size() - 1; i >= 0; i--) {
			if (particles.get(i).isDead()) {
				particles.remove(i);
			}
		}
	}

	void runParticles() {
		for (Particle p : particles) {
			// Update and display
			p.run();
		}
	}

	void runRegions() {
		regions.run(particles);
	}

	public static void main(String[] args) {
		PApplet.main(new String[] { Painter.class.getName() });
	}
}

@@ -191,32 +191,39 @@
 let %7B%0A%0A%09private 
+static 
 final int WIDTH 
@@ -238,16 +238,23 @@
 private 
+static 
 final in
@@ -269,17 +269,16 @@
  = 800;%0A
-%09
 %0A%09ArrayL
@@ -342,16 +342,23 @@
  = 0;%0A%0A%09
+static 
 JSONArra
@@ -366,16 +366,23 @@
  file;%0A%09
+static 
 int colo
@@ -394,16 +394,23 @@
 l = 1;%0A%09
+static 
 int back
@@ -423,16 +423,23 @@
 Color;%0A%09
+static 
 int%5B%5D pa
@@ -453,16 +453,23 @@
 olors;%0A%09
+static 
 int fill
@@ -854,47 +854,8 @@
 ));%0A
-%09%09fillRegion = JColors.getInt(%22fill%22);%0A
 %09%09pa
@@ -1119,24 +1119,63 @@
 t(%22a%22));%0A%09%09%7D
+%0A%09%09fillRegion = JColors.getInt(%22fill%22);
 %0A%0A%09%09init();%0A

Change some fields to be static