Hey there, I'm fairly new to Processing, but I seem to have picked it up quite easily thanks to the posts on the forum :)
I am working on a sketch that lets the user add ellipses by pressing one key, giving them focus by clicking on them, moving them about by clicking and dragging, and removing the focused shape them by pushing another key. It will eventually be used as a user interface for an audio installation I will be running in the next few months.
I can get most of this to work, but the thing I am having trouble with is recalling necessary data associated with each shape such as X/Y data as well as further information that I wish to store.
For example, lets say I have created 4 ellipses on the canvas, when I click on one of them, I would like to be able to recall a list with 6 pieces of info from them - [X(float), Y(float), float, float, float, String] .
The ultimate goal is to be able to save the array into a .txt file and the recall the locations and associated information for each shape on startup so the user can continue working on the project.
So far I have been using two examples of adding/removing shapes, but I am a little confused on the rest. Here is my modified ball class sketch to show you what I mean.
- //So here I have an arraylist that is storing the ball data, but I have no idea how to call it up from the array in order to
- //put it onscreen.
- ArrayList balls;
- int ballWidth = 48;
- void setup() {
- size(500, 500);
- smooth();
- noStroke();
- // Create an empty ArrayList
- balls = new ArrayList();
- // Start by adding one element
- balls.add(new Ball(width/2, 0, ballWidth));
- }
- void draw() {
- background(255);
- // With an array, we say balls.length, with an ArrayList, we say balls.size()
- // The length of an ArrayList is dynamic
- // Notice how we are looping through the ArrayList backwards
- // This is because we are deleting elements from the list
- for (int i = balls.size()-1; i >= 0; i--) {
- // An ArrayList doesn't know what it is storing so we have to cast the object coming out
- Ball ball = (Ball) balls.get(i);
- ball.display();
- /*
- if (ball.finished()) {
- // Items can be deleted with remove()
- balls.remove(i);
- }
- */
- }
- addsource();
- }
- void addsource () {
- if(keyPressed==true){
- if (key == 'b' || key == 'n') {
- balls.add(new Ball(mouseX, mouseY, ballWidth));
- } else {
- }
- if(key == 'r'){
- if(balls.size()>0){
- balls.remove(0);
- } else {
- println("Nothing to delete!");
- }
- }
- }
- }
- void mousePressed() {
- // A new ball object is added to the ArrayList (by default to the end)
- balls.add(new Ball(mouseX, mouseY, ballWidth));
- }
- //Class down here, not all of it is used.
- class Ball {
- float x;
- float y;
- float speed;
- float gravity;
- float w;
- float life = 255;
- Ball(float tempX, float tempY, float tempW) {
- x = tempX;
- y = tempY;
- w = tempW;
- speed = 0;
- gravity = 0.1;
- }
- void move() {
- // Add gravity to speed
- speed = speed + gravity;
- // Add speed to y location
- y = y + speed;
- // If square reaches the bottom
- // Reverse speed
- if (y > height) {
- // Dampening
- speed = speed * -0.8;
- y = height;
- }
- }
- boolean finished() {
- // Balls fade out
- life--;
- if (life < 0) {
- return true;
- } else {
- return false;
- }
- }
- void display() {
- // Display the circle
- fill(0,life);
- //stroke(0,life);
- ellipse(x,y,w,w);
- }
- }
I have also had a play with this. I have gotten the shapes to be added at a click of a button, but not to be removed in this case -
Thanks guys, I really appreciate your help.
Iyad.
1