Please written by computer source In this lab you will create and fill a two-dimensional array of 25 Rectangle objects, arranged in a 5x5 grid. Each Rectangle should have dimensions 100x100, and be arranged on your window so that there is no space between Rectangles. Each Rectangle object should start with one fill color (e.g. white) and when pressed with the mouse it should toggle to a second color (e.g. gray). Pressing a second time on the Rectangle should toggle it’s fill color back to the first color. 3. Use the following template for your main() method. Add missing statements necessary to create your two- dimensional 5x5 Rectangle array and nested for-loops to fill the array with 25 new Rectangle objects, located on your window in a regular 5x5 grid. Make sure to set the mouse-pressed event handler for each new Rectangle. 2. Declare at least two public static variables: a 2D array of Rectangle objects, and a Pad variable: public static Pad pad; public static void main(String[] args) { // Set the title and dimensions of your window. pad = new Pad("Lights Out", 500, 500); // Create the new Rectangle array to hold 25 Rectangle objects in a 5x5 array. // ... // Used nested for-loops to fill the array with Rectangle objects // and set mouse-pressed handler method for each new Rectangle. // ... } 4. Use the following method template to handle mouse-pressed events for all Rectangles. Toggle the fill color of the pressed Rectangle by inspecting the current fill color and using an if-statement to set a new fill color. // When a light is pressed, toggle the light public static void onMousePressed(Shape shp, double x, double y, int button) { // Compute Rectangle array indexes from mouse coordinates. int r = (int)Math.floor(y/100.0); int c = (int)Math.floor(x/100.0); // Use the computed array indexes to get a reference to the pressed Rectangle. // Check if the Rectangle (light) is on by inspecting its fill color elements. // Toggle fill color using an if-statement. // ... }
Please written by computer source
In this lab you will create and fill a two-dimensional array of 25 Rectangle objects, arranged in a 5x5 grid.
Each Rectangle should have dimensions 100x100, and be arranged on your window so that there is no space between Rectangles.
Each Rectangle object should start with one fill color (e.g. white) and when pressed with the mouse it should toggle to a second color (e.g. gray).
Pressing a second time on the Rectangle should toggle it’s fill color back to the first color.
3. Use the following template for your main() method. Add missing statements necessary to create your two- dimensional 5x5 Rectangle array and nested for-loops to fill the array with 25 new Rectangle objects, located on your window in a regular 5x5 grid. Make sure to set the mouse-pressed event handler for each new Rectangle.
2. Declare at least two public static variables: a 2D array of Rectangle objects, and a Pad variable: public static Pad pad; public static void main(String[] args) { // Set the title and dimensions of your window. pad = new Pad("Lights Out", 500, 500); // Create the new Rectangle array to hold 25 Rectangle objects in a 5x5 array. // ... // Used nested for-loops to fill the array with Rectangle objects // and set mouse-pressed handler method for each new Rectangle. // ... }
4. Use the following method template to handle mouse-pressed events for all Rectangles. Toggle the fill color of the pressed Rectangle by inspecting the current fill color and using an if-statement to set a new fill color. // When a light is pressed, toggle the light public static void onMousePressed(Shape shp, double x, double y, int button) { // Compute Rectangle array indexes from mouse coordinates. int r = (int)Math.floor(y/100.0); int c = (int)Math.floor(x/100.0); // Use the computed array indexes to get a reference to the pressed Rectangle. // Check if the Rectangle (light) is on by inspecting its fill color elements. // Toggle fill color using an if-statement. // ... }
Trending now
This is a popular solution!
Step by step
Solved in 4 steps