Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Falling Sands: Part 4 - updateDisplay Method The updateDisplay method is called ( by the run method ) at regular intervals. Its job is to
Falling Sands: Part updateDisplay Method
The updateDisplay method is called by the run method at regular intervals. Its job is to draw each particle and empty space found in grid onto the display, using SandDisplay's setColor method.
I have provided some starter code for you. This code creates a Color object to use for the empty cells. Color objects are defined by the RedGreenBlue RGB color scheme. Setting each of the values to results in a black color.
The nested for loops iterate through all the cells of the grid. When a cell is found to be empty, we call the SandDisplay's setColor method to draw the cell with the appropriate color.
Create a Color object to use for metal particles, and add code to the nested for loops to check for metal cells. When you find one, call the SandDisplay's setColor method with the appropriate values.
Tip for creating Color objects: each of the RedGreenBlue components can be set to a value between and Here are some examples.
When you have accomplished this, you should now be able to paint metal particles and erase them. Run your program and test this.
Part Sand Particles
Modify your program so that you can also paint with sand particles probably in yellow or some other sandlike color To do this, perform the following steps:
Add a constant at the top of the file for sand.
In the constructor, change the size of the "names" array to and store the name of the sand particle in the rd cell.
In the updateDisplay method, create a new Color object for sand and add an additional "else if inside the loop to set sand particles.
Part step Method
The step method is called by the run method at regular intervals. This method should choose a single random valid location do not use a loop If that location contains a sand particle and the location below it is empty, the particle should move down one row. Metal particles will never move. This code should only modify the array. Do not set any colors in the display. Test that your sand particles fall now.
Tip : Make sure your sand particles do not fall out of your grid the twodimensional array The origin is at the topleft corner of the grid, so when a sand particle falls down, it will be increasing its row value. The row value should never exceed the bounds of the twodimensional array. The end result is that sand particles will collect at the bottom of the grid.
Tip : If particles fall too quickly or too slowly, the speed can be adjusted by adjusting the slider in the display or by changing the dimensions passed to the SandLab constructor from main
Note: Because the step method picks a single random particle to move or act in some way each time it is called, it is possible that some sand particles will move several times before others have the chance to move at all. In practice, the step method is called so rapidly that you are unlikely to notice this effect when you run the code. package fallingsand;
import java.awt.;
public class SandLab
add constants for particle types here
public static final int EMPTY ;
public static final int METAL ;
do not add any more fields
private int grid;
private SandDisplay display;
public static final int ROWS ;
public static final int COLUMNS ;
public static void mainString args
SandLab lab new SandLabROWS COLUMNS;
lab.run;
public SandLabint numRows, int numCols
String names;
names new String;
namesEMPTY "Empty";
namesMETAL "Metal";
display new SandDisplayFalling Sand", numRows, numCols, names;
called when the user clicks on a location using the given tool
private void locationClickedint row, int col, int tool
copies each element of grid into the display
public void updateDisplay
Color empty new Color;
for int row ; row ROWS; row
for int column ; column COLUMNS; column
if gridrowcolumn EMPTY
display.setColorrow column, empty;
called repeatedly.
causes one random particle to maybe do something.
public void step
do not modify
public void run
while true
for int i ; i display.getSpeed; i
step;
updateDisplay;
display.repaint;
display.pause; wait for redrawing and for mouse
int mouseLoc display.getMouseLocation;
if mouseLoc nulltest if mouse clicked
locationClickedmouseLoc mouseLoc
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started