How to restart a sketch project in processing? - processing

I am working on a Processing project, but I donĀ“t know how to restart the project once it is over. I have searched and found that the setup() method will make it. But it's not working. Can anyone help me. I would like the sketch to restart by itself once it is finished.
/* OpenProcessing Tweak of *#*http://www.openprocessing.org/sketch/59807*#* */
/* !do not delete the line above, required for linking your tweak if you upload again */
//
// outline: takes an image (image.jpg) and creates a sketch version
//
// procsilas (procsilas#hotmail.com / http://procsilas.net)
//
String iName="image.jpeg";
void setup() {
llegeixImatge("./"+iName);
size(img.width, img.height);
}
// parameters
// NO real control, so be careful
int NP=6000; // 1000 for line art, 10000 for complex images, O(N^2) so be patient!!!
int B=1; // try 2 or 3
float THR=28; // range 5-50
float MD=6; // range 0-10
int NMP=6; // range 1-15
float[][] punts;
color[] cpunts;
int [] usat;
int [] NmP=new int[NMP];
float [] NdmP=new float[NMP];
int inici=0;
PImage img;
void llegeixImatge(String s) {
img = loadImage(s);
img.loadPixels();
}
float fVar(int x, int y) {
// neighborhood 2B+1x2B+1 pixels
float m=0;
for (int k1=-B; k1<=B; k1++) {
for (int k2=-B; k2<=B; k2++) {
color c=img.pixels[(y+k1)*img.width+(x+k2)];
m+=brightness(c);
}
}
m/=float((2*B+1)*(2*B+1));
float v=0;
for (int k1=-B; k1<B; k1++) {
for (int k2=-B; k2<B; k2++) {
color c=img.pixels[(y+k1)*img.width+(x+k2)];
v+=(brightness(c)-m)*(brightness(c)-m);
}
}
v=sqrt(v)/(float) (2*B+1);
return v;
}
void creaPunts() {
punts = new float[NP][2];
cpunts = new color[NP];
usat = new int[NP];
int nint1=0;
int nint2=0;
for (int i=0; i<NP;) {
int x=B+int(random(width-2*B));
int y=B+int(random(height-2*B));
//println(i+" = "+x+", "+y+": "+THR+", "+MD);
// points need to be at least MD far from each other
int flag=0;
if (MD>0.0) {
for (int j=0; flag==0 && j<i; j++) {
if (dist(x, y, punts[j][0], punts[j][1])<MD) {
flag=1;
}
}
}
if (flag==0) {
nint1=0;
float f=fVar(x, y);
// use only "valid" points
if (f>=THR) {
nint2=0;
punts[i][0]=x;
punts[i][1]=y;
cpunts[i]=img.pixels[y*img.width+x];
usat[i]=0;
i++;
}
else {
nint2++;
if (nint2>=10) {
THR/=(1+1.0/float(NP-i));
MD/=(1+1.0/float(NP-i));
nint2=0;
}
}
}
else {
nint1++;
if (nint1>=10) {
MD/=2.0;
THR*=1.618;
nint1=0;
}
}
}
}
int NessimMesProper(int i) {
if (NMP<=1) {
int mP=-1;
float dmP=dist(0, 0, width, height);
for (int j=0; j<NP; j++) {
if (usat[j]==0) {
float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
if (jmP<dmP) {
dmP=jmP;
mP=j;
}
}
}
return mP;
}
else {
for (int j=0; j<NMP; j++) {
NmP[j]=-1;
NdmP[j]=dist(0, 0, width, height);
}
for (int j=0; j<NP; j++) {
if (usat[j]==0) {
float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
int k=NMP;
while(k>0 && NdmP[k-1]>jmP) {
k--;
}
if (k<NMP) {
for (int l=0; l<(NMP-k)-1; l++) {
NmP[(NMP-1)-l]=NmP[(NMP-1)-(l+1)];
NdmP[(NMP-1)-l]=NdmP[(NMP-1)-(l+1)];
}
NmP[k]=j;
NdmP[k]=jmP;
}
}
}
return NmP[NMP-1];
}
}
int fase=0;
void draw() {
if (fase==0) {
creaPunts();
background(#FFFFFF);
fase=1;
}
else {
if (inici!=-1) {
stroke(#000000);
usat[inici]=1;
int seguent=NessimMesProper(inici);
if (seguent!=-1) {
line(punts[inici][0], punts[inici][1], punts[seguent][0], punts[seguent][1]);
}
inici=seguent;
}
else {
//save("outline_"+iName);
}
}
}

You should not call setup() yourself.
Step 1: Encapsulate the state of your program in a set of variables.
Step 2: Use those variables to draw your sketch.
Step 3: Modify those variables to change what's being drawn.
Step 4: Simply reset those variables to their initial values when you want to reset the sketch.
Here's an example program that stores its state (the positions the user has clicked) in an ArrayList. It uses that ArrayList to draw the sketch, and new points are added whenever the user clicks. When the user types a key, the sketch is reset by clearing out the ArrayList:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup(){
size(500, 500);
}
void draw(){
background(0);
for(PVector p : points){
ellipse(p.x, p.y, 20, 20);
}
}
void mousePressed(){
points.add(new PVector(mouseX, mouseY));
}
void keyPressed(){
points.clear();
}

Related

Processing String splitting and loops to form a facade

I have tried many methods, and can't seem to grasp the idea of extracting an index from my array of strings to help me generate my desired number of building with a desired height, please help, here is my example
edit: Hi, i saw your feedback and posted my code below, hopefully it helps with the idea overall, as much as it is just creating rects, its more complicated as i need to involve arrays and string splitting along with loops. i more or less got that covered but i as i said above, i cannot extract the values from my array of string and create my facades at my own desired number and height
String buffer = " ";
String bh = "9,4,6,8,12,2";
int[] b = int(split(bh, ","));
int buildingheight = b.length;
void setup () {
size(1200, 800);
background(0);
}
void draw () {
}
void Textbox() {
textSize(30);
text(buffer, 5, height-10);
}
void keyTyped() {
if (key == BACKSPACE) {
if (buffer.length() > 0) {
buffer = buffer.substring(0, buffer.length() - 1);
}
} else if (key == ENTER) {
background(0);
stroke(0);
GenerateFacade();
println(buffer);
}
else {
buffer = buffer + key;
Textbox();
}
}
void GenerateFacade() {
fill(128);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (int(b[j]) > buildingheight) {
buildingheight = int(b[j]);
}
}
rect(i*width/b.length, height - (int(b[i])*height/buildingheight), width/b.length, int(b[i])*height/buildingheight);
}
}
For the next time it would be great if you provide us with some code so we know what you tried and maybe can point you to the problem you have.
You need just the keyPressed function and some variables
int x = 0;
int buildingWidth = 50;
int buildingHeight = height / 6;
void setup(){
size(1000, 500);
background(255);
}
void draw(){
}
void keyPressed(){
if(key >= '0' && key <= '9'){
int numberPressed = key - '0' ;
fill(0);
rect(x, height - buildingHeight * numberPressed,
buildingWidth, buildingHeight * numberPressed);
x += buildingWidth;
}
}
This is my result

How can I create points, display them and store them in some sort of Array

I want to create points and then store them in an array. I'm doing this to put a linear regression through my data points afterwards. So I need to be able to cycle through all my points.
I could not find anything like that on the web for processing and as I was not really able to do it, I need your help. Here is my approach, but it doesn't seem to work:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
background(255);
}
void draw(){
for (int i = 1; i == dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
I would like to have a program to create points and store them in an array (or something similar, that you can cycle through).
Most of your code makes sense, there are only two gotchas I could spot that may prevent you from cycling through all your points and visualising them:
your condition is will go to an array index out of bounds: try for (int i = 0; i < dataPoints.size(); i++)
remember to clear the frame, otherwise you're drawing on top of the same dots over and over again
Remember array indices start at 0 in Processing/Java (and likewise the last index will not be the size() of your array, but the 1 less, hence the < in the for condition)
Here is your code with the above tweaks:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
}
void draw(){
background(255);
for (int i = 0; i < dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
Note that Processing has a handy PVector class (which has x,y properties) so you could do something like this:
ArrayList<PVector> dataPoints = new ArrayList<PVector>();
void setup(){
size(1000, 1000);
strokeWeight(10);
stroke(255,0,0);
noFill();
}
void draw(){
background(255);
beginShape();
for (int i = 0; i < dataPoints.size(); i++) {
PVector point = dataPoints.get(i);
vertex(point.x,point.y);
}
endShape();
}
void mousePressed() {
dataPoints.add(new PVector(mouseX, mouseY));
}
This a bit of a detail, but I recommend to following Java Naming Convention to keep the code consistent. (For example: renaming the dataPoint class to DataPoint and renaming the Point instance to point)

How to change cells of array by keyPressed()?

I try to make a web application.
You can change cells of the array by pressing arrow keys here.
There is a class "Module" with methods display() and update(). These methods change the inner array data[].
class Module {
int i; // index
int x; // coordinate
int y; // coordinate
int[] data = new int[]{0,0,0,0,0};
// Contructor
Module(int x){
this.x = x;
}
void update() {
data[i]=_mas_stor;
}
void display(){
text(data[i], x, 100);
}
}
But how to set the initial value of the array _mass[] at the beginning of the program?
The whole program here.
There is no need of an array of data in the class Module. It is sufficient that each object has it single data member. Wirte a constructor, eher you can pass to the initial data (Module(int x, int d)):
class Module {
int i;
int x;
int y;
int data;
// Contructor
Module(int x, int d){
this.x = x;
this.data = d;
}
void update() {
data=_mas[global_i];
}
void display(){
textSize(30);
text(data, x, 100);
}
}
Now the object can be initialized in a loop with ease:
int[] _mas ={1,2,3,4,5};
int global_i = 0;
Module [] mods;
void setup() {
size(500, 400);
mods = new Module[5];
for (int i = 0; i < 5; ++ i ) {
mods[i] = new Module(i*50+50, _mas[i]);
}
}
Further you have to ensure that global_i doesn't go out of bounds in keyPressed:
void keyPressed() {
if (keyCode == UP) {
_mas[global_i]++;
}
if (keyCode == DOWN) {
_mas[global_i]--;
}
if (keyCode == LEFT) {
global_i--;
if (global_i < 0)
global_i = 4;
}
if (keyCode == RIGHT) {
global_i++;
if (global_i > 4)
global_i = 0;
}
}
Note, you can further improve you program, if you skip the global variable _mas and add a increment method (inc) and decrement method (dec) to the class Module, instead of the update method:
int global_i = 0;
Module [] mods;
void setup() {
size(500, 400);
mods = new Module[5];
for (int i = 0; i < 5; ++ i ) {
mods[i] = new Module(i*50+50, i);
}
}
void draw() {
background(50);
for (int i = 0; i < 5; ++ i ) {
mods[i].display();
}
}
void keyPressed() {
if (keyCode == UP) {
println("up");
mods[global_i].inc();
}
if (keyCode == DOWN) {
mods[global_i].dec();
}
if (keyCode == LEFT) {
global_i--;
if (global_i < 0)
global_i = 4;
}
if (keyCode == RIGHT) {
global_i++;
if (global_i > 4)
global_i = 0;
}
}
class Module {
int i;
int x;
int y;
int data;
// Contructor
Module(int x, int d){
this.x = x;
this.data = d;
}
void inc() {
this.data ++;
}
void dec() {
this.data --;
}
void display(){
textSize(30);
text(data, x, 100);
}
}
You usually set the initial value of an array using a for loop. Something like this:
String myArray = new String[10];
for(int i = 0; i < myArray.length; i++){
myArray[i] = "hello world";
}
What you put inside the for loop depends on what values you want your array to start with.

Delay in my code

I'd like to know why there is a delay in function mousePressed.
I know there is a problem in my code especially in for loops,
and if there are two balls on each others the bottom one disappeared sometimes in first if I press on them.
And sometimes the top one disappeared first
int number=80;
int i;
ball [] balls =new ball[number];
void setup(){
size(1000,1000);
frameRate(60);
for(i=0 ;i<number;i++)
{
balls[i]=new ball(color(random(0,255),random(0,255),random(0,255)),random(30,970) , random(30,970),random(1.9,2));
}
}
void draw()
{
background(255,0,0);
for( i=number-1 ;i>=0;i--)
{
if (mousePressed == true) {
balls[i].disapeear();
}
balls[i].display();
balls[i].bouncing();
}
}
class ball
{
float speed,x,y;
color c;
float A=1;
float B =1;
ball(color colour ,float horiz,float vert,float s)
{
speed = s;
x = horiz;
y = vert;
c = colour;
}
void disapeear()
{
float L = sqrt((x-mouseX )*(x-mouseX))+((y-mouseY)*(y-mouseY));
if(L<15)
{
x=-100;
y=-100;
}
}
void bouncing()
{
x=x+(speed * A);
y=y+(speed * B);
if((x>width-30)||(x<0))
{
A =A * -1 ;
}
if((y>height-30)||(y<0))
{
B =B * -1 ;
}
}
void display()
{
fill(c);
stroke(0,0,255);
ellipse(x,y,30,30);
}
}
When I was looking at your code, the one thing you don't have is a mousePressed() function.
If you add that function to the main class, that might stop the delay.

Mixing active and static modes in Processing

Making a space invaders knock off game. My code will not compile as I get the error message "It looks like you're mixing 'active' and 'static' modes", but I can not see where I am mixing them. Can someone please take a look at my code?
final int SCREENX=400;
final int SCREENY=400;
final int GAP=10;
final int ALIEN_ALIVE=0;
final int ALIEN_DEAD=6;
final int FORWARD=0;
final int BACKWARD=1;
final int MARGIN=30;
Alien theAliens[];
Bullet bullets[];
Player thePlayer;
void setup() {
PImage normalImg, explodeImg;
size(SCREENX, SCREENY);
normalImg =loadImage("invader.GIF");
explodeImg =loadImage("exploding.GIF");
theAliens = new Alien[10];
bullets = new Bullet[20];
init_aliens(theAliens, normalImg, explodeImg);
thePlayer = new Player(SCREENY- 50);
}
void init_aliens(Alien baddies[], PImage okImg, PImage
exImg) {
for (int i=0; i<baddies.length; i++) {
// This is buggy, what is the problem?
baddies[i] = new Alien(i*(okImg.width+GAP), 0, okImg,
exImg);
}
}
void init_bullets() {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
b.move();
b.draw();
}
}
void shoot() {
if (mousePressed)
Player.shoot();
}
void draw() {
background(0);
thePlayer.draw();
thePlayer.move(mouseX);
draw_bullets(myBullets);
for (int i=0; i<theAliens.length; i++) {
theAliens[i].move();
theAliens[i].draw();
if (random(0, 500)<1)
theAliens[i].die();
}
}
////// Player Class //////
Player() { ///** When I get the error, this line is highlighted**///
this.x = width/2;
this.y = height-50;
this.timeLastShot = 0;
this.coolDown = 200;
colour playColour= color(50);
void draw() {
fill(playerColour);
rect(this.x, this.y, 30, 30);
}
void move(int x) {
if (x>SCREENX-50)
xpos= SCREENX-50;
else xpos=x;
}
void shoot() {
if (millis() - timeLastShot > coolDown) {
Bullet bullet = new Bullet(this.x+12.5, this.y, -5);
bullets.add(bullet);
timeLastShot = millis();
}
}
}
Your Player class is badly written. It should be:
class Player {
Player () {
//constructor
}
void functionOfSorts () {
} // Never forget to enclose functions with curly brackets!
}
...As opposed to what you wrote:
Player() {
//yadayada
}

Resources