Is there any simple library to get gui functionalities like text fields, text areas in processing? . I have written a pong game in processing and I want text fields to get the name of the players.
In the code below I wanted a text field to get names from the players
here is my code
int butt=0;
float velx=-2;
float vely=-2;
float x=350,y=200;
int s=150;
int score1=0,score2=0;
float speed = 1;
float speed_cont = 0.0001;
void setup(){
size(700,400);
smooth();
background(100,200,100);
}
void draw(){
stroke(255);
strokeWeight(15);
fill(100,100,200);
rect(175,100,340,100);
fill(255);
textSize(38);
text("CLICK TO PALY",200,160);
fill(0);
textSize(30);
text("Done By BOBO(TARUN GOVIND KESHAV)",50,300);
textSize(18);
text("mail: tarun.83581#gmail.com",50,330);
noStroke();
if (mousePressed && (mouseButton == LEFT)) {
butt=1;
}
if(butt==1){
background(210);
textSize(32);
text("score :", 100,30);
text(score1,300,30);
text(":",400,30);
text(score2,500,30);
x = x+velx*speed;
y = y+vely*speed;
speed = speed +speed_cont; // speed control value
if(x<=75){
x=350;y=200;score2+=1;
}
if((x>=625)){
x=350;y=200;score1+=1;
}
if((x<=80)&(y>s)&(y<(s+80))){
velx = -velx;
}
if((y>=380)||(y<=20)){
vely = -vely;
}
if((x>=620)&(y>mouseY)&(y<(mouseY+80))){
velx = -velx;
}
ball(x,y);
slider(s);
slider1(mouseY);
}
}
void ball(float x,float y){
fill(0);
arc(x,y,40,40,0,2*PI);
}
void slider(int y){
fill(200,100,100);
rect(20,y,40,80);
}
void keyPressed(){
if(keyCode == UP){s=s-15;}
if(keyCode == DOWN){s = s+15;}
}
void slider1(int y){
fill(200,100,100);
rect(640,y,40,80);
}
On the processing.org website there are 3 GUI libraries listed on the libraries page (https://processing.org/reference/libraries/)
I've worked with http://www.sojamo.de/libraries/controlP5/ before, it's not to hard to get running I will cover your needs.
Related
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)
I am making an animation in Processing it is a simulation of a ball going up and coming down, I have an error saying 'NullPointerException' it keeps appearing I will give the code and show the error place:
Obj[] objs = new Obj[100];
void setup(){
size(1000, 1000);
}
void draw(){
background(250);
for(int i = 0; i < objs.length; i++){
objs[i].render();
objs[i].up();
objs[i].run();
}
}
the error is at objs[i].render();
here is the code for Obj:
class Obj{
float x;
float y;
float speed;
float pspeed;
float velocity;
void render(){
fill(0);
ellipse(x, y, 5, 5);
}
void up(){
x = random(-1000, 1000);
y = y-1010;
}
void run(){
speed = random(4, 6);
pspeed = speed;
velocity = 0.05;
while(y<random(700, 1100)){
y=y-speed;
speed=speed-velocity;
}
while(y>0){
y=y+pspeed;
pspeed = pspeed - velocity;
}
}
}
This line creates an array that can hold 100 instances of Obj:
Obj[] objs = new Obj[100];
But it doesn't actually create any instances. In other words, your array contains 100 null values.
Then you're looping through those null values and trying to call functions on them, which is what's causing your error.
To fix the problem, you probably just want to fill your array up with instances:
void setup(){
size(1000, 1000);
for(int i = 0; i < objs.length; i++){
objs[i] = new Obj();
}
}
Shameless self-promotion: I wrote a tutorial on arrays in Processing available here.
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();
}
}
For a project I made a code that creates a computer wallpaper with icons. One Icon I set to draw a loading bar when clicked (void mousePressed). I want to be able to see the rectangle(loading bar)start at a determined location using RectMode(CORNER) and have the width increase every few seconds until the bar is about 3/4 full and then stop and remain.
please give suggestions
this draws the finished bar but i want to see each increment for a couple seconds
void setup(){
size(800,600);
}
void mousePressed(){
if (mousePressed && mouseX>width/4 && mouseX<width-width/4 && mouseY>height/3 && mouseY<height- height/3){
rectMode(CORNER);
noStroke();
fill(0,180,0,180);
for( int r = 0; r <= 7; r++){
if (r == 1)
i = 50;
rect(width/2-348,height/2-35,i,height/8-4);
if (r == 2)
i = 150;
rect(width/2-348,height/2-35,i,height/8-4);
if (r == 3)
rect(width/2-348,height/2-35,i,height/8-4);
i = 250;
if (r == 4)
rect(width/2-348,height/2-35,i,height/8-4);
i = 350;
if (r == 5)
rect(width/2-348,height/2-35,i,height/8-4);
i = 450;
if (r == 6)
rect(width/2-348,height/2-35,i,height/8-4);
i = 550;
if (r == 7)
rect(width/2-348,height/2-35,i,height/8-4);
i = 650;
}
}
}
Using loop is in processing defined by function loop() and can be stopped by noLoop() also I am using frameCount (contains the number of frames that have been displayed since the program started - each run of draw() function) to count percentage of loaded progress bar and to stop loading on 3/4 as you want.
boolean loading = false;
int fillX = 0;
void setup()
{
size(300,300);
background(0);
noLoop();
}
void draw()
{
stroke(255);
fill(0);
rect(48, 137, 204, 25);
noStroke();
fill(255);
rect(51, 140, fillX, 20);
if(loading == true)
{
fillX = ((frameCount%301) / 3 * 2);
if(frameCount%(300*0.75) == 0)
{
loading = false;
noLoop();
frameCount = 0;
}
}
}
void mousePressed() {
loading = true;
loop();
}
RectMode(CORNER) is default mode so the is no need to specify it unless you are using different modes within project.
Do you want to do something like this?
int time, myWidth;
boolean loading;
void setup(){
size(800,600);
loading = false;
myWidth = 0;
}
void draw(){
drawLoadingBar();
}
void drawLoadingBar(){
if(myWidth < width/3){
if(loading && millis()-time > 1000){
rect(20, height/2, myWidth, 30);
myWidth = myWidth + 10;
time = millis();
}
}
}
void mousePressed(){
if(loading == false){
time = millis();
loading = true;
}
}
This code works by increasing the bar width by 10 every second after the first mouse click.
I am trying to draw a number of shapes in processing which react to each others movements. So when I move one of them, it is connected to say 1 or 2 of the other shapes in the sketch. However I don't want it to be a static movement, I want it to look quite natural.
The only code I have at the moment is basic drawing of a shape which moves when you drag it, but have no idea how to link shapes :( Help!!
float bx;
float by;
int boxSize = 75;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
void setup()
{
size(640, 360);
bx = width/2.0;
by = height/2.0;
rectMode(RADIUS);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > bx-boxSize && mouseX < bx+boxSize &&
mouseY > by-boxSize && mouseY < by+boxSize) {
overBox = true;
}
// Draw the box
rect(bx, by, boxSize, boxSize);
}
void mousePressed() {
if(overBox) {
locked = true;
} else {
locked = false;
}
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
void mouseDragged() {
if(locked) {
bx = mouseX-xOffset;
by = mouseY-yOffset;
}
}
void mouseReleased() {
locked = false;
}
I am thinking I need to use PShape, but am unsure.
First of all, I recommend you to use a class for the shapes, that way you can link them easily.
I make some changes in your code in order to get some kind of following behaviour.
class Box{
float bx;
float by;
int size = 75;
Box(float bx, float by){
this.bx = bx;
this.by = by;
}
void drawBox(){
rect(bx, by, size, size);
}
void moveBox(float x, float y){
bx = bx + x;
by = by + y;
}
}
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
Box box, secondBox, thirdBox;
void setup()
{
size(640, 360);
box = new Box(width/2.0, height/2.0);
secondBox = new Box(100, 100);
thirdBox = new Box(500, 300);
rectMode(RADIUS);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > box.bx-box.size && mouseX < box.bx+box.size &&
mouseY > box.by-box.size && mouseY < box.by+box.size) {
overBox = true;
box.drawBox();
secondBox.drawBox();
thirdBox.drawBox();
}
}
void mousePressed() {
if(overBox) {
locked = true;
} else {
locked = false;
}
xOffset = mouseX-box.bx;
yOffset = mouseY-box.by;
}
void mouseDragged() {
if(locked) {
box.bx = mouseX-xOffset;
box.by = mouseY-yOffset;
deltaMovemente(box, secondBox);
deltaMovemente(box, thirdBox);
}
}
void mouseReleased() {
locked = false;
}
void deltaMovemente(Box follow, Box box){
float dx = (follow.bx - box.bx)/50;
float dy = (follow.by - box.by)/50;
box.moveBox(dx, dy);
}
Hope this can be useful.
Regards.