I’ve made an attempt at implementation, but I’m getting errors and I’m not super proficient with Java.
Here is the complete code:
Code -- All
import processing.svg.*;
//Create global variables
PGraphics svg;
int canvasW = 1400;
int canvasH = 800;
ArrayList<ArrayList<PVector>> group;
ArrayList<PVector> line;
//Settings (here because we may want variable canvas size)
void settings() {
size(canvasW,canvasH); //set canvas size
}
// setup runs before the main loop
void setup() {
svg = createGraphics(canvasW, canvasH, SVG, "Filename_new.svg"); //initialize the object we will record our .svg to.
}
// the main loop
void draw() {
stroke(0); //sets stroke color (there may be a better place for this
if (mousePressed == true) {
line(mouseX, mouseY, pmouseX, pmouseY); //visual feedback for the user
line.add(new PVector()); //add a new PVector containing the mouse_pos coordinates to an Array List ('line')
}
}
void mouseReleased() {
// add the ArrayList 'line' as an entry in ArrayList 'group'
// clear the contents of line
group.add(line); // Need to watch this one, it might make a shallow copy when we want a deep one.
line.clear();
}
void keyPressed() { //When a key is pressed, stop recording the SVG and exit.
noLoop();
svgRecord();
exit();
}
void svgRecord() {
svg.beginDraw();
svg.background(128,0,0);
for (int i = 0; i < group.size(); i = i+1){
svg.beginShape();
for (int j = 0; j < group.get(i).size(); j = j+1) {
svg.curveVertex(group.get(i).get(j).x,group.get(i).get(j).y);
}
svg.endShape();
}
svg.dispose();
svg.endDraw();
}
And here is the code that’s throwing an error:
Code -- The Issue
// the main loop
void draw() {
stroke(0); //sets stroke color (there may be a better place for this
if (mousePressed == true) {
line(mouseX, mouseY, pmouseX, pmouseY); //visual feedback for the user
line.add(new PVector()); //add a new PVector containing the mouse_pos coordinates to an Array List ('line')
}
}
More specifically,
line.add(new PVector()); //add a new PVector containing the mouse_pos coordinates to an Array List ('line')
This line throws a NullPointerException. A cursory search on geeksforgeeks tells me that it might be caused by one of these:
- Invoking a method from a null object.
- Accessing or modifying a null object’s field.
- Accessing or modifying the slots of null object, as if it were an array.
I am not sure how to troubleshoot this, but I think the issue is with the new PVector()
snippet. For reference, I am following this example, specifically the last line of this passage:
// Declaring the ArrayList, note the use of the syntax "<Particle>" to indicate
// our intention to fill this ArrayList with Particle objects
ArrayList<Particle> particles = new ArrayList<Particle>();
// Objects can be added to an ArrayList with add()
particles.add(new Particle());
Any thoughts on this syntax?
P.S.
@jafal
Thanks for the source, I’m not sure it fits my current strategy but I’ll keep it in mind.