I'm using Processing to make something and basically my keyDown() is not working. It supposed to be triggered when any key is pressed but the function is not being called. Code below:
int playerno=0; //determines player
boolean ready=true;
void setup() {
size(700, 700);
background(#FFFFFF);
fill(#000000);
textSize(50);
text("Press Any Key To Start", 350, 350);
}
void keyPressed() {
if (ready) {
fill(#FFFFFF);
rect(350, 350, 200, 100);
fill(#000000);
textSize(50);
text("Game Ready", 350, 350);
boolean ready=false;
}
}
This will won't work without draw function. Also you are declaring new local variable ready inside keypressed() this is bad mistake. Try move your drawing code from "keyDown()" into "drawing" like this:
void draw() {
if (ready == false) {
background(#FFFFFF); //This is needed for redrawing whole scene
fill(#FFFFFF);
rect(350, 350, 200, 100);
fill(#000000);
textSize(50);
text("Game Ready", 350, 350);
}
}
void keyPressed() {
if (ready) {
ready=false;
}
}
Related
I am doing an audio visualisation application that changes the size of a shape based on the values of meyda's zcr feature. However once the shape is drawn, it still leaves a traces of the previous value on the canvas. Is the a way I can remove these traces?
I assigned the zcr values to the shapes height however the drawn shape does not refresh its appearance even though the values are changed.
function setup() {
createCanvas(800,600);
background(180);
playStopButton = createButton('play');
playStopButton.position(20, 20);
playStopButton.mousePressed(playStopSound);
amplitude = new p5.Amplitude();
rectH = 0;
if (typeof Meyda === "undefined"){
console.log("meyda could not be found");
} else {
analyzer = Meyda.createMeydaAnalyzer({
"audioContext": getAudioContext(),
"source": mySound,
"bufferSize": 512,
"featureExtractors": ["zcr"],
"callback": features => {
console.log(features);
rectH = features.zcr;
}
});
}
}
function draw() {
rectMode(RADIUS);
rect(80,300, 80, rectH);
console.log(rectH);
}
draw and set up function for reference.
Move the a 'background(180);' from the setup() to the draw() function.
Like this:
function draw() {
background(180);
rectMode(RADIUS);
rect(80,300, 80, rectH);
console.log(rectH);
}
Using Cp5, I'm attempting to create a textfield. I want the user to only be able to edit the textfield after clicking a button. The user is unable to edit the textfield if the button is not pressed.
Does Textfield have a method that can assist me with this function?
I am asking because I didn't find any documentation for the methods in the library.
setLock() should do what you want to do:
import controlP5.*;
ControlP5 cp5;
String textValue = "";
public void lock() {
cp5.get(Textfield.class, "textField").setLock(true);
}
public void unlock() {
cp5.get(Textfield.class, "textField").setLock(false);
}
void setup() {
size(700, 400);
background(0);
fill(255);
PFont font = createFont("arial", 20);
cp5 = new ControlP5(this);
cp5.addTextfield("textField")
.setPosition(60, 100)
.setSize(150, 30)
.setFont(createFont("arial", 18))
.setAutoClear(false)
.setLock(false);
;
cp5.addBang("lock")
.setPosition(240, 100)
.setSize(60, 30)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addBang("unlock")
.setPosition(330, 100)
.setSize(60, 30)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
textFont(font);
}
void draw() {
}
Why I can use size(1920, 1080) in setup()
but If I use
setup()
visualContext = new VisualContext(
new Area(0, 0, 1920, 1080),
new Area(158, 150, 1340, 950)
);
size(visualContext.getGlobalArea().getWidth(), visualContext.getGlobalArea().getHeight());
There will be an error
When not using the PDE, size() can only be used inside settings().
Remove the size() method from setup(), and add the following:
public void settings() {
size(1920, 1080);
}
I can not find any doc about this topic.
Curious, can size only be initialized by constant but variable?
use settings if you want to invoke size with parameters.
VisualContext visualContext = new VisualContext(
new Area(0, 0, 1920, 1080),
new Area(158, 150, 1340, 950)
);
void settings() {
size(visualContext.getGlobalArea().getWidth(), visualContext.getGlobalArea().getHeight()); //<>//
}
https://processing.org/reference/settings_.html
Hi if anyone could help me out i would be very grateful, i have a sketch that will enable a user to draw graffiti on a screen with a wireless spray can. At the minute, with the tuio code installed, when the user presses the mouse button, a spray sound is made.. but i am having difficulty in the sketch creating an ellipse when presses the mouse button.
This is my code;
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer player;
AudioInput input;
/*TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You shgould have greceived a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
TuioProcessing tuioClient;
import java.util.*; //ADD THIS LINE TO BE ABLE TO USE TUIOCLIENT WITH PROCESSING 2+
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;
//declare a boolean to check mouse click
boolean drag = false;
int n=0;
int size[]= {20,40};
int sizeChosen;
boolean inside = false;
PImage bg;
PImage img;
PGraphics pg;
import controlP5.*;
ControlP5 cp5;
boolean mp = true;
void setup ()
{
size(1000,1000);//size(screen.width, screen.height).
smooth();
noStroke();
fill(0);
loop();
frameRate(30);
hint(ENABLE_NATIVE_FONTS);
font = createFont("Arial",18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
ellipseMode( CENTER);
//smooth();
noCursor();
background(170);
bg = loadImage("brickwall.jpg");
background(bg);
img = loadImage("instructions.jpg");
image (img,30,40,THUMB_SIZE, THUMB_SIZE);
cp5 = new ControlP5(this);//screenshot button
cp5.addButton("Save Graffiti Artwork").setPosition(0,650).setSize(200,100);//screenshot details
minim = new Minim(this);
player = minim.loadFile("spray_close.wav");
input = minim.getLineIn();
}
void draw() {
if (mp = true);
ellipse(255,0,255,0);
background(bg);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
noStroke();
fill(0);
pushMatrix();
translate(tobj.getScreenX(width), tobj.getScreenY(height));
rotate(tobj.getAngle());
// ellipse(-obj_size/2, -obj_size/2, obj_size, obj_size);
popMatrix();
//fill(255);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
if (mousePressed) {
ellipse(255,0,255,0);
// printIn("Pink");
//mouse press 1
if (tobj.getSymbolID()==12) {
ellipse(255,0,255,0);
}
}
}
Vector tuioCursorList = tuioClient.getTuioCursors();
for (int i=0;i<tuioCursorList.size();i++) {
TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
Vector pointList = tcur.getPath();
if (pointList.size()>0) {
stroke(0, 0, 255);
TuioPoint start_point = (TuioPoint)pointList.firstElement();
;
for (int j=0;j<pointList.size();j++) {
TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
line(start_point.getScreenX(width), start_point.getScreenY(height), end_point.getScreenX(width), end_point.getScreenY(height));
start_point = end_point;
}
stroke(192, 192, 192);
fill(192, 192, 192);
ellipse( tcur.getScreenX(width), tcur.getScreenY(height), cur_size, cur_size);
fill(0);
text(""+tcur.getCursorID(), tcur.getScreenX(width)-5, tcur.getScreenY(height)+5);
}
}
if (drag) //if drag = true, i-e if mouse click is holding, ellipse are drawing according the mouse's position
{
fill(#FF00FF); //black color
ellipse(mouseX, mouseY, 50,50); //draw ellipse with x and y mouse's position + size 10*10
//or line strokeWeight(3);stroke(0);line(mouseX,mouseY,25,25);
}
//draw palette size
for(n=0;n<2;n++)
{
fill(0);
ellipse(360,10+n*40,20*(n+1),20*(n+1));
}
}
//size selector
void mousePressed() {
//bDrawFullSize = true;
if (inside==true){
sizeChosen=size[n];
}
player.play();
mp = true;
}
void mouseReleased() {
//bDrawFullSize = true;
drag = false;
player.close();
//since close closes the file, we need to load the sound effect again.
player = minim.loadFile("spray_close.wav");
}
//function "drag and drop"
void mouseDragged() {
drag = true;
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur) {
println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
}
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {
println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {
println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void keyPressed() {
endRecord();
background(bg);
// exit();
}
public void saveScreen() {
saveFrame();
player.pause();
}
// returns true if mouse is inside this rectangle
boolean inside(int left, int top, int right, int bottom ) {
if (mouseX>left && mouseX<right && mouseY>top && mouseY<bottom ) {
return true;
}
else {
return false;
}
}
Anything under the function:
void mousePressed() {
//bDrawFullSize = true;
if (inside==true){
sizeChosen=size[n];
}
player.play();
mp = true;
}
is run every frame while mouse is pressed. So you would create an ellipse (or whatever you want) here.
You can put the ellipse generation function inside branching condition that's checking on the mouse pressed state:
if (mousePressed) {
ellipse(random(0,width),random(0,height),random(0,100),random(0,100));
}
put the condition at the end of the Draw function so that it overwrites any background element
I want to add/remove an ellipse using a press of a button in Processing. I use void keyPressed() and void keyReleased() for the keys. But how can I use if statements to add/remove an ellipse?
Now that i can add more , and remove some ellipses. For example, if i have one i can press a button then have one more. Or press a button to remove one. But now I would like to move those ellipses (max 4) using different key sets for each of them. How can i do that?
What you need to do is have a boolean to be read whenever the ellipse is to be drawn. If that is true draw it, if it not don't! THe keypress should just switch that boolean. Like this:
boolean iShouldDrawTheEllipse = true;
void draw() {
background(0);
if(iShouldDrawTheEllipse) ellipse(50,50,10,10);
}
void keyPressed() {
iShouldDrawTheEllipse = !iShouldDrawTheEllipse;
}
You can use some booleans to determinate which ellipse you have to print and which not.
E.G.
void keyPressed(){
switch(keyCode){
case 'z':
drawingFirstEllipse = !drawingFirstEllipse;
break;
case 'x':
drawingSecondEllipse = !drawingSecondEllipse;
break;
case 'c':
drawingThirdEllipse = !drawingThirdEllipse;
break;
case 'v':
drawingFourthEllipse = !drawingFourthEllipse;
break;
}
}
Then in your void draw(void) method you can easily draw the ellipses:
void draw(){
background(0);
if(drawingFirstEllipse) ellipse(50, 50, 10, 10);
if(drawingSecondEllipse) ellipse(50, 50, 70, 10);
if(drawingThirdEllipse) ellipse(50, 50, 130, 10);
if(drawingFourthEllipse) ellipse(50, 50, 190, 10);
}
I hope I helped [;