Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.awt.*;
- public class MapDataDrawer
- {
- // store map data in grid array
- private int[][] grid;
- // Read 2D array into grid. Data in file "filename", grid is rows x cols
- public MapDataDrawer(String filename, int rows, int cols) throws Exception{
- Scanner readIn = new Scanner(new File(filename));
- grid = new int[rows][cols];
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- grid[row][col] = readIn.nextInt() ;
- }
- }
- }
- /**
- * @return the min value in the entire grid
- */
- public int findMin(){
- return 0;
- }
- /**
- * @return the max value in the entire grid
- */
- public int findMax(){
- int MAX_VALUE = grid[0][0];
- for (int i = 0; i < grid.length; i++) {
- for (int j = 0; j < grid[i].length; j++) {
- if (grid[i][j] > MAX_VALUE) {
- MAX_VALUE = grid[i][j];
- }
- }
- }
- return MAX_VALUE;
- }
- /**
- * @param col the column of the grid to check
- * @return the index of the row with the lowest value in the given col for the grid
- */
- public int indexOfMinRow(int col){
- int MIN_VALUE = grid[0][0];
- for (int i = 0; i < grid.length; i++)
- {
- for (int j = 0; j < grid[i].length; j++)
- {
- if (grid[i][j] < MIN_VALUE)
- {
- MIN_VALUE = grid[i][j];
- }
- }
- }
- return MIN_VALUE;
- }
- /**
- * DON'T CHANGE THIS CODE, except to uncomment it when you instantiate the grid
- * Draws the grid using the given Graphics object.
- * Colors should be grayscale values 0-255, scaled based on min/max values in grid
- */
- // ******ALERT******
- // Note - until you instantiate a grid, through the constructor, this
- // method will generate a null pointer exception, since there is no grid.length
- // ********************
- public void drawMap(Graphics g){
- int minVal = findMin();
- int maxVal = findMax();
- double range = maxVal - minVal;
- for(int row=0; row < grid.length; row++){
- for(int col=0; col<grid[0].length; col++){
- int val = (int)(((grid[row][col]-minVal)/range) * 255);
- //g.setColor(new Color(val,255-val,255-val));
- g.setColor(new Color(val,val,val));
- g.fillRect(col,row,1,1);
- }
- }
- }
- /**
- * Find a path from West-to-East starting at given row.
- * Choose a foward step out of 3 possible forward locations, using greedy method described in assignment.
- * @return the total change in elevation traveled from West-to-East
- */
- public int drawLowestElevPath(Graphics g, int row){
- int currY = row; // row in grid of step one
- // draw initial step - column 0, current row (sent in as parameter)
- g.fillRect(0,row,1,1);
- // Code to compute next step
- // draw next step where x is currently column and currY is row in grid
- int x = 0; // the value of x will be generated by a loop that goes through the
- // columns, but for now, need something to put in "paint" statement
- g.fillRect(x,currY,1,1);
- return 0; // computed change in elevation
- }
- /**
- * @return the index of the starting row for the lowest-elevation-change path in the entire grid.
- */
- public int indexOfLowestElevPath(Graphics g){
- return 0; // row of path with lowest elevation
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement