Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Falling Sand Project: Part 1 - Run the Program Run the program in NetBeans. You should see a window pop up . On the left

Falling Sand Project: Part 1- Run the Program
Run the program in NetBeans. You should see a window pop up. On the left side is a black rectangular canvas which will soon be inhabited by particles. On the right side there is one button for each tool you will be able to paint with: Empty (for erasing) and Metal (for creating metal particles). You can't actually paint now, because you haven't written the code yet.
Look in the SandLab.java file, and you'll see that a SandLab remembers two things:
grid - a 2-dimensional array of int values that represent the type of particle found at each location
display - the SandDisplay used to show the particles on the screen
Notice that we're using int values to represent particle types, with 0 representing empty, 1 representing metal, and higher values representing the additional particle types you'll be adding. To avoid confusion, we never want to see these particle type numbers (0,1, etc.) in our code! Instead, we've declared variables for each of these types. You'll see these listed near the top of SandLab.java.
public static final int EMPTY =0;
public static final int METAL =1;
This lets us use meaningful variable names instead of confusing type numbers in our code. For example:
if (type == METAL)
These variables are marked final to indicate that they are constants. (Attempts to re-assign to these variables will not compile.) By convention in Java, we use all-caps names for constants. (Traditionally, constants are also declared as public and static, so that we can access them from outside the file by writing SandLab.METAL, for example.)
Part 2- Constructor
The SandLab constructor (lines 23-30) already initializes the display field to refer to a new SandDisplay with appropriate dimensions and tool names. Insert code to initialize the grid field to refer to a 2-dimensional array of the same dimensions. The rows are the first dimension of the array, and the columns are the second dimension. (You won't be able to test this code yet.)
Part 3- locationClicked Method
The locationClicked method is called (by the run method) whenever the user clicks on some part of the canvas. The selected tool (empty, metal, etc.) is passed to the method. Store this value in the corresponding position of the grid array. (You won't be able to test this code yet.)
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[1], display.getTool());
}
}
}
}

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Explain what is meant by the term 'what if'.

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago