Hi, i’m new to this forum but i’ve already have some experience with Processing. Recently i started learning Android programming using Android Studio so i decided to setup the Processing core library. Inspired by a Shiffman’s video tutorial about object trails, my AS project is composed of:
- a Particle class in which an ArrayList (called history) saves the x-y coordinates of a ball. It contains an update() method for adding a random position to this array and a show() method that draws the ball and his trail:
package com.example.myfirstprocessingapp;
import java.util.ArrayList;
import processing.core.PApplet;
public class Particle extends PApplet {
private int x;
private int y;
private ArrayList<int[]> history = new ArrayList<int[]>();
public Particle (int x, int y) {
this.x = x;
this.y = y;
this.history.add(new int[] {x, y});
}
public void settings() { }
public void setup() { }
public void update() {
this.x += random(-10, 10);
this.y += random(-10, 10);
this.history.add(new int[] {this.x, this.y});
if (this.history.size() > 100) {
this.history.remove(0);
}
}
public void show() {
stroke(0);
fill(0, 150);
ellipse(this.x, this.y, 12, 12);
noFill();
beginShape();
for (int i = 0; i < this.history.size(); i++) {
int[] pos = this.history.get(i);
vertex(pos[0], pos[1]);
}
endShape();
}
}
- the Sketch class called by the MainActivity as a fragment. It simply set the canvas, draw particles and manage the mousePressed() adding a new Particle instance inside a Particle ArrayList:
package com.example.myfirstprocessingapp;
import java.util.ArrayList;
import processing.core.*;
public class Sketch extends PApplet {
ArrayList<Particle> particles = new ArrayList<Particle>();
public void settings() {
fullScreen();
background(255);
}
public void setup() {
}
public void mousePressed() {
particles.add(new Particle(mouseX - width/2, mouseY - height/2));
}
public void draw() {
background(255);
pushMatrix();
translate(width/2,height/2);
ellipse(0,0,50,50);
for (int i = 0; i < particles.size(); i++) {
particles.get(i).update();
particles.get(i).show();
}
popMatrix();
}
}
- the main Activity class used as launcher:
package com.example.myfirstprocessingapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import processing.android.CompatUtils;
import processing.android.PFragment;
import processing.core.PApplet;
public class MainActivity extends AppCompatActivity {
private PApplet sketch = new PApplet();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(CompatUtils.getUniqueViewId());
setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
sketch = new Sketch();
PFragment fragment = new PFragment(sketch);
fragment.setView(frame,this);
}
@Override
public void onNewIntent(Intent intent) {
if (sketch != null) {
sketch.onNewIntent(intent);
}
}
}
Although i was able to run the project explained in the Processing for Android (Studio) tutorial, it seems that in this case i forgot some basic steps to write a working Processing code.
Needless to say that this sketch worked in Android Mode in the Processing IDE.
This is the error generated in the logcat:
2019-05-30 10:12:30.354 30472-30507/com.example.myfirstprocessingapp E/AndroidRuntime: FATAL EXCEPTION: Animation Thread
Process: com.example.myfirstprocessingapp, PID: 30472
java.lang.NullPointerException: Attempt to invoke virtual method 'void processing.core.PGraphics.stroke(int)' on a null object reference
at processing.core.PApplet.stroke(PApplet.java:9440)
at com.example.myfirstprocessingapp.Particle.show(Particle.java:36)
at com.example.myfirstprocessingapp.Sketch.draw(Sketch.java:29)
at processing.core.PApplet.handleDraw(PApplet.java:1855)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)
2019-05-30 10:12:30.667 30472-30494/com.example.myfirstprocessingapp W/libEGL: EGLNativeWindowType 0x79384a1010 disconnect failed
2019-05-30 10:12:30.735 30472-30472/com.example.myfirstprocessingapp E/ViewRootImpl: sendUserActionEvent() returned.
Thanks in advance