So basically, I'm fairly new to processing and have a question about how to make your mouse location affect various things in the code. I tried an exercise to see if I could do this by changing the appearance of the cursor when the mouse hovers over a button that I made. However, I found that I do not know how to specify whether or not my mouse is inside of the button on both axes. If the button was a square, the workaround that I implemented would be sufficient, although probably amateur. However, since the button is a circle, even if the mouse is just barely outside of the circle but within where the vertices would be on a similar sized square, the cursor changes. How do I tell the program to only execute this code if mouseX and mouseY are inside of button1?
Button button1;
void setup() {
size(800, 800);
smooth();
button1 = new Button(255, width/2, height/2, 100);
Hi, I was just curious about 2 things regarding the following code.
1. Why is there a brief interval of time from when the program starts to when the shape is drawn in which there is just a
white background? In other words, why doesn't the program start with the shape already drawn? I tried moving shape(tpz, 0, 0); to the setup function but this doesn't work, presumably for very obvious reasons beyond my comprehension.
2. Why is there a momentary flicker of gray in the instant that the shape is drawn?
I'm still a beginner in coding and decided to practice basic algorithms and how to make objects display at a given time after pressing play. My question is why does the following code yield the error message "NullPointerException"? The bold blue line is the one that is highlighted with the error message. Basically, I was trying to create a sketch that would create a button at a random location every 2 seconds. What am I overlooking? Thanks in advance for all the help.
Hi, I am relatively new to programming, and I am learning through the book
Learning Processing as well as experimenting and building stuff independently. I have a question for experienced programmers regarding variables and passing copies. My question is "Is it necessary for variables that pass as copies to be made global?"
Here is a concrete example of my problem:
int carX = width-100;
int carY = height-450;
int carLength = width/10;
int carWidth = height/20;
int wheel = carLength/4;
void setup () {
size (800, 800);
background (0, 100, 50);
}
void draw () {
road ();
carLooks (700, 350, 80, 40, 20, color (random(255), random(255), random(255)));
carLooks (450, 350, 80, 40, 20, color (random(255), random(255), random(255)));
carLooks (200, 350, 80, 40, 20, color (random(255), random(255), random(255)));
}
void road () {
int roadX = 400;
int roadY = 400;
strokeWeight (5);
stroke (255);
fill (128);
rectMode (CENTER);
rect (roadX, roadY, width+5, height/4);
// Lane dividing lines
for (int i = 0; i <= width; i += 96) {
line (i, roadY, i+48, roadY);
}
}
void carLooks (int carX, int carY, int carLength, int carWidth, int wheel, color c) {
// Car body
noStroke ();
fill (c);
rect (carX, carY, carLength, carWidth, 50);
noLoop ();
// Car wheels
fill (0);
rect (carX-wheel, carY-wheel, wheel, wheel/2, 5);
rect (carX+wheel, carY-wheel, wheel, wheel/2, 5);
rect (carX-wheel, carY+wheel, wheel, wheel/2, 5);
rect (carX+wheel, carY+wheel, wheel, wheel/2, 5);
}
I have tried to put the global variables (bold) in the block of code entitled "void carLooks" but I received an error message reading "duplicate local variable carX...". Is there a way to move the bold section to "void carLooks" without receiving this error message? It seems reasonable that this may not be possible, that these variables must remain global because the variables are being used in a block of code outside of "void carLooks", namely, "void draw", but I don't have a firm understanding on how passing copies works. The reason I would like to turn the global variables into local ones under "draw carLooks" is because I don't see myself reusing them in other blocks of code, and I am practicing how to be organized by keeping variables local whenever possible. If there is a way, then let me know how or refer me to a source, and if there is not, then that is okay too.
Thank you all for your time.
Twitter: @eselotter
*edit*
It is also worth mentioning that the reason I am passing the copies of the parameters to "void draw" is so I can create multiple versions of the car. This is why I am avoiding using carLooks (); without parameters.