Answered step by step
Verified Expert Solution
Link Copied!

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 4- 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 Red-Green-Blue (RGB) color scheme. Setting each of the values to 0 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 Red-Green-Blue components can be set to a value between 0 and 255. 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 5- Sand Particles
Modify your program so that you can also paint with sand particles (probably in yellow or some other sand-like 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 3 and store the name of the sand particle in the 3rd 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 6- 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 1: Make sure your sand particles do not fall out of your grid (the two-dimensional array). The origin (0,0) is at the top-left 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 two-dimensional array. The end result is that sand particles will collect at the bottom of the grid.
Tip 2: 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 =0;
public static final int METAL =1;
//do not add any more fields
private int[][] grid;
private SandDisplay display;
public static final int ROWS =120;
public static final int COLUMNS =80;
public static void main(String[] args)
{
SandLab lab = new SandLab(ROWS, COLUMNS);
lab.run();
}
public SandLab(int numRows, int numCols)
{
String[] names;
names = new String[2];
names[EMPTY]= "Empty";
names[METAL]= "Metal";
display = new SandDisplay("Falling Sand", numRows, numCols, names);
}
//called when the user clicks on a location using the given tool
private void locationClicked(int row, int col, int tool)
{
}
//copies each element of grid into the display
public void updateDisplay()
{
Color empty = new Color(0,0,0);
for (int row =0; row < ROWS; row++)
{
for (int column =0; column < COLUMNS; column++)
{
if (grid[row][column]== EMPTY)
{
display.setColor(row, 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 =0; i < display.getSpeed(); i++)
{
step();
}
updateDisplay();
display.repaint();
display.pause(1); //wait for redrawing and for mouse
int[] mouseLoc = display.getMouseLocation();
if (mouseLoc != null)//test if mouse clicked
{
locationClicked(mouseLoc[0], mouseLoc[

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago