Advertisement
Dragon105801

Java5

Mar 28th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.awt.*;
  4.  
  5. public class MapDataDrawer
  6. {
  7.   // store map data in grid array
  8.   private int[][] grid;
  9.  
  10.   // Read 2D array into grid. Data in file "filename", grid is rows x cols
  11.   public MapDataDrawer(String filename, int rows, int cols) throws Exception{
  12.         Scanner readIn = new Scanner(new File(filename));
  13.         grid = new int[rows][cols];
  14.         for (int row = 0; row < rows; row++) {
  15.             for (int col = 0; col < cols; col++) {
  16.                 grid[row][col] = readIn.nextInt() ;
  17.             }
  18.         }
  19.     }
  20.  
  21.   /**
  22.    * @return the min value in the entire grid
  23.    */
  24.   public int findMin(){
  25.  
  26.       return 0;  
  27.   }
  28.  
  29.   /**
  30.    * @return the max value in the entire grid
  31.    */
  32.   public int findMax(){
  33.       int MAX_VALUE = grid[0][0];
  34.         for (int i = 0; i < grid.length; i++) {
  35.             for (int j = 0; j < grid[i].length; j++) {
  36.                 if (grid[i][j] > MAX_VALUE) {
  37.                     MAX_VALUE = grid[i][j];
  38.                 }
  39.             }
  40.         }
  41.       return MAX_VALUE;
  42.   }
  43.  
  44.   /**
  45.    * @param col the column of the grid to check
  46.    * @return the index of the row with the lowest value in the given col for the grid
  47.    */
  48.   public int indexOfMinRow(int col){
  49.       int MIN_VALUE = grid[0][0];
  50.         for (int i = 0; i < grid.length; i++)
  51.         {
  52.             for (int j = 0; j < grid[i].length; j++)
  53.             {
  54.                 if (grid[i][j] < MIN_VALUE)
  55.                 {
  56.                     MIN_VALUE = grid[i][j];
  57.                 }
  58.             }
  59.         }
  60.       return MIN_VALUE;  
  61.   }
  62.  
  63.  
  64.   /**
  65.    * DON'T CHANGE THIS CODE, except to uncomment it when you instantiate the grid
  66.    * Draws the grid using the given Graphics object.
  67.    * Colors should be grayscale values 0-255, scaled based on min/max values in grid
  68.    */
  69.  
  70.   // ******ALERT******
  71.   // Note - until you instantiate a grid, through the constructor, this
  72.   // method will generate a null pointer exception, since there is no grid.length
  73.   // ********************
  74.   public void drawMap(Graphics g){
  75.      
  76.     int minVal = findMin();
  77.     int maxVal = findMax();
  78.     double range = maxVal - minVal;
  79.    
  80.     for(int row=0; row < grid.length; row++){
  81.       for(int col=0; col<grid[0].length; col++){
  82.          int val = (int)(((grid[row][col]-minVal)/range) * 255);
  83.          //g.setColor(new Color(val,255-val,255-val));
  84.          g.setColor(new Color(val,val,val));
  85.          g.fillRect(col,row,1,1);
  86.         }
  87.     }      
  88.   }
  89.  
  90.  
  91.    /**
  92.    * Find a path from West-to-East starting at given row.
  93.    * Choose a foward step out of 3 possible forward locations, using greedy method described in assignment.
  94.    * @return the total change in elevation traveled from West-to-East
  95.    */
  96.   public int drawLowestElevPath(Graphics g, int row){
  97.     int currY = row; // row in grid of step one
  98.     // draw initial step - column 0, current row (sent in as parameter)
  99.     g.fillRect(0,row,1,1);
  100.    
  101.     // Code to compute next step
  102.    
  103.     // draw next step where x is currently column and currY is row in grid
  104.     int x = 0; // the value of x will be generated by a loop that goes through the
  105.     // columns, but for now, need something to put in "paint" statement
  106.     g.fillRect(x,currY,1,1);
  107.    
  108.     return 0; // computed change in elevation
  109.   }
  110.  
  111.   /**
  112.    * @return the index of the starting row for the lowest-elevation-change path in the entire grid.
  113.    */
  114.   public int indexOfLowestElevPath(Graphics g){
  115.      
  116.       return 0; // row of path with lowest elevation
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement