Hi, I am using Processing v3.2.3 and controlP5 v2.2.6 library to develop my own application. In a sketch I need to intercept user keyboard key press to active some functions in my application so I need to know if a textfield got focus to disable keyboard key press functions. How can I check if a textfield got focus?
Processing v3.2.3 + controlP5 v2.2.6 questions....
Fix text that is misplaced
Every time I fail and press R to restart the startup text is very off coordinate to where it is when it first starts up.
/* How to Play : Use arrow-keys and press the key shoutcasted by the window (e.g. Press left if it says "! Left !").
You win once you've reached a win streak of 20. You lose if you reach 0 HP.*/
import ddf.minim.*;
AudioPlayer spl;
Minim song;
PImage imgChrome, imgChrome2;
String[] symDanceKeys = {"←", "→", "↑", "↓"};
String[] ltrDanceKeys = {"Left", "Right", "Up", "Down"};
String cLTRSkey = "A";
String currentSYMkey = "←";
String cSk = currentSYMkey;
String currentLTRkey = "Left";
String cLk = currentLTRkey;
int aSec;
int emoHP = 0;
int emoExpression = 0;
int danceStreak = 0;
int displayKeyX = 50;
int displayKeyY = 60;
boolean paused = false;
boolean tMin=false; //Timer minutes
boolean ending = false;
boolean displayKeyPressed=true;
boolean startScreen=false;
boolean winGame=false;
boolean endScreen=false;
boolean easy=false;
boolean medium=false;
boolean hard=false;
boolean expert=false;
float danceDiff = 2;
float timer = 0; // in seconds.
float sTimer; //Screen timer
float suTimer; //Startup Text timer
float dTimer; //Difficulty timer
float splTimer; // Song timer
PFont ar;
void setup() {
song = new Minim(this);
spl = song.loadFile("Tet.mp3");
imgChrome2 = loadImage("chrome2.jpg");
imgChrome = loadImage("chrome.jpg");
background(#C6ba32);
ar = createFont("ArialMT-48", 14, true);
size(800, 500);
frameRate(60);
}
void draw() {
//if (splTimer>=4) {
// spl.play();
//spl.loop(-0);
//}
splTimer += 0.0166666667;
suTimer += 0.0166666667;
dTimer += 0.0166666667;
if (suTimer<=4) {
startScreen=true;
}
if (startScreen) {
textSize(14);
fill(#00E8FF);
noStroke();
quad (30, 15, 500, 15, 480, 130, 10, 130);
fill(#000000);
text("To play, press the keys you're asked to press", 30, 30);
text("as they are scrolling down the screen.", 30, 45);
text("The more you press the correct key, the fast it gets.", 30, 60);
text("The more you press the wrong key, your HP goes down.", 30, 75);
text("If your HP hits 0, game over, but if your streak hits 20, you win!", 30, 90);
text("Press the keys 1-4 on the startup screen to change the difficulty.", 30, 105);
text("Press nothing for Super Easy mode!", 30, 120);
if (suTimer>=4) {
startScreen=false;
}
}
textFont(ar, 14);
if (ending) {
endScreen=true;
background(#C6ba32);
displayText("Press R to restart the game!", (310));
displayText("The game has ended..", (230));
String endingText = "";
if (winGame) {
endScreen=true;
endingText = "You have won the game, Congrats! ( ͡° ͜ʖ ͡°)";
} else {
endingText = "You have lost the game, You suck! ( ͡° ʖ̯ ͡°)";
}
displayText(endingText, (270));
delay(500);
} else {
// Since the frameRate is 60, to calculate 1 second, add by 1/60 everytime draw() runs.
//vv|LOOK HERE FOR TIMER|vv\\
timer += 0.0166666667;
if (timer >=4 ) {
println(mouseX+"X", mouseY+"Y");
println( width+"W", height+"H");
timer = 0;
//^^|LOOK HERE FOR TIMER|^^\\
if (displayKeyPressed) {
// Selects a random number between 0 and the length of the danceKeys array/table.
//vv|LOOK HERE FOR RANDOMIZER|vv\\
int randomKey = int(random(ltrDanceKeys.length));
cLk = ltrDanceKeys[randomKey];
cSk = symDanceKeys[randomKey];
//^^|LOOK HERE FOR RANDOMIZER|^^\\
displayKeyY = 60;
displayKeyX = int(random(50, width - 50));
displayKeyPressed = false;
text(cSk + cSk + cLk + cSk + cSk, displayKeyX, displayKeyY);
} else {
displayKeyPressed = true;
}
}
// If four seconds hasn't passed yet..
else if (!displayKeyPressed) {
//Resets the background so the moving text effect is more pleasing.
background(#C6ba32);
drawEmoticon(emoExpression);
displayKeyY += danceDiff;
// If it reaches the bottom, you can't press the assigned key anymore.
if (displayKeyY >= (height - 30)) {
displayKeyPressed = true;
} else {
textSize(14);
// Creates a wind effect after dance streak reaches 16.
if (danceStreak >= 16) {
for (int i = 1; i <= (danceStreak - 15); i++) {
fill(0, ((4 - i) * 30));
text(cSk + cSk + cLk + cSk + cSk, int(random(displayKeyX - 30, displayKeyX + 30)), displayKeyY - (i * 20));
}
}
fill(0);
textSize(20);
text(cSk + cSk + cLk + cSk + cSk, displayKeyX, displayKeyY);
}
displayBars();
displayHealth();
}
fill(#FFFFFF);
//quad(1 x, 1 y, 2 x, 2 y, 3 x, 3 y, 4 x, 4 y);
quad(676, 443, 737, 443, 737, 458, 676, 458);
quad(676, 430, 737, 430, 737, 444, 676, 444);
textSize(10);
fill(#000000);
text("Mins | Sec", 707, 440);
textSize(14);
sTimer +=0.0166666667;
//If sTimer is 0
if (sTimer >=0) {
//Display timer
fill(#000000);
text(sTimer, 711, 457);
}
if (sTimer>=60.000) {
tMin = true;
sTimer=0;
}
//If tMin is true
if (tMin) {
tMin=false;
//Add a minute to the timer
fill(#000000);
text(aSec+".", 685, 457);
aSec = aSec + 1;
}
if (sTimer>=0.000) {
fill(#000000);
text(aSec+".", 683, 457);
}
}
}
void keyPressed() {
if (key=='r'||key=='R') {
if (endScreen==true) {
restart();
}
}
if (key=='1') {
if (dTimer<=4) {
easy=true;
medium=false;
hard=false;
expert=false;
fill(#ffffff);
quad(50, 420, 128, 420, 128, 440, 50, 440);
fill(#000000);
text("EASY", 70, 435);
} else {
if (dTimer>=4) {
println("Cannot change difficulty at this time.");
}
}
}
if (key=='2') {
if (dTimer<=4) {
easy=false;
medium=true;
hard=false;
expert=false;
fill(#ffffff);
quad(50, 420, 128, 420, 128, 440, 50, 440);
fill(#000000);
text("MEDIUM", 60, 435);
emoHP=8;
danceDiff=3;
} else {
if (dTimer>=4) {
println("Cannot change difficulty at this time.");
}
}
}
if (key=='3') {
if (dTimer<=4) {
easy=false;
medium=false;
hard=true;
expert=false;
fill(#ffffff);
quad(50, 420, 128, 420, 128, 440, 50, 440);
fill(#000000);
text("HARD", 70, 435);
emoHP=4;
danceDiff=6;
} else {
if (dTimer>=4);
println("Cannot change difficulty at this time.");
}
}
if (key=='4') {
if (dTimer<=4) {
easy=false;
medium=false;
hard=false;
expert=true;
fill(#ffffff);
quad(50, 420, 128, 420, 128, 440, 50, 440);
fill(#000000);
text("EXPERT", 60, 435);
emoHP=1;
danceDiff=10;
} else {
if (dTimer>=4) {
println("Cannot change difficulty at this time");
}
}
}
if (key=='P'||key=='p') {
paused=true;
noLoop();
textSize(24);
fill(#00E8FF);
rect(335, 190, 160, 100);
fill(#000000);
text("Paused", 415, 245);
textSize(12);
text("Press ENTER to continue", 415, 280);
} else {
if (keyCode==ENTER&&paused==true) {
paused=false;
background(#C6ba32);
drawHead();
displayBars();
displayHealth();
draw();
clear();
loop();
}
// Problem #1; How2 detect WASD keys?
if (!displayKeyPressed) {
if (keyCode == LEFT) {
if (cLk == ltrDanceKeys[0]) { // Selects the first (0) value from the danceKeys array.
//println(danceKeys[0]) == String "Left"
changeHealth(1);
} else {
paused=false;
changeHealth(-1);
}
} else if (keyCode == RIGHT) {
if (cLk == ltrDanceKeys[1]) {
changeHealth(1);
} else {
paused=false;
changeHealth(-1);
}
} else if (keyCode == UP) {
if (cLk == ltrDanceKeys[2]) {
changeHealth(1);
} else {
changeHealth(-1);
}
} else if (keyCode == DOWN) {
if (cLk == ltrDanceKeys[3]) {
changeHealth(1);
} else {
changeHealth(-1);
}
}
}
}
}
void changeHealth(int num) {
displayKeyPressed = true;
emoHP = emoHP + num;
// Check if win or lose.
//VV||MAX HP||VV\\
if (emoHP > 10) {
emoHP = 10;
//^^||MAX HP||^^\\
} else if (emoHP <= 0) {
println("Game over!");
ending = true;
}
if (danceStreak >= 20) {
println("You have won the game!");
ending = true;
winGame = true;
endScreen = true;
}
// Checking win streaks for Difficulty Management and Emoticon Control.
if (num >= 0) {
danceStreak ++;
emoExpression = 0;
} else {
danceStreak --;
emoExpression = 1;
if (danceStreak<=0) {
danceStreak=0;
}
}
if (danceStreak<=1 && easy==true) {
danceDiff = 2;
} else {
if (danceStreak >2&& easy==true) {
danceDiff = danceStreak;
}
}
if (danceStreak <= 1 && medium==true) {
danceDiff = 3;
} else {
if (danceStreak >=3 && medium==true) {
danceDiff = danceStreak;
}
}
if (danceStreak <= 1 && hard==true) {
danceDiff = 6;
} else {
if (danceStreak >=6 && hard==true) {
danceDiff = danceStreak;
}
}
if (danceStreak<=1 && expert==true) {
danceDiff = 10;
if (danceDiff > 10) {
danceDiff = 10;
}
}
// This is to prevent that "freezing" effect that occurs when a key is pressed (due to timer use).
background(#C6ba32);
drawEmoticon(emoExpression);
displayBars();
displayHealth();
}
void displayBars() {
noStroke();
fill(150);
rect(0, 0, width, 30);
}
void displayHealth() {
noStroke();
fill(150);
rect(0, 0, width, 30);
fill(250, 250, ((10 - emoHP) * 10) + 100);
// Using algebra to determine how long the HP bar should be.
int totalTopBarLen = width;
int healthBarLen = (totalTopBarLen / 10) * emoHP;
rect(0, 0, healthBarLen, 30);
displayText("(" + emoHP + " HP) (" + danceStreak + " Streak) (" + danceDiff + " Diff)", 20);
}
void displayText(String str, int y) {
fill(0);
textSize(16);
textAlign(CENTER);
text(str, width / 2, y);
}
//This uses math to make the faces
void drawEmoticon(int faceNum) {
drawHead();
// Mouth
if (faceNum == 0) {
if (emoHP <= 6) {
line((width / 2) - 20, (height / 2) + 50, (width / 2) + 20, (height / 2) + 50);
} else {
// Arithmetic sequences formulae used to calculate the mouth-thingys' lengths.
int mouthLenX = ((-5) + (emoHP - 1) * 5);
int mouthLenY = ((75) + (emoHP - 1) * -5);
line(width / 2, (height / 2) + 60, (width / 2) - mouthLenX, (height / 2) + mouthLenY);
line(width / 2, (height / 2) + 60, (width / 2) + mouthLenX, (height / 2) + mouthLenY);
}
} else if (faceNum == 1) {
line((width / 2) - 20, (height / 2) + 50, (width / 2) + 20, (height / 2) + 50);
if (emoHP <= 6) {
int mouthLenX = (50 + ((emoHP - 1) * -4));
int mouthLenY = (80 + ((emoHP - 1) * -4));
line((width / 2) - 20, (height / 2) + 50, (width / 2) - mouthLenX, (height / 2) + mouthLenY);
line((width / 2) + 20, (height / 2) + 50, (width / 2) + mouthLenX, (height / 2) + mouthLenY);
}
}
}
void drawHead() {
// Head
noStroke();
fill(250, 250, ((10 - emoHP) * 10) + 100); // Changes face-paleness according to health.
ellipse(width / 2, height / 2, width / 3, width / 3);
// Eyes
stroke(50);
strokeWeight(5);
ellipse((width / 2) - 40, (height / 2) - 30, 20, 20);
ellipse((width / 2) + 40, (height / 2) - 30, 20, 20);
}
void restart() {
background(#C6ba32);
danceDiff = 2;
timer = 0; // in seconds.
sTimer = 0; //Screen timer
suTimer = 0; //Startup Text timer
dTimer = 0; //Difficulty timer
splTimer = 0; // Song timer
paused = false;
tMin=false; //Timer minutes
ending = false;
displayKeyPressed=true;
startScreen=false;
winGame=false;
endScreen=false;
easy=false;
medium=false;
hard=false;
expert=false;
aSec = 0;
emoHP = 10;
emoExpression = 0;
danceStreak = 0;
endScreen=false;
redraw();
}
HTTP POST request problem
Hi everyone! I have a problem with HTTP request library. I use this code:
import http.requests.*;
import java.net.*;
import java.io.*;
import java.util.*;
String API="myApi"; // Api code
String answer="My answer"; // answer text
String messageTo="1111111"; // example integer
String urlAnswer = URLEncoder.encode(trim(answer)); // encode answer string
PostRequest post = new PostRequest("https://"+"api.telegram.org/bot"+API+"/sendmessage?chat_id="+messageTo+"&text="+urlAnswer);
post.send();
and status monitor show me this error message:
java.lang.NullPointerException
at http.requests.PostRequest.send(Unknown Source)
at sketch_160109a.setup(sketch_160109a.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:2373)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1523)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
But GET request work perfect.
Can somebody explain me how I fix this problem?
I use Processing 3.0 on MacOS 10.9.5
color mapping
Hey everyone! I'm quite new to processing and there's something I can't quite figure out. I'm trying to make a sphere of which the radius of each point is calculated by perlin noise. This seems to be working but now I'm trying to map the color of each part of the sphere be mapped to the radius of that point. This doesn't happen however, the entire sphere is changing color. Can anyone tell me where I'm making the mistake? Any input is appreciated!
P.S. also, after a while of running the program, the radius stops being updated for some reason. If anyone knows why that is I'd love to hear it!
import peasy.*;
import peasy.org.apache.commons.math.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam cam;
PVector[][] globe;
int total = 150;
float rad = 25;
float nI=0;
float nJ=0;
float hu;
boolean calculated = false;
void setup() {
cam = new PeasyCam(this, 100);
size(600, 600, P3D);
globe = new PVector[total+4][total+4];
colorMode(HSB);
}
void draw() {
background(0);
lights();
// if (!calculated) {
//float nJ=0;
for (int i = 0; i < total+1; i++) {
float lat = map(i, 0, total, 0, PI);
for (int j = 0; j < total+1; j++) {
rad = map(noise(i+nI, j+nJ), 0, 1, 90, 110);
nI+=0.0000005;
nJ+=0.0000005;
hu = map(rad, 90, 110, 0, 255);
fill(hu, 255, 255);
float lon = map(j, 0, total, 0, TWO_PI);
float x = rad*cos(lon)*sin(lat);
float y = rad*sin(lon)*sin(lat);
float z = rad*cos(lat);
globe[i][j] = new PVector(x, y, z);
}
}
calculated = true;
//}
for (int i =0; i < total; i++) {
beginShape(TRIANGLE_STRIP);
for (int j = 0; j < total + 1; j++) {
PVector v = globe[i][j];
PVector v2 = globe[i+1][j];
vertex(v.x, v.y, v.z);
vertex(v2.x, v2.y, v2.z);
}
endShape();
}
}
com.hamoid.VideoExport library sync issue with processing.video library
Hello!
I'm using the VideoExport library in conjunction with the processing.video library to try to display and capture a webcam video while running Processing. The video displays beautifully in a section of the window in Processing. The issue I'm having is that I want to store the video at 10fps, and Processing's frame rate shifts around a bit as the program runs. If I grab every frame after the draw function I get a video that drifts out of time as the frame rate shifts slightly over time. For my application, having the video well synchronized is important, so I need it to match the system clock. So, I tried the following code to address this problem.
After creating the VideoExport instance I set the frame rate to 10 per second and set the filename, then start the movie: videoExport.setFrameRate(OutputFrameRate); videoExport.setMovieFileName("NewMovie.mp4"); videoExport.startMovie();
Then in the draw() function, I use the code below to read a frame from the web cam, but then only export the frame if it's been 100ms since the last frame was written.
if (cam.available() == true) {
cam.read();
}
image(cam, x, y-navHeight,640,480);
if(WebCamActive){
CurrentMilliseconds = millis();
if (CurrentFrame == 0){ //This is the first frame to be recorded
videoExport.saveFrame();
CurrentFrame = 1;
} else {
if (CurrentMilliseconds > BaseFrameTime + (MillisecondsBetweenFrames*CurrentFrame)) {
println(CurrentMilliseconds);
videoExport.saveFrame();
CurrentFrame = CurrentFrame+1;
}
}
}
Looking at the output in the console, the export code is being executed every 100ms.
When I look at the video on the screen in processing, it looks great. When I look at the recorded video, it's synced up correctly with the time (I added a text overlay with a timestamp to make sure), but the video keeps freezing and restarting, like the export function is sometimes sending out the same frame multiple times, and then it jumps ahead to the correct frame.
I don't understand why if the video is being displayed correctly on screen, the saveFrame function is not grabbing the frame that's currently being displayed when it's called. Any ideas? I feel like this is VERY close to what I need. Just need that one last piece.
Thanks! Dan
OpenCV
Can we expect an update on the OpenVC library?
Video 1.0.1 - Read a frame from a paused video?
I'm using Processing to make a small app. I want to display and edit text overlays on top of a video file - similar to subtitles. For that, I want to be able to jump a paused video to a specific timecode and display the video frame at that time. Unfortunately, this doesn't seem to work.
So if I make something like this:
movie.pause();
movie.jump(30);
movie.read();
The video will NOT show the frame at second 30, but whatever frame was on the screen before the jump. If I jump around in the video the same frame keeps being displayed until I hit play.
Is there a workaround for this?
Changing sound-value with camera tracking?
/edit
ControlP5 - font/color & linebreak() usage
Hi, I have just found this library, but do not know how to implement linebreak() or font/color/size changes for Textlabel. When I try to change the font it doesn't work, and I don't know how to use linebreak and can't find any information - (Beginner) Cheers!
using GStreamer in Eclipse multiple project architecture
i have the following applet:
public class FakeApp extends PApplet{ public PApplet father; int numPixels; int[] backgroundPixels; Capture video;
Display d;
public void init(){
super.init();
this.setup();
}
public void setup() {
super.setup();
size(640, 480);
d=new display();
d.setup((PApplet)this);
}
public void draw(){
d.draw();
}
}
where Display is delegated to show camera input (background subtraction in the processing showcase):
public void setup(PApplet father) { this.father=father;
// This the default video input, see the GettingStartedCapture // example if it creates an error video = new Capture(father, 640, 480); //video = new Capture(father, father.width, father.height);
// Start capturing the images from the camera video.start();
numPixels = 640* 480;//video.width * video.height; // Create array to store the background image backgroundPixels = new int[numPixels]; // Make the pixels[] array available for direct manipulation father.loadPixels(); }
public void draw() { if (video.available()) { video.read(); // Read a new video frame video.loadPixels() ; // Make the pixels of video available
// Difference between the current frame and the stored background int presenceSum = 0; for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... // Fetch the current color in that location, and also the color // of the background in that spot int currColor = video.pixels[i]; int bkgdColor = backgroundPixels[i]; // Extract the red, green, and blue components of the current pixel's color int currR = (currColor >> 16) & 0xFF; int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract the red, green, and blue components of the background pixel's color int bkgdR = (bkgdColor >> 16) & 0xFF; int bkgdG = (bkgdColor >> 8) & 0xFF; int bkgdB = bkgdColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = father.abs(currR - bkgdR); int diffG = father.abs(currG - bkgdG); int diffB = father.abs(currB - bkgdB); // Add these differences to the running tally presenceSum += diffR + diffG + diffB; // Render the difference image to the screen father.pixels[i] = father.color(diffR, diffG, diffB); // The following line does the same thing much faster, but is more technical //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; } father.updatePixels(); // Notify that the pixels[] array has changed //father.println(presenceSum); // Print out the total amount of movement } }
// When a key is pressed, capture the background image into the backgroundPixels
// buffer, by copying each of the current frame's pixels into it.
public void keyPressed() {
video.loadPixels();
father.arraycopy(video.pixels, backgroundPixels);
}enter code here
}
running this PApplet as an applet actually works, and also the following:
public class FakeStart {
public static void main(String[] args) { // TODO Auto-generated method stub JFrame f=new JFrame(); f.setBounds(0, 0, 640, 480); f.setLayout(null); FakeApp app=new FakeApp(); app.init(); //app.size(500, 500); //app.setup(); f.setResizable(false); f.show(); f.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent we) { System.exit(0); } } ); f.add(app); app.show(); } }
works fine. the problem is that the code :
public static void main(String[] args) { aaa.view.video.FakeStart.main(null); }
fails with:
(javaw.exe:5620): GStreamer-WARNING **: Failed to load plugin 'C:/Users/bbb/Desktop/aaa/dskV/video/library/\windows64\plugins\libgstwavparse.dll': `C:/Users/bbb/Desktop/aaa/dskV/video/library/\windows64\plugins\libgstwavparse.dll': Impossibile trovare il modulo specificato. +30 similar warnings, and the camera fails to load. (the path C:/Users/bbb/Desktop/aaa/dskV/video/library/\windows64\plugins\ is correct)
So in short:
-when i run the code as an applet it works.
-when i launch a main that opens the applet in a jframe it works (if the main is in the dskV project where the native code is).
-when i launch a 1 line main that invokes the working main it doesnt work (in this case the main is in the root project).
-the dskV project has all the libraries required to make the native code work(and in fact it works), the root project has the dskV project in its build path. and to make sure also has all the libraries of dskV (tested with them and without, im desperate). i tried both
System.setProperty("jna.library.path","C:/Users/bbb/Desktop/aaa/dskV/video/library/windows64/plugins/" ); and
System.setProperty("jna.library.path","C:/Users/bbb/Desktop/aaa/dskRoot/video/library/windows64/plugins/" ); after copying the natives in the root project. in all possible places.
what can be this jna starting project related issue?
How to list all COM ports in Combobox or Listbox and allow user to select the desired COM Port
How to list all COM ports in Combobox or Listbox and allow user to select the desired Port speed.
Motion Detection
What's wrong with my code. When I run the code, it pops out a grey windows nothing inside there. The camera should be activated and detect motion in the screen
import processing.video.*;
int numPixels;
int[] previousFrame;
Capture video;
void setup(){
size (640, 480);
video = new Capture (this, width, height);
numPixels = video.width * video.height;
previousFrame = new int[numPixels];
video.start();
}
void draw() {
if (video.available()){
video.read();
video.loadPixels();
int movementSum=0;
loadPixels();
for (int i = 0; i < numPixels; i++){
color currColor = video.pixels[i];
color prevColor = previousFrame[i];
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int prevR = (currColor >> 16) & 0xFF;
int prevG = (currColor >> 8) & 0xFF;
int prevB = currColor & 0xFF;
int diffR = abs(currR - prevR);
int diffG = abs(currG - prevG);
int diffB = abs(currB - prevB);
movementSum += diffR + diffG + diffB;
pixels[i] = color(diffR,diffG,diffB);
previousFrame[i] = currColor;
}
if (movementSum > 0){
updatePixels();
println(movementSum);
}
}
}
How to Change the Text of Button
Following is the code snippet
`cp5.addButton("Login")
.setPosition(675, 10)
.setSize(100, 70)
.setFont(myFont)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
; `
Is there a way to change the text "Login" to "Logout" or vice-versa?
Newbie seeking help re. combining example code (& using a Leapmotion)
First up, hello everybody. I'm new, both to the forum, and to processing, but despite my very limited experience, what an awesome program - It's so exciting to tinker with!
I'm using processing 3.2.3, and for now, I'm learning the basics through modifying example code provided via example librarys, which is proving to be a slow but valuable learning experience.
I've now got a little project where I want to combine 2 parts of example code, and I've hit a road block - I simply can't seem to get it to work.
There is a nice SineWave demo in the official sound library (libraries/sound/examples/Oscillators/SineWave) which generates a tone, that tracks your mouse position and changes frequency, amplitude and panning based on your mouse position.
The second is from an example provided as part of the Library item: Leapmotion | Forwards Leap Motion controller events to a processing sketch by Michael Heuer, specifically, the leap_position_example. This example tracks finger tip position provided by the leap, and displays them in a processing window as circular vectors of a random colour.
What I would like to do, is to combine both parts of code - I'd like to bring the SineWave code into the Leap example and then map one of the values provided by the leap (Wrist/palm position ideally - anybody know how to visually display this also?) to SineWave code so that you are able to control the frequency etc. of the sound with you hard movements rather than the mouse. Like a simple theremin!
I'm clearly missing something fundamental, because no matter how I try to format it, I can't get both examples of working in the same processing window. As such, any help would be greatly appreciated - Thank you!
Rather than provide my numerous examples butchered code for you to untangle, here are the two examples of untouched code that I am hoping to use:
SOUND:
import processing.sound.*;
SinOsc sine;
float freq=400;
float amp=0.5;
float pos;
void setup() {
size(800, 600);
background(255);
// Create and start the sine oscillator.
sine = new SinOsc(this);
//Start the Sine Oscillator.
sine.play();
}
void draw() {
// Map mouseY from 0.0 to 1.0 for amplitude
amp=map(mouseY, 0, height, 1.0, 0.0);
sine.amp(amp);
// Map mouseX from 20Hz to 1000Hz for frequency
freq=map(mouseX, 0, width, 80.0, 1000.0);
sine.freq(freq);
// Map mouseX from -1.0 to 1.0 for left to right
pos=map(mouseX, 0, width, -1.0, 1.0);
sine.pan(pos);
}
LEAP:
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Tool;
import com.leapmotion.leap.Vector;
import com.leapmotion.leap.processing.LeapMotion;
LeapMotion leapMotion;
ConcurrentMap<Integer, Integer> fingerColors;
ConcurrentMap<Integer, Integer> toolColors;
ConcurrentMap<Integer, Vector> fingerPositions;
ConcurrentMap<Integer, Vector> toolPositions;
void setup()
{
size(800, 600);
background(20);
frameRate(60);
ellipseMode(CENTER);
leapMotion = new LeapMotion(this);
fingerColors = new ConcurrentHashMap<Integer, Integer>();
toolColors = new ConcurrentHashMap<Integer, Integer>();
fingerPositions = new ConcurrentHashMap<Integer, Vector>();
toolPositions = new ConcurrentHashMap<Integer, Vector>();
}
void draw()
{
fill(20);
rect(0, 0, width, height);
for (Map.Entry entry : fingerPositions.entrySet())
{
Integer fingerId = (Integer) entry.getKey();
Vector position = (Vector) entry.getValue();
fill(fingerColors.get(fingerId));
noStroke();
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 24.0, 24.0);
}
for (Map.Entry entry : toolPositions.entrySet())
{
Integer toolId = (Integer) entry.getKey();
Vector position = (Vector) entry.getValue();
fill(toolColors.get(toolId));
noStroke();
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 24.0, 24.0);
}
}
void onFrame(final Controller controller)
{
Frame frame = controller.frame();
fingerPositions.clear();
for (Finger finger : frame.fingers())
{
int fingerId = finger.id();
color c = color(random(0, 255), random(0, 255), random(0, 255));
fingerColors.putIfAbsent(fingerId, c);
fingerPositions.put(fingerId, finger.tipPosition());
}
toolPositions.clear();
for (Tool tool : frame.tools())
{
int toolId = tool.id();
color c = color(random(0, 255), random(0, 255), random(0, 255));
toolColors.
(toolId, c); toolPositions.put(toolId, tool.tipPosition()); } }
Unable to Insert Record in MySQL
I am unable to Insert Record in the MySQL. Processing 2.2.1. The error message is as follows
Avoid Exception Error on msql.query()
The following Code Snippet gives run-time error
//===Read From MySQL==========================
if (msql.connect()) {
//int recCount = msql.query("SELECT * FROM %s WHERE %s IS NOT NULL", "interface", "ToSerial");
//if (recCount > 0) {
msql.query("SELECT * FROM %s WHERE %s IS NOT NULL", "interface", "ToSerial");
try {
String serialTxt = msql.getString("ToSerial");
if (serialTxt != null) {
cp5.get(Textfield.class, "Transmit").setText(serialTxt);
int deleteID = msql.getInt("ID");
msql.query( "DELETE FROM %s WHERE %s = %s", "interface", "ID", deleteID );
}
}
catch (RuntimeException ex) {
// println(ex);
}
}
The Error on the Processing 2.2.1 IDE is as follows:-
SQL.getString(): java.sql.SQLException.
java.sql.SQLException: Illegal operation on empty result set.
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:693)
at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:4832)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:4810)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:4826)
at de.bezier.data.sql.SQL.getString(Unknown Source)
at CIOMSerial.draw(CIOMSerial.java:107)
at processing.core.PApplet.handleDraw(PApplet.java:2386)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Unknown Source)
After sometime, it fails to connect to the database. I couldn't find a way to check if the Query() has returned any records or not. Please anyone, kindly provide a way out.
Link Facebook to processing
Hey guys!
So this is kind of a long shot but here it goes.
I wanted to make an artwork that uses data from your Facebook account.
The whole idea is that is uses: https://www.facebook.com/ads/preferences/?entry_product=education_page
and generates an artwork or something.
Im pretty decent at programming but have no idea how to start.
I know there are some API's available but i'm not sure where to look.
Perhaps I could use the XML page or something?
Anyone knows how I can get on the right track? Greetings!
Track shape and copy image
hello, i want to make a program that tracks some shapes in a paper from the camera and then copies the content of the paper to an image to my drive,something like qrcode or opencv. Does anyone have any idea how to approach it?
Minim setGain/probably my fault
Hello guys!! I'm new to this forum :) !! And I'm new to processing in general.....and in this code I have a problem with minim "setGain()". I'm trying to triggering a piano note every time the play bar is hitting a rectangle with "setGain(0) and play(0)" (the length of rectangles represent the duration of the piano note duration) and muting/stopping the sound when the play bar is hitting the end of a rectangle with "setGain(-200)" (I've tried also with "setVolume()").
The problem is: there are two rectangles for now and both duration always refer to the length of the first rectangle(when "setGain(-200)") , the second sound (combined with the second rectangle) is not happening at all, or, it happing with the length of the first rectangle :((
Is there anybody who can help me 8-| ? Where am I doing wrong :-? ?
p.s. the rectangle are created randomly so you have to click every time on "play" to make a different combination between the two rectangles.
import ddf.minim.*;
Minim minim;
AudioPlayer piano_C3;
AudioPlayer piano_Cdiesis3;
AudioPlayer piano_D3;
AudioPlayer piano_Ddiesis3;
AudioPlayer piano_E3;
AudioPlayer piano_F3;
AudioPlayer piano_Fdiesis3;
AudioPlayer piano_G3;
AudioPlayer piano_Gdiesis3;
AudioPlayer piano_A3;
AudioPlayer piano_Adiesis3;
AudioPlayer piano_B3;
AudioPlayer piano_C4;
float menusp;
float noteX1;
float noteX2;
float noteY1;
float noteY2;
int noteAlpha1;
int noteAlpha2;
int sceltoX1;
int sceltoX2;
int sceltoY1;
int sceltoY2;
int sceltoAlpha1;
int sceltoAlpha2;
float movelineX;
float Ktempo;
float PX1I;
float PX2I;
float fixedNoteY;
float lenght1;
float lenght2;
float lenghtTot;
void setup () {
size (450, 750);
rectMode (CENTER);
minim=new Minim (this);
piano_C3=minim.loadFile ("C3.mp3");
piano_Cdiesis3=minim.loadFile ("Cdiesis3.mp3");
piano_D3=minim.loadFile ("D3.mp3");
piano_Ddiesis3=minim.loadFile ("Ddiesis3.mp3");
piano_E3=minim.loadFile ("E3.mp3");
piano_F3=minim.loadFile ("F3.mp3");
piano_Fdiesis3=minim.loadFile ("Fdiesis3.mp3");
piano_G3=minim.loadFile ("G3.mp3");
piano_Gdiesis3=minim.loadFile ("Gdiesis3.mp3");
piano_A3=minim.loadFile ("A3.mp3");
piano_Adiesis3=minim.loadFile ("Adiesis3.mp3");
piano_B3=minim.loadFile ("B3.mp3");
piano_C4=minim.loadFile ("C4.mp3");
menusp=height-(height/4);
fixedNoteY=menusp/12;
float[] dataY1={(height-fixedNoteY), (height-(fixedNoteY*2)),
(height-(fixedNoteY*3)), (height-(fixedNoteY*4)),
(height-(fixedNoteY*5)), (height-(fixedNoteY*6)),
(height-(fixedNoteY*7)), (height-(fixedNoteY*8)),
(height-(fixedNoteY*9)), (height-(fixedNoteY*10)),
(height-(fixedNoteY*11)), (height-(fixedNoteY*12))};
sceltoY1=int(random(dataY1.length));
noteY1=(dataY1 [sceltoY1]);
float[] dataY2={(height-fixedNoteY), (height-(fixedNoteY*2)),
(height-(fixedNoteY*3)), (height-(fixedNoteY*4)),
(height-(fixedNoteY*5)), (height-(fixedNoteY*6)),
(height-(fixedNoteY*7)), (height-(fixedNoteY*8)),
(height-(fixedNoteY*9)), (height-(fixedNoteY*10)),
(height-(fixedNoteY*11)), (height-(fixedNoteY*12))};
sceltoY2=int(random(dataY2.length));
noteY2=(dataY2 [sceltoY2]);
int[] dataX1={4, 8, 16};
sceltoX1=int(random(dataX1.length));
noteX1=(dataX1 [sceltoX1]);
int[] dataX2={4, 8, 16};
sceltoX2=int(random(dataX2.length));
noteX2=(dataX2 [sceltoX2]);
int[] alpha1 ={255, 0, 255, 0};
sceltoAlpha1=int(random(alpha1.length));
noteAlpha1=(alpha1[sceltoAlpha1]);
int[] alpha2 ={255, 0, 255, 0};
sceltoAlpha2=int(random(alpha2.length));
noteAlpha2=(alpha2[sceltoAlpha2]);
lenght1=width/noteX1;
PX1I=0;
lenght2=(width/noteX2);
PX2I=(width/16);
}
void draw () {
background (255);
//triggering note 1
if (movelineX==PX1I && noteY1==(height-fixedNoteY) && noteAlpha1!=0 ) { //C3
piano_C3.setGain(0);
piano_C3.play(0);
}
if (movelineX>lenght1) {
piano_C3.setGain(-200);
} else {
piano_C3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(2*fixedNoteY)) && noteAlpha1!=0 ) { //Cdiesis3
piano_Cdiesis3.setGain(0);
piano_Cdiesis3.play(0);
}
if (movelineX>lenght1) {
piano_Cdiesis3.setGain(-200);
} else {
piano_Cdiesis3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(3*fixedNoteY)) && noteAlpha1!=0 ) { //D3
piano_D3.setGain(0);
piano_D3.play(0);
}
if (movelineX>lenght1) {
piano_D3.setGain(-200);
} else {
piano_D3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(4*fixedNoteY)) && noteAlpha1!=0 ) { //Ddiesis3
piano_Ddiesis3.setGain(0);
piano_Ddiesis3.play(0);
}
if (movelineX>lenght1) {
piano_Ddiesis3.setGain(-200);
} else {
piano_Ddiesis3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(5*fixedNoteY)) && noteAlpha1!=0 ) { //E3
piano_E3.setGain(0);
piano_E3.play(0);
}
if (movelineX>lenght1) {
piano_E3.setGain(-200);
} else {
piano_E3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(6*fixedNoteY)) && noteAlpha1!=0 ) { //F3
piano_F3.setGain(0);
piano_F3.play(0);
}
if (movelineX>lenght1) {
piano_F3.setGain(-200);
} else {
piano_F3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(7*fixedNoteY)) && noteAlpha1!=0 ) { //Fdiesis3
piano_Fdiesis3.setGain(0);
piano_Fdiesis3.play(0);
}
if (movelineX>lenght1) {
piano_Fdiesis3.setGain(-200);
} else {
piano_Fdiesis3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(8*fixedNoteY)) && noteAlpha1!=0 ) { //G3
piano_G3.setGain(0);
piano_G3.play(0);
}
if (movelineX>lenght1) {
piano_G3.setGain(-200);
} else {
piano_G3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(9*fixedNoteY)) && noteAlpha1!=0 ) { //Gdiesis3
piano_Gdiesis3.setGain(0);
piano_Gdiesis3.play(0);
}
if (movelineX>lenght1) {
piano_Gdiesis3.setGain(-200);
} else {
piano_Gdiesis3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(10*fixedNoteY)) && noteAlpha1!=0 ) { //A3
piano_A3.setGain(0);
piano_A3.play(0);
}
if (movelineX>lenght1) {
piano_A3.setGain(-200);
} else {
piano_A3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(11*fixedNoteY)) && noteAlpha1!=0 ) { //Adiesis3
piano_Adiesis3.setGain(0);
piano_Adiesis3.play(0);
}
if (movelineX>lenght1) {
piano_Adiesis3.setGain(-200);
} else {
piano_Adiesis3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(12*fixedNoteY)) && noteAlpha1!=0 ) { //B3
piano_B3.setGain(0);
piano_B3.play(0);
}
if (movelineX>lenght1) {
piano_B3.setGain(-200);
} else {
piano_B3.setGain(0);
}
if (movelineX==PX1I && noteY1==(height-(13*fixedNoteY)) && noteAlpha1!=0 ) { //C4
piano_C4.setGain(0);
piano_C4.play(0);
}
if (movelineX>lenght1) {
piano_C4.setGain(-200);
} else {
piano_C4.setGain(0);
}
//triggering note2
if (movelineX==PX2I && noteY2==(height-fixedNoteY) && noteAlpha2!=0 ) { //C3
piano_C3.setGain(0);
piano_C3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_C3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(2*fixedNoteY)) && noteAlpha2!=0 ) { //Cdiesis3
piano_Cdiesis3.setGain(0);
piano_Cdiesis3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_Cdiesis3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(3*fixedNoteY)) && noteAlpha2!=0 ) { //D3
piano_D3.setGain(0);
piano_D3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_D3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(4*fixedNoteY)) && noteAlpha2!=0 ) { //Ddiesis3
piano_Ddiesis3.setGain(0);
piano_Ddiesis3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_Ddiesis3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(5*fixedNoteY)) && noteAlpha2!=0 ) { //E3
piano_E3.setGain(0);
piano_E3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_E3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(6*fixedNoteY)) && noteAlpha2!=0 ) { //F3
piano_F3.setGain(0);
piano_F3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_F3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(7*fixedNoteY)) && noteAlpha2!=0 ) { //Fdiesis3
piano_Fdiesis3.setGain(0);
piano_Fdiesis3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_Fdiesis3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(8*fixedNoteY)) && noteAlpha2!=0 ) { //G3
piano_G3.setGain(0);
piano_G3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_G3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(9*fixedNoteY)) && noteAlpha2!=0 ) { //Gdiesis3
piano_Gdiesis3.setGain(0);
piano_Gdiesis3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_Gdiesis3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(10*fixedNoteY)) && noteAlpha2!=0 ) { //A3
piano_A3.setGain(0);
piano_A3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_A3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(11*fixedNoteY)) && noteAlpha2!=0 ) { //Adiesis3
piano_Adiesis3.setGain(0);
piano_Adiesis3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_Adiesis3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(12*fixedNoteY)) && noteAlpha2!=0 ) { //B3
piano_B3.setGain(0);
piano_B3.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_B3.setGain(-100);
}
if (movelineX==PX2I && noteY2==(height-(13*fixedNoteY)) && noteAlpha2!=0 ) { //C4
piano_C4.setGain(0);
piano_C4.play(0);
}
if (movelineX>(lenght1+lenght2)) {
piano_C4.setGain(-100);
}
// orizontal lines
stroke (1);
strokeWeight (1);
for (float ia = 0; ia < 750; ia = ia+(menusp/12)) {
line (0, ia, 450, ia);
}
// vertical lines
for (float ib = 0; ib < 450; ib = ib+(width/16)) {
line (ib, 0, ib, 750);
}
//note 1
fill (24, 0, 191, noteAlpha1);
if (noteAlpha1==0) {
stroke (0, 0, 0, 0);
} else {
stroke (1);
}
rectMode (CORNER); // modalità corner rect (x1,y1,x2,y2)
rect (PX1I, noteY1, lenght1, fixedNoteY);
rectMode (CENTER); // ritorna la modalità centro
//note 2
fill (255, 0, 1, noteAlpha2);
if (noteAlpha2==0) {
stroke (0, 0, 0, 0);
} else {
stroke (1);
}
rectMode (CORNER);
rect (PX2I, noteY2, lenght2, fixedNoteY);
rectMode (CENTER); // ritorna la modalità centro
// play bar
strokeWeight (5);
stroke (#FF05DE);
Ktempo=1;
movelineX=movelineX+Ktempo;
if (movelineX>=width) {
movelineX=0;
piano_C3.setGain(0);
piano_Cdiesis3.setGain(0);
piano_D3.setGain(0);
piano_Ddiesis3.setGain(0);
piano_E3.setGain(0);
piano_F3.setGain(0);
piano_Fdiesis3.setGain(0);
piano_G3.setGain(0);
piano_Gdiesis3.setGain(0);
piano_A3.setGain(0);
piano_Adiesis3.setGain(0);
piano_B3.setGain(0);
piano_C4.setGain(0);
}
line(0+movelineX, 0, 0+movelineX, height);
strokeWeight (1);
stroke (1);
strokeWeight (1);
noStroke ();
fill (0, 220, 141);
rect (width/2, (height/8), width, ((height/4)+2));
}
How to auto post on Twitter by everyday
I am doing a task for project . This is using API twitter to auto post on Twitter .Everyday select 3 -4 post after auto post on Twitter . Can I help you ? Thanks so lot !