Animating in a class outside of the main class
in
Integration and Hardware
•
2 years ago
There have been similar questions asked such as
https://forum.processing.org/topic/animating-outside-draw but none of these seem to work.
Below is a simplified version of my application:
- package example;
- import processing.core.*;
- import java.util.*;
- public class Example extends PApplet{
- public static ArrayList<Thing> things = new ArrayList<Thing>();
- public static void main(String[] args) {
- System.out.println("Creating objects!");
- for(int i = 0; i< 5; i++) {
- //New Thing (size, location)
- things.add(new Thing(5,new PVector(10,10)));
- }
- PApplet.main(new String[]{"example.Example"});
- }
- @Override
- public void setup() {
- size(1000,1000);
- background(100);
- }
- @Override
- public void draw(){
- for(int i = 0; i<things.size(); i++) {
- things.get(i).run();
- }
- }
- }
package example;- import processing.core.*;
- import java.util.*;
- public class Thing extends PApplet{
- private int size;
- private PVector location;
- public Thing(int size, PVector location) {
- this.size = size;
- this.location = location;
- }
- public void run() {
- draw();
- }
- public void draw(PApplet parent) {
- int aColour = color(100,50,50);
- fill(aColour);
- ellipse(500,500,100,100);
- }
- }
The error message I get when I try to animate in the Thing class is:
Exception in thread "Animation Thread" java.lang.NullPointerException
Exception in thread "Animation Thread" java.lang.NullPointerException
Any ideas?
1