/** * --Copyright notice-- * * Copyright (c) School of Geography, University of Leeds. * http://www.geog.leeds.ac.uk/ * This software is licensed under 'The Artistic License' which can be found at * the Open Source Initiative website at... * http://www.opensource.org/licenses/artistic-license.php * Please note that the optional Clause 8 does not apply to this code. * * The Standard Version source code, and associated documentation can be found at... * [online] http://mass.leeds.ac.uk/ * * * --End of Copyright notice-- * */ import java.awt.image.*; import java.awt.*; import java.io.Serializable; /** * Holds environmental data as a raster for the model. * @version 1.0 * @author Andy Evans */ public class Environment implements Serializable { /** Width of environment. */ private int width = 300; /** Height of environment. */ private int height = 200; /** Raster dataset for environment. */ private double[][] data = null; /** * Gets the width, as one might expect. * @return current environment width */ public int getWidth() { return width; } /** * Gets the height, as one might expect. * @return current environment height */ public int getHeight() { return height; } /** * Sets the data array. * @param data 2D data array */ public void setData(double[][] data) { this.data = data; height = data.length; width = data[0].length; } /** * Gets the data array. * @return the 2D data array */ public double[][] getData() { return data; } /** * Mutator method for a single data point. * @param x location across a row * @param y location down a column / row number * @param value value to set at position y,x */ public void setDataValue (int x, int y, double value) { data[y][x] = value; } /** * Accessor method for a single data point. * @param x location across a row * @param y location down a column / row number * @return value at position y,x */ public double getDataValue (int x, int y) { return data[y][x]; } /** * Method to find the maximum value in an array. * @return maximum value */ public double getMaximum () { double maximum = data[0][0]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { if (data[i][j] > maximum) maximum = data[i][j]; } } return maximum; } /** * Method to find the minimum value in an array. * @return minimum value */ public double getMinimum () { double minimum = data[0][0]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { if (data[i][j] < minimum) minimum = data[i][j]; } } return minimum; } /** * Method to find the rerange data in the data array. * @param newMinimum new minimum for range * @param newMaximum new maximum for range * @return reranged data */ double[][] getRerangedData (double newMinimum, double newMaximum) { double currentMaximum = getMaximum(); double currentMinimum = getMinimum(); double[][] tempArray = new double[data.length][data[0].length]; if ((currentMaximum - currentMinimum) == 0.0) return tempArray; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { tempArray[i][j] = data[i][j]; tempArray[i][j] = tempArray[i][j] - currentMinimum; tempArray[i][j] = tempArray[i][j] / (currentMaximum - currentMinimum); tempArray[i][j] = tempArray[i][j] * (newMaximum - newMinimum); tempArray[i][j] = tempArray[i][j] + newMinimum; } } return tempArray; } /** * 2D data array to 1D array conversion. * @return 1D array */ double[] get1DData () { double[][] reranged = getRerangedData(0.0, 255.0); double tempArray[] = new double[reranged.length * reranged[0].length]; for (int i = 0; i < reranged.length; i++) { for (int j = 0; j < reranged[i].length; j++) { tempArray[(i * reranged[0].length) + j] = reranged[i][j]; } } return tempArray; } /** * Gets current data as an image. * @return data as image */ public Image getDataAsImage() { if (data == null) return null; double [] data1Dreranged = get1DData(); int[] pixels = new int [data1Dreranged.length]; for (int i = 0; i < pixels.length; i++) { int value = (int) data1Dreranged[i]; Color c = new Color(value,value,value); pixels[i] = c.getRGB(); } MemoryImageSource m = new MemoryImageSource(data[0].length, data.length, pixels, 0, data[0].length); Panel panel = new Panel(); Image image = panel.createImage(m); return image; } }