Question: Pls use the java language Lets write a processing program that uses some clever math to make visual effects. This program will have two distinct
Pls use the java language
Lets write a processing program that uses some clever math to make visual effects. This program will have two distinct features: a rectangle will snap to a grid under the mouse (move along the grid as you move the mouse instead of being right under the mouse), and, the color will follow a simple formula that makes it look unpredictable. a. Create a clean active Java program, with a canvas sized 500 b. You will make a rectangle snap to a grid 10 by 10 (100 places). Make a global called gridSize and set it to 10. In the draw loop, make a local variable called cellSize (how big each cell is) and store the size of each cell. Hint: screen size divided by grid size. c. Calculate the top left corner for a rectangle to draw. This corner will have to snap to this grid. That is, it can only be 0,0, or cellsize*i, cellsize*j for some integers i and j. You will find the nearest grid point to the top left of the mouse cursor. There is a trick to it. d. Integer division always throws away the fraction part. If you divide the mouse coordinate by the cell size, it will tell you how many cells along the mouse is. Do this separately for x and y and store in int cellX, cellY. This is not the coordinates on the screen, but rather, the cell number e. Convert from the cell numbers to screen coordinates. This is easy. Just multiply the cell number by the cell size. Cell 0 gives you 0, cell 1 gives cellSize, and cell 2 gives 2*cellSize f. Draw a rectangle at this cell g. Now you should be done the drawing part. Set some simple color for now and debug up to this point before moving along. Next lets work on the color. h. Create a global integer called runningColor to keep track of the last color we used. Then, each frame calculate the color as follows. This is not something you should develop an intuition for, its just a silly bit of math to make the color act interestingly as you move the mouse around. i. Take the product of the mouseX and runningColor and add mouseY to it. Then modulo the result by 256, and multiply by -1. Store this final result as the new runningColor. j. Before setting the current fill and stroke color to running color, make a copy and cap it above zero in case it happened to go negative. Dont store the capped number in runningColor, if its negative let it stay that way for next time.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
