So I'm just trying to replicate this for starters.
https://processing.org/examples/setupdraw.html
It tells me "unexpected token: int".
So how the frick...
Like I got the book "Getting Started with Processing."
Its got some good stuff. But i still can't figure out why this doesn't work.
Can you not establish a variable in the set up?
void setup() {
size(1000, 1000);
background(0);
stroke(255)
int line1 = 100;
}
void draw() {
line1 = line1 -1;
if (line1 < 0){
line1 = height;
}
line(0, line1, width, line1);
}
Semicolon missing issue. Best practice is Just remove the statement int line1 = 100; and put it as global, outside the methods as below
int line1 = 100;
void setup() {
size(1000, 1000);
background(0);
stroke(255);
}
void draw() {
line1 = line1 -1;
if (line1 < 0){
line1 = height;
}
line(0, line1, width, line1);
}
Related
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
I need to write a typing program to create a “three-line” text editor. If the length of the
input string is longer than the line (40 characters), it will be automatically move to
the next line.
This is my code so far:
String word = "";
void setup() {
size(1000, 600);
textSize(26);
fill(0);
}
void draw() {
background(255);
text(word, 0, 0, width, height);
}
void keyPressed() {
int lineBreak = word.length();
if(lineBreak > 39) {
word = word + "\n";
}
else {
word = word + key;
}
}
Whenever I run the problem, the string length stops at 40 characters but it doesn't move to the next line.
text() does not support line breaks.
You have to store each line to a separate string. For instance use StringList to store the lines of the text.
Create an array of strings, that stores the finished lines. The current line is still stored in word:
StringList lines = new StringList();
When the line limit is reached, then add the line to the list and start a new line:
if(lineBreak > 39) {
lines.append(word);
word = "";
}
Define a lineheight an draw the lines in a loop. The vertical position of a line is computed by lineheight * (i + 1), where i is the index of the line:
int lineheight = 30;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
text(line, 0, lineheight * (i + 1));
}
text(word, 0, lineheight * (lines.size() + 1));
See the example:
StringList lines = new StringList();
String word = "";
void setup() {
size(1000, 600);
textSize(26);
fill(0);
}
void draw() {
background(255);
int lineheight = 30;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
text(line, 0, lineheight * (i + 1));
}
text(word, 0, lineheight * (lines.size() + 1));
}
void keyPressed() {
int lineBreak = word.length();
if(lineBreak > 39) {
lines.append(word);
word = "";
}
else {
word += key;
}
}
I am currently using a processing sketch to work through a large number of images I have in a folder. I have set an onClick command to advance to the next image in the string. It is quite time consuming and I would like to automate the action that once the sketch is completed the sketch would repeat it's self selecting the next image from the string. The onClick command also save the image the export folder but each time saves with the same file name, I've tried to set up sequential numbering but it hasn't worked so far. Any help would be greatly appreciated.
String[] imgNames = {"1.jpg", "2.jpg", "3.jpg"};
PImage img;
int imgIndex = 0;
void nextImage() {
background(255);
frameRate(10000);
loop();
frameCount = 0;
img = loadImage(imgNames[imgIndex]);
img.loadPixels();
imgIndex += 1;
if (imgIndex >= imgNames.length) {
imgIndex = 0;
}
}
void paintStroke(float strokeLength, color strokeColor, int strokeThickness) {
float stepLength = strokeLength/4.0;
// Determines if the stroke is curved. A straight line is 0.
float tangent1 = 0;
float tangent2 = 0;
float odds = random(1.0);
if (odds < 0.7) {
tangent1 = random(-strokeLength, strokeLength);
tangent2 = random(-strokeLength, strokeLength);
}
// Draw a big stroke
noFill();
stroke(strokeColor);
strokeWeight(strokeThickness);
curve(tangent1, -stepLength*2, 0, -stepLength, 0, stepLength, tangent2, stepLength*2);
int z = 1;
// Draw stroke's details
for (int num = strokeThickness; num > 0; num --) {
float offset = random(-50, 25);
color newColor = color(red(strokeColor)+offset, green(strokeColor)+offset, blue(strokeColor)+offset, random(100, 255));
stroke(newColor);
strokeWeight((int)random(0, 3));
curve(tangent1, -stepLength*2, z-strokeThickness/2, -stepLength*random(0.9, 1.1), z-strokeThickness/2, stepLength*random(0.9, 1.1), tangent2, stepLength*2);
z += 1;
}
}
void setup() {
size(1600, 700);
nextImage();
}
void draw() {
translate(width/2, height/2);
int index = 0;
for (int y = 0; y < img.height; y+=1) {
for (int x = 0; x < img.width; x+=1) {
int odds = (int)random(20000);
if (odds < 1) {
color pixelColor = img.pixels[index];
pixelColor = color(red(pixelColor), green(pixelColor), blue(pixelColor), 100);
pushMatrix();
translate(x-img.width/2, y-img.height/2);
rotate(radians(random(-90, 90)));
// Paint by layers from rough strokes to finer details
if (frameCount < 20) {
// Big rough strokes
paintStroke(random(150, 250), pixelColor, (int)random(20, 40));
} else if (frameCount < 1000) {
// Thick strokes
paintStroke(random(75, 125), pixelColor, (int)random(8, 12));
} else if (frameCount < 1500) {
// Small strokes
paintStroke(random(20, 30), pixelColor, (int)random(1, 4));
} else if (frameCount < 10000) {
// Big dots
paintStroke(random(5, 10), pixelColor, (int)random(5, 8));
} else if (frameCount < 10000) {
// Small dots
paintStroke(random(1, 2), pixelColor, (int)random(1, 3));
}
popMatrix();
}
index += 1;
}
}
if (frameCount > 10000) {
noLoop();
}
// if(key == 's'){
// println("Saving...");
// saveFrame("screen-####.jpg");
// println("Done saving.");
// }
}
void mousePressed() {
save("001.tif");
nextImage();
}
Can't you just call the nextImage() function from inside this if statement?
if (frameCount > 10000) {
noLoop();
}
Would be:
if (frameCount > 10000) {
nextImage();
}
As for using different filenames, just use an int that you increment whenever you save the file, and use that value in the save() function. Or you could use the frameCount variable:
save("img" + frameCount + ".tif");
If you have follow-up questions, please post a MCVE instead of your whole project. Try to isolate one problem at a time in a small example program that only does that one thing. Good luck.
Code Updated 1/26
So I came up with a problem for my students that seemed simple in theory. A little embarrassingly, however, I cannot create a working solution for it.
So, I've done some extensive troubleshooting and research and I think I've found the hang-up, but not how to get around it.
We are using ProcessingJS. It seems that in any situation where I have a for-loop running through an array inside another for-loop searching through a different array, my program hangs up. It doesn't crash, it just... doesn't do anything. It creates a window that doesn't do anything.
The task is to create a program that looks at the documents of a file and identify how many instances each letter and character there are.
My code has nested for-loops. The first loop goes through the contents of the first line in my file. The second loops uses the result of the first loop and looks through an array to see if any 'instances' of that letter have already been found. If it has not been found, it appends it. If an instance of the letter HAS been found, then it increases the number value found in a second array.
The contents of the file are 4 lines, each line containing hundreds of words separated by commas.
Here's my code:
//!!!!!!!!!!Declare Variables!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
String exampleTXT[] = loadStrings("sounds.txt");
//String [][] results = {{"null", "null"}};
String [] resultsAlpha = {"null"};
int[] resultsNumber = {0};
boolean analyze = true;
//
//!!!!!!!!!!!Void Setup!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
void setup() {
println("hey 1");
size(800, 800);
println("hey 2");
}
//!!!!!!!!!!!End of Void Setup!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
//!!!!!!!!!!!Void Draw!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
//
void draw() {
println("hey 3");
background(0);
println("hey 4");
fill(0, 255, 0);
println("hey 5");
textAlign(LEFT, TOP);
println("hey 6");
//nothing to see on this line
println("hey 7");
if (analyze == true) {
println("hey 8");
for (int i = 0; i < exampleTXT[0].length(); i++) {
println("hey 9");
for ( int r = 0; r < resultsAlpha.length(); r++) {
println("hey 10");
if (exampleTXT[0][i] != resultsAlpha[r]) {
println("hey 11");
resultsAlpha = append(resultsAlpha, exampleTXT[0][i]);
println("hey 12");
resultsNumber = append(resultsNumber, 0);
println("hey 13");
}
println("hey 14");
if ( exampleTXT[0][i] == resultsAlpha[r]) {
println("hey 15");
resultsNumber[r]++;
println("hey 16");
}
println("hey 17");
}
println("hey 18");
}
println("hey 19");
analyze = false;
println("hey 20");
}
println("hey 21");
for (int i = 0; i<resultsAlpha.length(); i++) {
println("hey 22");
text(resultsAlpha[i] + " " + resultsNumber[i], 0, 10*i);
println("hey 23");
}
println("hey 24");
if (frameCount%60==0) {
println("hey 25");
println(frameCount / 60);
println("hey 26");
}
println("hey 27");
}
//!!!!!!!!!!!End of Void Draw!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
//!!!!!!!!!!!Void KeyPressed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
//
void keyPressed() {
if (key == DELETE) {
println(exampleTXT[0]);
}
if (keyCode == UP) {
println(resultsNumber);
}
//
// Break your code up into logical sections with comments!
//
}
//!!!!!!!! End of Void keyPressed !!!!!!!!!!!!!!!
So that's what I'm working with. I am wondering if maybe I am running into some sort of memory issue when trying to do this. I'm not sure, though, because that seems like it wouldn't impact the system that hard, but I frankly don't have any other reasoning.
The one answer provided so far was for another version of code. With that helpful advice I was able to get that to work, but the problem with this code still remains :(
Can anyone explain what my issue is?
for (int r = 0; r < numbers.length; i++){
You are incrementing i instead of r in this loop.
I am trying to create 100 frames where the numbers 0-100 are "printed" in the center of every frame. If I try the code below I get the error "It looks like you're mixing "active" and "static" modes".
size(1440,900);
font = loadFont("Arial-Black-96.vlw");
textFont(font,96);
int x = 0;
void draw() {
background(204);
y=0;
if (x < 100) {
text(y, 10, 50);
x = x + 1;
y=y+1;
} else {
noLoop();
}
// Saves each frame as screen-0001.tif, screen-0002.tif, etc.
saveFrame();
}
You need to wrap the first 3 lines in the setup() function.
Like:
void setup(){
size(1440,900);
font = loadFont("Arial-Black-96.vlw");
textFont(font,96);
}
I answered this without running the code, there were others issues, here a version of your code:
PFont font;
int x = 0;
int size = 96;
void setup() {
size(1440, 900);
font = createFont("Arial-Black", size);
textFont(font);
}
void draw() {
background(204);
if (x <= 100) {
String number = nf(x, 2);
text(number, width/2 - textWidth(number)/2, height/2);
x++;
saveFrame();
}
else {
exit();
}
}