How to use mousePressed() for 2 different events in processing? - processing

I am working on my university project and came across an issue. I am creating a visualization for some data and want to use mousePressed() for different events, but when I try to add another event to mousePressed() it will not run as the code does not know which event to run (when the mouse is pressed it fires both events).
I made an event that allows the user to click the main menu buttons and want to make another event when the user is in the pieChartPage(), they are able to click on the PS_Button (which is a PlayStation Logo) to display a description. Right now the user is able to hover over the PS_Button and the description appears but I want the user to click on the PS_Button so that the description stays on the screen. I created a class 'ImageButton' for the description formatting and I am not sure how, if possible, to add the mousePressed() code into that class and still work with the rest of the code.
Here is my code:
In the setup() draw() page: mousePressed() - line 180, pieChartPage() - line 244
https://drive.google.com/drive/folders/1d5y2Ksq-y05J8tLiyK-1P2uwLjBhm-iH?usp=sharing
The link contains the code and the data used within the code.
Any help would be very appreciated, thank you.
setup() draw():
PieChart pieChart; //pie chart
PImage PS_Button, XBOX_Button, NINTENDO_Button; //creating variables for the images to be used
ImageButton psImageDscpt, xboxImageDscpt, nintendoImageDscpt;
void setup() {
size(1200, 800);
//functions to make the render of the program Apple Retina displays and Windows High-DPI displays look smoother and less pixelated
pixelDensity(2);
smooth();
loadData();
//Pie Chart
//pie chart for pie chart page
pieChart = new PieChart();
psImageDscpt = new ImageButton("Sony", 800, 480, 300, 200);
xboxImageDscpt = new ImageButton("Microsoft", 20, 425, 250, 200);
nintendoImageDscpt = new ImageButton("Nintendo", 925, 75, 200, 300);
}
void draw() {
switch(state) {
case 2:
// pie chart
pieChartPage();
drawMenuButton();
break;
case 3:
bubbleChartPage();
drawMenuButton();
// pie chart
break;
case 4:
scatterGraphPage();
drawMenuButton();
// pie chart
break;
default:
// menu
// the default case means "everything else" and in our case it will be the menu so you cannot have an impossible case
drawMenu();
drawMenuButton();
}
}
Current mousePressed() and commented is the code I want to add:
void mousePressed() {
switch(state) {
case 2: // pie chart page
ClickedMenuButton();
println("Going back to state home from PC");
//state = 1;
break;
case 3: // bubble chart page
ClickedMenuButton();
println("Going back to state home from BG");
break;
case 4: // scatter graph page
ClickedMenuButton();
println("Going back to state home from SG");
break;
default:
// menu
ClickMenu();
}
}
//void mousePressed() {
// if ( mouseX > psImgX && mouseX < (psImgX + PS_Button.width) && mouseY > psImgY && mouseY < (psImgY + PS_Button.height)) {
// psImageDscpt.drawPieChartDscpt();
// }
//}
void pieChartPage() in setup() draw() page:
//page for pie chart visualisation
void pieChartPage() {
background(10);
//psImgX = 100;
//psImgY = 50;
//xboxImgX = 200;
//xboxImgY = 50;
//nintendoImgX = 300;
//nintendoImgY = 50;
PS_Button = loadImage("ps_logo.png"); //loading images from program folder
psLegend = loadImage("ps_logo.png");
XBOX_Button = loadImage("xbox_logo.png");
xboxLegend = loadImage("xbox_logo.png");
NINTENDO_Button = loadImage("nintendo_logo.png");
nintendoLegend = loadImage("nintendo_logo.png");
//int psImgX, psImgY, xboxImgX, xboxImgY, nintendoImgX, nintendoImgY;
PS_Button.resize(100, 0); //size of the button, all are the same
XBOX_Button.resize(100, 0);
NINTENDO_Button.resize(100, 0);
psLegend.resize(75, 0);
xboxLegend.resize(75, 0);
nintendoLegend.resize(125, 0);
tint(225);// set tint of buttons to be zero
image(PS_Button, psImgX, psImgY); //drawing image to the screen
image(XBOX_Button, xboxImgX, xboxImgY);
image(NINTENDO_Button, nintendoImgX, nintendoImgY);
//drawing legend and tints represent the company's colour respectively
tint(pieChartColours[1]);
image(psLegend, 65, 75);
tint(pieChartColours[2]);
image(xboxLegend, 65, 175);
tint(pieChartColours[0]);
image(nintendoLegend, 45, 285);
pieChart.pieChart(width/2, height/2, 400, values_PieChart, pieChartColours); //draws the pie chart into the window
textSize(12);
if (mouseX > psImgX && mouseX < (psImgX + PS_Button.width) && mouseY > psImgY && mouseY < (psImgY + PS_Button.height)) {
psImageDscpt.drawPieChartDscpt();
} else if (mouseX > xboxImgX && mouseX < (xboxImgX + XBOX_Button.width) && mouseY > xboxImgY && mouseY < (xboxImgY + XBOX_Button.height)) {
xboxImageDscpt.drawPieChartDscpt();
} else if (mouseX > nintendoImgX && mouseX < (nintendoImgX + NINTENDO_Button.width) && mouseY > nintendoImgY && mouseY < (nintendoImgY + NINTENDO_Button.height)) {
nintendoImageDscpt.drawPieChartDscpt();
} else {
psImageDscpt.drawPieChartDscpt();
xboxImageDscpt.drawPieChartDscpt();
nintendoImageDscpt.drawPieChartDscpt();
}
//legend text
fill(225);
text("Sony / PlayStation", 55, 150);
text("Microsoft / XBOX", 52.5, 260);
text("Nintendo", 80, 340);
textSize(40);
fill(pieChartColours[0]);
text("/50", 230, 330);
fill(pieChartColours[1]);
text("/50", 215, 130);
fill(pieChartColours[2]);
text("/50", 185, 230);
fill(150,0,0);
textSize(60);
text("64%", 590, 350);
fill(0,150,0);
textSize(20);
text("7%", 460, 465);
fill(0,0,150);
textSize(40);
text("11%", 565, 525);
homeButtonBar.Draw();
}
ImageButton class:
class ImageButton {
String pieDscptLabel;
int xPos1, yPos1, xPos2, yPos2;
float wDiam1, hDiam1;
ImageButton (String pieDscptLabel, int xPos1, int yPos1, float wDiam1, float hDiam1) {
this.pieDscptLabel=pieDscptLabel;
this.xPos1=xPos1;
this.yPos1=yPos1;
this.wDiam1=wDiam1;
this.hDiam1=hDiam1;
}
void drawPieChartDscpt() { //draws the bubbles description
noFill();
stroke(225);
rect(xPos1, yPos1, wDiam1, hDiam1, 20);
textAlign(LEFT, BASELINE);
fill(200);
text(pieDscptLabel, xPos1+10, yPos1+20);
}
void mousePressed() {
if ( mouseX > psImgX && mouseX < (psImgX + PS_Button.width) && mouseY > psImgY && mouseY < (psImgY + PS_Button.height)) {
psImageDscpt.drawPieChartDscpt();
println("it works");
}
}
}

you don't need to write two mousePressed() method, just test the cases inside the method to differentiate which case you are working on, after some reading I figured your state variable equals 2 when the user is on pieChartPage() So I moved your code inside the first mousePressed() and added the test, the method call to draw description works fine but you will have to change it to switch between visible/invisible instead of drawing when the user click, here is the updated code:
void mousePressed() {
if(state == 2)
{
if ( mouseX > psImgX && mouseX < (psImgX + PS_Button.width) && mouseY > psImgY && mouseY < (psImgY + PS_Button.height))
{
psImageDscpt.drawPieChartDscpt();
}
}
switch(state) {
case 2: // pie chart page
ClickedMenuButton();
println("Going back to state home from PC");
//state = 1;
break;
case 3: // bubble chart page
ClickedMenuButton();
println("Going back to state home from BG");
break;
case 4: // scatter graph page
ClickedMenuButton();
println("Going back to state home from SG");
break;
default:
// menu
ClickMenu();
}
}
Edit:
to keep the description on the screen you have 3 changes to do: first, you declare a boolean global variable on top and set it to false by default :
//boolean to hold the info whether we draw description or not
boolean show_ps_description=false;
Secondly, you change the Click on Playstation image event to inverse the previous boolean image instead of calling that draw method :
void mousePressed() {
if(state == 2)
{
if ( mouseX > psImgX && mouseX < (psImgX + PS_Button.width) && mouseY > psImgY && mouseY < (psImgY + PS_Button.height))
{
//inverte the boolean
show_ps_description=!show_ps_description;
}
}
...
}
Lastly you call your drawing method (drawPieChartDscpt()) in your draw loop after checking if our boolean is true:
void draw() {
switch(state) {
case 2:
// pie chart
pieChartPage();
drawMenuButton();
if(show_ps_description)
psImageDscpt.drawPieChartDscpt();
break;
...
}

Related

drawing on screen after region clicked processing

I'm trying to make it so that when the user clicks the pencil icon in the program they are allowed to draw until the pencil has been clicked again. ive not added in a toggle yet but my current problem is that i get the desired behaviour in the mouse region which i specified but nowhere else on the screen, also please excuse my code its devolved a lot while playing with it, however by running it in its currrent state you should see what bug im talking about.
thanks
PImage[] img = new PImage[3];
boolean drawPencil = false;
boolean draw = false;
void setup() {
size(960, 720);
for (int i = 0; i < img.length; i++)
img[i] = loadImage("image"+i+".png");
//an array of images that sorts and load them into our program
image(img[0],0,0);
img[0].resize(width, height);
// set the desired image to screen size
}
void mousePressed() {
if (mouseX > 772 && mouseX < 864 && mouseY > 1 && mouseY < 74) {
drawPencil = true;
draw = true;
// if the user clicks the pencil icon they will begin to draw
}
}
void mouseReleased()
{
draw = false;
}
void draw() {
if (draw) {
stroke(255);
line(mouseX, mouseY, pmouseX, pmouseY);
}
if (key == 'w'||key == 'W') {
image(img[0],0,0);
img[0].resize(width, height);
println(drawPencil);
}
}
Way better post than the last. I think I now get what you want. I coded you a short example so you can get the hang of it. Here's what it will do:
PImage img;
boolean canDraw;
void setup() {
size(960, 720);
background(255);
img = loadImage("pencil.png");
}
void draw() {
if (canDraw) {
fill(color(0, 128, 0));
} else {
fill(color(128, 0, 0));
}
rect(800, 1, 100, 100);
image(img, 800, 1, 100, 100);
if (canDraw && mousePressed) {
stroke(0);
line(mouseX, mouseY, pmouseX, pmouseY);
}
}
void mouseClicked() {
if (mouseX > 800 && mouseX < 800+img.width && mouseY > 1 && mouseY < 1+img.height) {
canDraw = !canDraw;
}
}
I'll hang around in case you want to ask questions about this. Have fun!

why does the score keep counting after gameover?

Everything works perfectly as it should except the fact that even when the person has lost the game, the score keeps counting. The score should freeze when the game is lost so that when the method gameover (); is called the score can be given for that game.
It also does not add a specific amount, the score just raises from 10 to 210 to 430 - it just all over the place.
Can someone find the reason for it?
// variables for the ball
int ball_width = 15,
ball_height = 15;
float ballX,
ballY;
// // variables for the paddles
int paddle_width = 20,
paddle_height = 200;
int paddle1,
paddle2=0;
// // direction variables
float directionX = 12,
directionY = 12;
// // variables for the score
int scorecounter = 0;
// //game states
boolean playing = false,
gameover = false,
finalscore = false,
score = true;
// ----------------------------------------------------
void setup () {
size (1900, 800); // the field game is going to be 1900x800 px big
background (0); // black background
rectMode (CENTER);
paddle1 = 60;
paddle2 = width - 60;
}
void draw () {
background (0); // black background
playing ();
contact ();
gameover ();
}
// ----------------------------------------------------
void playing () {
if (keyPressed) { // starts the game and makes the restart possible
playing = true;
scorecounter = 0 ;
gameover = false;
ballX = width/2;
ballY = height/2;
}
if (!playing) {
// playing = false
fill(255);
textSize(80);
textAlign(CENTER);
text("Press Space to Play", width/2, height/4);
fill (255);
ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
fill (255, 10, 20);
rect(paddle1, (height/2), paddle_width, paddle_height); // red pong
fill (60, 255, 0);
rect(paddle2, (height/2), paddle_width, paddle_height); // green pong
}
if (playing) { // playing = true
score(); // does what i wrote down in void score () -- keeps track of the score
ballX = ballX + directionX;
ballY = ballY + directionY; // gives the directions of the ball
fill (255);
ellipse (ballX, ballY, ball_width, ball_height); // ball itself
fill ( 255, 10, 20 );
rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
fill ( 60, 255, 0 );
rect(paddle2, mouseY, paddle_width, paddle_height); // green pong
// top and bottom of screen --------------------
if ( ballY + ball_width/2 > height ) {
directionY = -directionY;
} // if the ball reaches the lower wall it will bounce off
if ( ballY - ball_width/2 < 0 ) {
directionY = -directionY;
} // if the ball reaches the upper wall it will bounce off
if ( ballX > width || ballX < 0 ) {
gameover = true;
} // if the ball reaches one of the bounderies it will be game over
}
}
void contact () {
// right paddle = paddle2
if (ballY - ball_width/2 > mouseY - paddle_height/2 &&
ballY + ball_width/2 < mouseY + paddle_height/2 &&
ballX + ball_width/2 > paddle2-4 ) {
directionX = -abs(directionX);
scorecounter = scorecounter + 10;
}
// left paddle = paddle1
if (ballY - ball_width/2 > mouseY - paddle_height/2 &&
ballY + ball_width/2 < mouseY + paddle_height/2 &&
ballX + ball_width/2 < paddle1+ paddle_width+4 ) {
directionX = abs(directionX);
scorecounter = scorecounter + 10; // if the ball touches the paddles it will bounce off
}
score ();
}
void gameover () {
if (gameover) {
background (0);
finalscore (); // gives me the final score + play again option
score = false;
} // it overrides the scorecounter with a black background
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4); // keeps the score on the display while playing
}
}
void finalscore () {
if (gameover) {
score = false; // the scorecounter will stop counting
fill(255);
textSize(45);
textAlign(CENTER);
text("Game Over. Press a key to play again.", width/2, height/4); // game over message
fill(255);
textSize(80);
textAlign(CENTER);
text("You scored " + scorecounter+ " points", width/2, (height/4) * 3); // the final score will appear here
}
}
You should have
playing = false;
somewhere in game over method or somewhere else. As I see you haven't such line of code.
I ask a question. Where did you change playing to false so that
You expect to get
If(!playing)
Condition instead of
If(playing)
You press the key. Game begins. Now you reach
If(playing)
Then the ball get to boundaries and the code reach
if ( ballX > width || ballX < 0 ) {
gameover = true; } // if the ball reaches one of the bounderies it will be game over
In this situation. Playing var and gameover var both are true. Right?
And under
If(playing)
You have score() method.so even when gameover is true the score method still is performed.
You can set playing to false under:
if ( ballX > width || ballX < 0 ) { gameover = true;
Playing =false ; }
Or you can modify the score method like this:
void score () { if (playing && !gameover) { fill(255); textSize(45); textAlign(CENTER); text(scorecounter, width/2,height/4);
// keeps the score on the display while playing }

Ball should bounce off the paddles and game should restart once it is gameover

So this is my entire code and what it does not do, but should definitely do is first bounce off when colliding with the paddles and second if it does not do so then once the game enters in gameover mode once a key is pressed it should restart the game. Now I've tried several things and nothing seems to work.
Can someone please find a solution and try to explain what I did wrong?
// variables for the ball
int ball_width = 15, ball_height = 15;
int ballX = width/2, ballY = height/2;
//
// variables for the paddles
int paddle_width = 20, paddle_height = 150;
int paddle1 = 60, paddle2;
//
// direction variables
int directionX = 15, directionY = 15;
//
// variables for the score
int scorecounter = 0;
//
//game states
boolean playing = false, gameover = false, finalscore = false, score = true;
void setup () {
size (1900, 1300); // the field game is going to be 1900x1300 px big
rectMode (CENTER);
paddle2 = width - 60;
}
void draw () {
background (0); // black background
playing ();
gameover ();
finalscore();
}
//
void playing () {
if (keyPressed) {
playing = true;
}
if (!playing) { // playing = false
fill(255);
textSize(80);
textAlign(CENTER);
text("Press Space to Play", width/2, height/4);
fill (255);
ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
fill (255, 10, 20);
rect(paddle1, (height/2), paddle_width, paddle_height); // red pong
fill (60, 255, 0);
rect(paddle2, (height/2), paddle_width, paddle_height); // green pong
}
if (playing) { // playing = true
score();
ballX = ballX + directionX;
ballY = ballY + directionY;
fill (255);
ellipse (ballX, ballY, ball_width, ball_height);
fill ( 255, 10, 20 );
rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
fill ( 60, 255, 0 );
rect(paddle2, mouseY, paddle_width, paddle_height); // green pong
if ( ballY > height ) {
directionY = -directionY;
} // if the ball reaches the lower wall it will bounce off
if ( ballY < 0 ) {
directionY = -directionY;
} // if the ball reaches the upper wall it will bounce off
if ( ballX > width || ballX < 0 ) {
gameover = true; }
}
if (ballX == paddle1 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
if (ballX == paddle2 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
}
void gameover () {
if (gameover) {
background (0);
}
finalscore ();
score = false;
if (keyPressed) {
playing = true;
}
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4);
if (ballX == paddle1 && ballY <= paddle_height) {
scorecounter = scorecounter + 10;
}
if (ballX == paddle2 && ballX <= paddle_height) {
scorecounter = scorecounter + 10;
}
}
if (!playing) {
score = false;
}
}
void finalscore () {
if (gameover) {
score = false;
fill(255);
textSize(45);
textAlign(CENTER);
text("Game Over. Press a key to play again.", width/2, height/4);
fill(255);
textSize(80);
textAlign(CENTER);
text("You scored " + scorecounter + " points", width/2, (height/4) * 3);
if (keyPressed) {
playing = playing;
}
}
}
Not sure if that's the problem, but
if (keyPressed) {
playing = playing;
}
looks not very useful. You have both a function and a variable named playing. Reusing identifiers is usually a recipe for confusion.
For your first issue of the ball not bouncing off the paddle, take a look at these lines:
if (ballX == paddle1 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
if (ballX == paddle2 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
Notice that you're checking whether ballX == paddle1 or ballX == paddle2. You're checking whether the exact pixel position of the ball matches either of the paddles.
However, also notice that you're moving the ball by 15 pixels each time, so the chances of it hitting an exact pixel are pretty low. In other words, your ball is going to move through the exact pixel position and be inside your paddles. That is what you have to check for.
Furthermore, you're only checking whether the ball's y position is less than paddleHeight. That's not enough. You also have to check whether it's greater than the paddle position!
Even after that, I'm not sure you want to reverse directionY when it hits a paddle, but I'll let you decide that for yourself.
For your other question about the gameover not reseting, notice that you never reset the ball position! That means you start playing, but then instantly get a game over. You need to reset the position of the ball before you restart the game.
Honestly, if I were you, I would start with a more basic sketch. Your code has a lot of extra stuff in it that doesn't make a lot of sense, so I'm not surprised that you're getting confused. Start with something much simpler: can you create a sketch that simply detects when the mouse is inside a rectangle? Start from there (or even simpler!) and take small steps instead of trying to program your end goal all at once. Good luck.
The reason why this is not reseting is because you are never updating the ballX o gameover variable. Every time you press a key you are falling into this conditional.
if ( ballX > width || ballX < 0 ) {
gameover = true;
}
I got around this by adding your inital values to the playing method:
void playing () {
if (keyPressed) {
playing = true;
gameover = false;
ballX = width/2;
ballY = height/2;
}
// rest of playing() code
}

Can someone find the reason why my rectangles do not follow my mouse?

So down below I have my entire code for a pong game I am programming and although I used mouseY in the y coordinate
fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100); // green pong
it does not work and I can simply not find my mistake.
Also I am struggling with the Score () part because everytime I try to put
if ( ballX = paddleX ) { score = score + 1;}
processing tells me that it is not possible to convert int to Boolean. Can someone please tell me how I can rewrite my code so this error does not appear anymore. What my goal with that part of the code is, is that I want the counter to increase by one every time the ball hits one of the paddles.
full code:
float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float x = 40, y, score = 0;
float paddleX, paddleY = 0; // variables for the paddles
float mouseY, mouseX;
boolean playing = false, gameover = false, finalscore = false, Score = true; // game states
boolean keyPressed, key;
void setup() {
size (1500,1100); // the field is going to be 1500x1100px big
paddleX = width - 40;
}
void keyPressed () { // the game will only start when a key is pressed
playing = true;
}
void draw() {
background(0); // black background
if (!playing) { // playing = false
fill(255); textSize(80); textAlign(CENTER); text("Press Space to Play",width/2, height/4);
fill (255); ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
fill (255,10,20); rect(paddleX/58, (height/2)-50, 20, 100); // red pong
fill (60,255,0); rect(paddleX, (height/2)-50, 20, 100); // green pong
}
if (playing) { // playing = true
Score();
fill ( 0, 255, 0 ); ellipse (ballX, ballY, 15, 15);
fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100); // green pong
if ( ballY > height ) { dY = -dY; } // if the ball reaches the lower wall it will bounce off
if ( ballY < 0 ) { dY = -dY; } // if the ball reaches the upper wall it will bounce off
ballX = ballX + dX; ballY = ballY + dY;
if (ballX > width || ballX < 0 ) { gameover = true;}
}
if (gameover) { // gameover = true
finalscore = true;
if (keyPressed) { playing = false; }
else if (playing) { playing = true; }
if (finalscore) { // score = true
fill(255); textSize(45); textAlign(CENTER); text("Game Over. Press a letter to play again.",width/2, height/4);
fill(255); textSize(80); textAlign(CENTER); text("You scored " + score + " points" ,width/2, (height/4) * 3);
if (keyPressed) { playing = true;}
}
}
}
void Score() {
fill(255); textSize(45); textAlign(CENTER); text(score,width/2, height/4);
if (playing) {
if (paddleX <= ballX){ score = score + 1;}
}
}
You're declaring your own mouseX and mouseY variables at the top of your sketch:
float mouseY, mouseX;
Don't do this. Processing automatically creates mouseX and mouseY variables for you. You don't create them yourself. Get rid of this line so that you're using Processing's mouseX and mouseY variables, not your own.
Also I am struggling with the Score () part because everytime I try to put
if ( ballX = paddleX ) { score = score + 1;}
Look at that if statement. You're only using a single =, which is an assignment. You need to use two equals ==, or better yet an inequality like <= or >=, since the chances of the ball hitting the exact pixel value of the paddle is pretty low.

How can i register when text and shapes has been clicked on?

rect(x, y, 100, 100)
text("click here", 50, 50)
Is there a way to use mousePressed() so it registers when these two items have been clicked on?
It sounds like you're looking for collision detection. Specifically, you're probably looking for point-rectangle collision detection, to determine whether the mouse is inside a rectangle.
Google is your friend, but here's an example:
float rectX;
float rectY;
float rectWidth;
float rectHeight;
void setup() {
size(300, 300);
rectX = 50;
rectY = 100;
rectWidth = 200;
rectHeight = 100;
}
void draw() {
background(64);
if (mouseX > rectX && mouseX < rectX + rectWidth && mouseY > rectY && mouseY < rectY + rectHeight) {
fill(255, 0, 0);
}
else {
fill(0, 255, 0);
}
rect(rectX, rectY, rectWidth, rectHeight);
}
Shameless self-promotion: here is a tutorial on collision detection in Processing.

Resources