Processing: Rendering stops when delay function used - processing

I created a simple delay function for my sketch and tried to use it, but it seems as if the rendering stops i.e., there is simple a grey screen and then everything is rendered all at once.
Could someone please tell me where I am going wrong? What exactly is happening?
Also how are draw() and setup() defined internally? I understand that setup() is a one time render and draw() like an infinite loop.
Code:
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
void setup(){
size(600,400);
smooth();
}
void draw(){
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}

Also how are draw() and setup() defined internally? I understand that setup() is a one time render >and draw() like an infinite loop.
The thing is that draw() only renders a frame at the end of each execution, so stoping it is usually not a good idea.
http://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F
If you want to time things use booleans as flags for when things should happen.
check those also:
http://wiki.processing.org/w/What_are_setup()_and_draw()%3F
http://wiki.processing.org/w/How_do_I_display_a_message_for_a_few_seconds%3F

If you want delay, use a Thread() object, like the following code, to run the delays in a separate thread:
void delay(int delay)
{
try
{
Thread.sleep(delay);
}
catch(Exception EX)
{
}
}
void setup(){
size(600,400);
smooth();
thread("sleepy"); // the function sleepy will be started in a new thread
}
void draw(){
// draw() does nothing here except refresh the screen
}
void sleepy()
{
while(true)
{
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}
}
Additionally, the above delay method, Thread.sleep(), does not consume an entire CPU core as it runs (The millis() method is akin to a temporary infinite loop).
Tested on Processing 2.2.1, Windows 7 professional.

Related

Processing Translations

In the processing language I am trying to create a translation similar to the image below:
Output Goal
In my code, the image is moving but it isn't showing the original picture in addition to the translation and it isn't being shown across the screen as i'd like.
I have included the code I have so far below:
PImage img;
int reps=10;
void setup()
{
size(600,120);
triangle(30,5,50,30,15,20);
save("image.png");
img=loadImage("image.png");
}
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
image(img,0,0);
translate(img.height,0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}
This is what it produces so far:
current_output
Im happy it's translating, I am just trying to figure out how to see the original in addition to the translation and have it shown multiple times.
Thanks in advance!!
The images have to be translated differently depending on the index. The 2nd image has to be translated more than the 1st one and the 3rd more than the 2nd:
translate(img.height * i, 0);
function draw:
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
translate(img.height * i, 0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}

A while loop is causing some graphical glitches

I was just messing around with the while loop, testing it vs the for loop, when this graphical glitch occurred. This happens on an inconsistent basis, so I do refresh it to get results, but occasionally the screen will be split into horizontal lines, each with their different color. Why is this occurring?
int i = 0;
void setup() {
size(400, 400);
}
void draw() {
while(i < 1) {
background(random(255),random(255),random(255));
}
println("yes");
}
void mousePressed() {
i += 1;
}

I came across a reflect invocation target exeption in processing and can't find what is causing it

I was remaking a platformer after I closed it without saving my code and came across the error. I don't know how the error occurred and am completely lost.
Here's the code below(sorry for any incomplete code):
PImage saw = loadImage("saw.png");
game run = new game();
void setup(){
size(1000, 1000);
}
void draw(){
run.saw(100, 100);
}
class game{
boolean dead;
int ballX;
int ballY;
int sawRotate;
game(){
dead = false;
ballX = 100;
ballY = 50;
sawRotate = 1;
}
void ball(){
}
void saw(int x, int y){
pushMatrix();
rotate(sawRotate);
translate(x, y);
image(saw, 100, -100, 500, 500);
popMatrix();
}
void platform(){
}
}
You can't use Processing functions until after the setup() function has been called. This line happens before that:
PImage saw = loadImage("saw.png");
You need to move the call to loadImage() to be inside the setup() function:
PImage saw;
void setup(){
size(1000, 1000);
saw = loadImage("saw.png");
}

Stop and Restart game in Processing on button press

I'm currently working on a small game to learn Processing.
I want my game to stop when I press "Stop" and want my game to reset/restart when "Stop" changed to Start.
(When I click on the button, it changes Stop to Start and back to Stop etc etc. (so basically, I have 1 'button')
I'm struggling and can't find a solution on internet / stackoverflow so maybe someone can help me?
(# mousePressed's if else I need the 'stop and restart function')
float x = width/2;
float speed = 2;
boolean textHasBeenClicked = false;
int aantalRaak = 0;
int aantalMis = 0;
int positieText = 20;
void setup() {
background(0);
size(600,500);
}
void draw() {
clear();
move();
display();
smooth();
//Scoreboard bovenaan
fill(255);
textSize(20);
textAlign(LEFT);
text("Aantal geraakt: " + aantalRaak,0, positieText); text("Aantal gemist: " + aantalMis, width/2, positieText);
//button onderaan
fill(0,255,0);
rect(width/2-40, height-40, 100, 50);// draw anyway...
}
void mousePressed() {
// toggle
textHasBeenClicked = ! textHasBeenClicked;
fill(0);
if (textHasBeenClicked) {
// display text 2
textSize(30);
textAlign(CENTER);
text("Stop" , width/2,height-10);
}
else {
// display text 1
textSize(30);
textAlign(CENTER);
text("Start" , width/2,height-10);
}
}
void move() {
x = x + speed;
if (x > width) {
x = 0;
}
}
void display(){
//schietschijf
float y = height/2;
noStroke();
fill(255, 0, 0);
ellipse(x, y, 40, 40);
fill(255);
ellipse(x, y, 30, 30);
fill(255, 0, 0);
ellipse(x, y, 20, 20);
fill(255);
ellipse(x, y, 10, 10);
}
You should try to break your problem down into smaller steps and take those steps on one at a time. You're really asking two questions:
How do I show a stop button that turns into a start button?
How do I reset a sketch?
For the first question, you can create a boolean variable. Use that variable in your draw() function, and then modify that variable in the mousePressed() function.
boolean running = false;
void draw() {
fill(0);
if (running) {
background(255, 0, 0);
text("Stop", 25, 25);
} else {
background(0, 255, 0);
text("Start", 25, 25);
}
}
void mousePressed() {
running = !running;
}
Then for resetting the sketch, you can create a function that changes all of your variables back to their default values. Here's a simple example:
float circleY;
void setup() {
size(100, 500);
}
void draw() {
background(0);
circleY++;
ellipse(width/2, circleY, 20, 20);
}
void reset() {
circleY = 0;
}
void mousePressed() {
reset();
}
Try to work from small examples like this instead of your full program, and post a MCVE if you get stuck. Good luck.
You may consider implementing a while loop. I don't know what library you're using for input, so I can't tell you exactly what to do. But something along the lines of:
while(!InputReceived) {
if(CheckForMouseInput()) // Assuming CheckForMouseInput returns true if input was detected
break // Input was detected, now do stuff based on that.
else {
// Must #include <thread> and #include <chrono>
// Wait a bit...
continue; // Jump back to the top of the loop, effectively restarting it.
}
Would probably suit your needs. That is what I would do, at least. After the loop breaks, the game is effectively restarted, and you can do whatever you need to do based on that.

Use of delay() in Processing Environment

I am using Processing language to sketch a rectangle that grows in size with time. Following code is not giving any output.
void setup()
{
size(900,900);
}
void draw()
{
int edge=100;
for(int i=0;i<300;i++)
{
delay(100);
edge++;
rect(100,100,edge,edge);
}
}
I suspect having wrongly used delay() function.
Here is one such "roll your own" delay method which is good for most purposes. Just change the values passed into the delay method to alter the timing. This just outputs "start" and "end" roughly every 2 seconds for example.
void draw()
{
System.out.println("start");
delay(2000);
System.out.println("end");
delay(2000);
}
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
I recommend rolling your own delay system using the millis() function.
Have a look at this example.
With processing, the screen does not get refreshed until the program flow reaches the end of draw()
Try the following:
void setup()
{
size(900,900);
frameRate(10);
}
int edge = 100;
void draw()
{
edge++;
rect(100,100,edge,edge);
}

Resources