Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Falling Sand Project: Part 4 - updateDisplay Method The updateDisplay method is called ( by the run method ) at regular intervals. Its job is
Falling Sand Project: 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.
Part Water Particles
Modify your program so that you can also paint with water particles, which move in one of three randomly chosen directions: down, left, or right. In the step method, when the randomly chosen location contains a water particle, pick one of three random directions. If the location in that randomly chosen direction is empty, the water particle moves there. Test that the water behaves roughly like a liquid, taking the shape of a container.
Part Dropping Sand into Water
What happens now when you drop sand particles into water? Right now, sand is only allowed to move into empty spaces. Modify your code so that a sand particle can also move into a space containing a water particle by trading places with the water particle Test that you can drop sand into water now without destroying the water
Part Additional Particles
Congrats, you're doing well! To complete the assignment, add two additional particle types that behave differently than the existing ones. You can add whatever you want. Here are some ideas:
a gas that raises to the top of the grid
a particle that floats on water maybe wood or plastic
an acid that eats metal or another type of material 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",
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