Images
[Agent practical 8 of 9]


This practical we're going to finish our model by making it display our data as an image.


So, finally, we get to making the image. First we need to make a new method in our Environment class that will return an image to us. Add the following method declaration to Environment (make sure Environment also imports java.awt.*; and java.awt.image.*):

   public Image getDataAsImage() {
      // Our Environment code this practical will go here.
   }

Ultimately our algorithm for this method looks like this (we've given you some as real code -- you just need to implement the comments as code):

public Image getDataAsImage() {

   double[] data1Dreranged = get1DArray();

   // Make a int array called "pixels" [data1Dreranged.length]

   for (int i = 0; i < data1Dreranged.length; i++) {

      int value = (int) data1Dreranged[i];

      // Use value to make a new greyscale Color
      // pixel[i] = greyscale Color's compressed int
   }

   // Make a MemoryImageSource, remembering that data.length and
   //   data[0].length give you the height and width of the data.

   // Make an Image object.

   // Return the Image object.

}

Using the lecture materials and Key Ideas page, have a go at converting the algorithm above into code, then go on to displaying the image, where we'll test it.