Update method tip
in
Share your Work
•
1 year ago
Hi,
I see a lot of people doing calculations in the draw method, which in some cases is necessary (when working on opengl textures etc.), but in most it can be avoided and usually only slows down the drawing thread.
I thought I'd share how I make a separate thread to handle the calculations.
I have a class called CalculationThread which looks like this:
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- public class CalculationThread implements Runnable {
- private Thread t;
- private int sleepTime;
- private ArrayList<UpdateListener> updateListeners = new ArrayList<UpdateListener>();
- public CalculationThread(int sleepTime) {
- this.sleepTime = sleepTime;
- }
- public void addUpdateListener(Object object) {
- try {
- updateListeners.add(new UpdateListener(object, object.getClass().getMethod("update", new Class[] {
- }
- )));
- }
- catch (SecurityException e) {
- e.printStackTrace();
- }
- catch (NoSuchMethodException e) {
- System.err.println("No public update method in " + object.getClass().getSimpleName());
- }
- catch (IllegalArgumentException e) {
- System.err.println("Update method in " + object.getClass().getSimpleName() + " shouldn't have any arguments");
- }
- }
- public void run() {
- Thread me = Thread.currentThread();
- while (t == me) {
- for (int i = 0; i < updateListeners.size(); i++) {
- UpdateListener ul = updateListeners.get(i);
- try {
- ul.getMethod().invoke(ul.getObject(), new Object[] {
- }
- );
- }
- catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- try {
- Thread.sleep(sleepTime);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- t = null;
- }
- public void start() {
- if (t == null) {
- t = new Thread(this);
- t.start();
- }
- }
- public void stop() {
- if (t != null) {
- t.interrupt();
- }
- t = null;
- }
- public void dispose() {
- stop();
- }
- public class UpdateListener {
- private Object object;
- private Method method;
- public UpdateListener(Object object, Method method) {
- this.object = object;
- this.method = method;
- }
- public Object getObject() {
- return object;
- }
- public void setObject(Object object) {
- this.object = object;
- }
- public Method getMethod() {
- return method;
- }
- public void setMethod(Method method) {
- this.method = method;
- }
- }
- }
What it does it looping all update methods added to the ArrayList
updateListeners.
This is how I use it.
- public static CalculationThread CalcThread;
- private PVector position;
- public void setup() {
- size(500, 500);
- background(255);
- CalcThread = new CalculationThread(16);
- CalcThread.start();
- CalcThread.addUpdateListener(this);
- position = new PVector(random(0, 450), random(0, 450));
- }
- public void update() {
- position.x = random(0, 450);
- position.y = random(0, 450);
- }
- public void draw() {
- background(255);
- fill(0);
- rect(position.x, position.y, 50, 50);
- }
Hope someone finds this useful!