S
stublair
Hi all,
I have a raster file landgrey.raster- essentially a 300 x 300 byte array
of pixel values where each value represents a different landuse type. The
file looks something like this:
100 100 100 100…..
100 83 83 100….
100 83 83 100…
100 100 100 100…..
where the value 100 represents grass and the value 83 represents cars. The
full list of different landuse objects within the raster file are given
below:
133 = Building
83 = Cars
48 = Roads
100 = Grass
73 = Trees
I have written a class - OpenRasterListnr.java - which opens up the
landgrey.raster and displays the 300 x 300 file in greyscale, the code
for which is given below:
package Java;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
class OpenRasterListnr implements ActionListener {
private ImagePanel panel = null;
OpenRasterListnr (ImagePanel pn) {
panel = pn;
}
public void actionPerformed (ActionEvent ae) {
FileDialog openDialog = new FileDialog(new Frame(), "Open file",
FileDialog.LOAD);
openDialog.show();
File file = new File(openDialog.getDirectory() + openDialog.getFile());
if ((openDialog.getDirectory() == null) || (openDialog.getFile() ==
null)) return;
//-------------------
//FileDialog saveDialog = new FileDialog(new Frame(), "Save file",
FileDialog.SAVE);
//saveDialog.show();
String fileName = openDialog.getDirectory() + openDialog.getFile();
//File fileName = new File (saveDialog.getDirectory() +
saveDialog.getFile());
//Image ourImage = panel.getDisplayImage();
int width = 300;//ourImage.getWidth(panel);
int height = 300;//ourImage.getHeight(panel);
int x = 0;
int y = 0;
int[] pixels = new int[width * height];
//try
//{
//---------------------
//FileWriter fw = new FileWriter(fileName);
File f = null;
ReaderThing r = null;
// Code to read integer arrays.
f = new File("c:/Java/landgrey.raster");
//f = new File(file);
r = new ReaderThing(f, true);
//int [][] d = r.getTwoDintArray();
int [] a = r.getOneDintArray();
//----------------------
//sets yourpixelarray to same length as pixels
//int[] a = new int[width * height];
int [][] d = new int[width][height];
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
// get compressed int out of pixels at (j * width) + i
// make a new color object from that int
Color newColor = new Color (a[j * width + i]); //for 1D array
//Color newColor = new Color (d[j]);
//a[j * width + i] = d[j];
int red = newColor.getRed();
int green = newColor.getGreen();
int blue = newColor.getBlue();
double temp = 0.56 * (double)green + 0.33 *(double) red + 0.11
*(double) blue;
newColor = new Color((int)temp,(int)temp,(int)temp);
int compressedInt = newColor.getRGB();
String grey = String.valueOf(temp);
//fw.write(grey + " ");
// put int back in pixels
a[j * width + i] = compressedInt;
d[j]=compressedInt;
}
}
//fw.close();
Image ourImage = panel.getToolkit().createImage(new
MemoryImageSource(width, height, a, 0, width));
panel.setDisplayImage(ourImage);
panel.repaint();
//}catch (IOException e){
//e.printStackTrace();
//}
}
}
For those interested ReaderThing.java is given below:
package Java;
import java.io.*;
import java.util.*;
import java.awt.Color;
/**
* This Class takes in a file object in its constructor and turns it into
an 2D array.<P>
* It also includes a method to get a 1D compressed int array for making
images and 1D String arrays.
* @author Jonathan Gill (class of 2000, now working at GMap and still
coding, lucky man!) and Andy Evans.<P>
* @version 1.2
**/
public class ReaderThing {
private int [][] twoDintArray = null;
private String [][] twoDStringArray = null;
private int width, height = 0;
public ReaderThing(File file, boolean convertToInts){
FileReader filer = null;
StringTokenizer st = null;
String temp = null;
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String line = buff.readLine();
// There seems to be a problem on some systems where
// end of lines in text files are recognised by java as
// a second, blank, line. We get rid of these by checking the
// length of the line after calling trim(). Trim is a method
// in every String class object, which removes the spaces
// from its start and end.
if (line != null) {
line.trim();
if (line.length() != 0){
try {
st = new StringTokenizer(line, ",");
temp = st.nextToken();
// Integer.parseInt() converts Strings to ints.
width = Integer.parseInt(temp);
temp = st.nextToken();
height = Integer.parseInt(temp);
} catch(NumberFormatException e){
System.out.println("Invalid Format: " +
"First line must be width,height");
}
}
}
twoDintArray = new int[width][height];
twoDStringArray = new String[width][height];
line = buff.readLine();
// Get rid of any blank lines.
if (line != null) {
line.trim();
if (line.length() == 0) line = buff.readLine();
}
while (line != null){
for (int i = 0; i < height; i++) {
st = new StringTokenizer(line, ", ");
for (int j = 0; j < width; j++) {
twoDStringArray[j] = st.nextToken();
if (convertToInts == true)
{
try {
int z =
Integer.parseInt(twoDStringArray[j]);
twoDintArray[j]
= z;
}
catch(NumberFormatException e) {
twoDintArray[j]
= 0;
}
}
}
line = buff.readLine();
if (line != null) {
line.trim();
if (line.length() == 0) line = buff.readLine();
}
}
}
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int[][] getTwoDintArray() {
return twoDintArray;
}
public String[][] getTwoDStringArray() {
return twoDStringArray;
}
public int[] getOneDintArray() {
int [] oneDintArray = new int [width * height];
for (int a = 0; a < width; a++) {
for (int b = 0; b < height; b++) {
int temp = twoDintArray[a];
Color tempColor = new Color(temp,temp,temp);
oneDintArray [(b*width) + a] = tempColor.getRGB();
}
}
return oneDintArray;
}
public String[] getOneDStringArray() {
String[] oneDStringArray = new String [width * height];
for (int a = 0; a < width; a++) {
for (int b = 0; b < height; b++) {
oneDStringArray [(b*width) + a] = twoDStringArray[a];
}
}
return oneDStringArray;
}
}
As part of the requirements of the programme I am making, the colours
associated with each land-use object have to be set via a point class
which uses get and set methods such as the one below (I realise that what
follows is not right – what I am basically trying to do is to set each of
the values in the landgrey.raster file to an object type – i.e int
buildings = 133; which can then be called in the OpenRasterListnr.java
file using a get/set method – whichever is appropriate – to display each of
the above landuses in the following colours
Buildings = red (255, 0, 0)
Cars = black (0, 0, 0)
Roads = grey (192, 192, 192)
Grass = green (0, 255, 0)
Trees = dark green (0, 128, 0)
In a kind of garbled way I have attempted the code to do this though it is
obviously wrong. I know that I will need a new class – say MakeColour.java
– which will essentially be the same as OpenRaster.Listnr.java but will
have the additional code to display the image in colour instead of
greyscale. The code I have attempted to write so far is in lines 69 - 96
of the following code given below:
package Java;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
class MakeColour implements ActionListener {
private ImagePanel panel = null;
MakeColour (ImagePanel pn) {
panel = pn;
}
public void actionPerformed (ActionEvent ae) {
FileDialog openDialog = new FileDialog(new Frame(), "Open file",
FileDialog.LOAD);
openDialog.show();
File file = new File(openDialog.getDirectory() + openDialog.getFile());
if ((openDialog.getDirectory() == null) || (openDialog.getFile() ==
null)) return;
//-------------------
//FileDialog saveDialog = new FileDialog(new Frame(), "Save file",
FileDialog.SAVE);
//saveDialog.show();
String fileName = openDialog.getDirectory() + openDialog.getFile();
//File fileName = new File (saveDialog.getDirectory() +
saveDialog.getFile());
//Image ourImage = panel.getDisplayImage();
int width = 300;//ourImage.getWidth(panel);
int height = 300;//ourImage.getHeight(panel);
int x = 0;
int y = 0;
int[] pixels = new int[width * height];
//try
//{
//---------------------
//FileWriter fw = new FileWriter(fileName);
File f = null;
ReaderThing r = null;
// Code to read integer arrays.
f = new File("c:/Java/landgrey.raster");
//f = new File(file);
r = new ReaderThing(f, true);
//int [][] d = r.getTwoDintArray();
int [] a = r.getOneDintArray();
//----------------------
//sets yourpixelarray to same length as pixels
//int[] a = new int[width * height];
int [][] d = new int[width][height];
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
Point secondColor = new Point (pixels[j * width + i]);
int a = secondColor.setBuilding(Color.RED); // make buildings red
int b = secondColor.setCars(Color.BLACK); // make cars black
int c = secondColor.setRoads(Color.GRAY); //make roads grey
int d = secondColor.setGrass(Color.GREEN); //make grass green
int e = secondColor.setTrees(Color.YELLOW); //make trees yellow
double tempt = (double)a + (double)b + (double)c + (double)d +
(double)e;
secondColor = new Color((int)tempt,(int)tempt,(int)tempt, (int)tempt,
(int)tempt);
int compressedInt2 = secondColor.getRGB();
String notgrey = String.valueOf(tempt);
fw.write(notgrey + " ");
// put int back in pixels
pixels[j * width + i] = compressedInt2;
String grey = String.valueOf(temp);
//fw.write(grey + " ");
// put int back in pixels
a[j * width + i] = compressedInt2;
d[j]=compressedInt2;
}
}
//fw.close();
Image ourImage = panel.getToolkit().createImage(new
MemoryImageSource(width, height, a, 0, width));
panel.setDisplayImage(ourImage);
panel.repaint();
//}catch (IOException e){
//e.printStackTrace();
//}
}
}
any help on this one would be greatly greatly appreciated - if anyone
wants the rest of the code to see how bits are working email me!
stu
x
I have a raster file landgrey.raster- essentially a 300 x 300 byte array
of pixel values where each value represents a different landuse type. The
file looks something like this:
100 100 100 100…..
100 83 83 100….
100 83 83 100…
100 100 100 100…..
where the value 100 represents grass and the value 83 represents cars. The
full list of different landuse objects within the raster file are given
below:
133 = Building
83 = Cars
48 = Roads
100 = Grass
73 = Trees
I have written a class - OpenRasterListnr.java - which opens up the
landgrey.raster and displays the 300 x 300 file in greyscale, the code
for which is given below:
package Java;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
class OpenRasterListnr implements ActionListener {
private ImagePanel panel = null;
OpenRasterListnr (ImagePanel pn) {
panel = pn;
}
public void actionPerformed (ActionEvent ae) {
FileDialog openDialog = new FileDialog(new Frame(), "Open file",
FileDialog.LOAD);
openDialog.show();
File file = new File(openDialog.getDirectory() + openDialog.getFile());
if ((openDialog.getDirectory() == null) || (openDialog.getFile() ==
null)) return;
//-------------------
//FileDialog saveDialog = new FileDialog(new Frame(), "Save file",
FileDialog.SAVE);
//saveDialog.show();
String fileName = openDialog.getDirectory() + openDialog.getFile();
//File fileName = new File (saveDialog.getDirectory() +
saveDialog.getFile());
//Image ourImage = panel.getDisplayImage();
int width = 300;//ourImage.getWidth(panel);
int height = 300;//ourImage.getHeight(panel);
int x = 0;
int y = 0;
int[] pixels = new int[width * height];
//try
//{
//---------------------
//FileWriter fw = new FileWriter(fileName);
File f = null;
ReaderThing r = null;
// Code to read integer arrays.
f = new File("c:/Java/landgrey.raster");
//f = new File(file);
r = new ReaderThing(f, true);
//int [][] d = r.getTwoDintArray();
int [] a = r.getOneDintArray();
//----------------------
//sets yourpixelarray to same length as pixels
//int[] a = new int[width * height];
int [][] d = new int[width][height];
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
// get compressed int out of pixels at (j * width) + i
// make a new color object from that int
Color newColor = new Color (a[j * width + i]); //for 1D array
//Color newColor = new Color (d[j]);
//a[j * width + i] = d[j];
int red = newColor.getRed();
int green = newColor.getGreen();
int blue = newColor.getBlue();
double temp = 0.56 * (double)green + 0.33 *(double) red + 0.11
*(double) blue;
newColor = new Color((int)temp,(int)temp,(int)temp);
int compressedInt = newColor.getRGB();
String grey = String.valueOf(temp);
//fw.write(grey + " ");
// put int back in pixels
a[j * width + i] = compressedInt;
d[j]=compressedInt;
}
}
//fw.close();
Image ourImage = panel.getToolkit().createImage(new
MemoryImageSource(width, height, a, 0, width));
panel.setDisplayImage(ourImage);
panel.repaint();
//}catch (IOException e){
//e.printStackTrace();
//}
}
}
For those interested ReaderThing.java is given below:
package Java;
import java.io.*;
import java.util.*;
import java.awt.Color;
/**
* This Class takes in a file object in its constructor and turns it into
an 2D array.<P>
* It also includes a method to get a 1D compressed int array for making
images and 1D String arrays.
* @author Jonathan Gill (class of 2000, now working at GMap and still
coding, lucky man!) and Andy Evans.<P>
* @version 1.2
**/
public class ReaderThing {
private int [][] twoDintArray = null;
private String [][] twoDStringArray = null;
private int width, height = 0;
public ReaderThing(File file, boolean convertToInts){
FileReader filer = null;
StringTokenizer st = null;
String temp = null;
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String line = buff.readLine();
// There seems to be a problem on some systems where
// end of lines in text files are recognised by java as
// a second, blank, line. We get rid of these by checking the
// length of the line after calling trim(). Trim is a method
// in every String class object, which removes the spaces
// from its start and end.
if (line != null) {
line.trim();
if (line.length() != 0){
try {
st = new StringTokenizer(line, ",");
temp = st.nextToken();
// Integer.parseInt() converts Strings to ints.
width = Integer.parseInt(temp);
temp = st.nextToken();
height = Integer.parseInt(temp);
} catch(NumberFormatException e){
System.out.println("Invalid Format: " +
"First line must be width,height");
}
}
}
twoDintArray = new int[width][height];
twoDStringArray = new String[width][height];
line = buff.readLine();
// Get rid of any blank lines.
if (line != null) {
line.trim();
if (line.length() == 0) line = buff.readLine();
}
while (line != null){
for (int i = 0; i < height; i++) {
st = new StringTokenizer(line, ", ");
for (int j = 0; j < width; j++) {
twoDStringArray[j] = st.nextToken();
if (convertToInts == true)
{
try {
int z =
Integer.parseInt(twoDStringArray[j]);
twoDintArray[j]
= z;
}
catch(NumberFormatException e) {
twoDintArray[j]
= 0;
}
}
}
line = buff.readLine();
if (line != null) {
line.trim();
if (line.length() == 0) line = buff.readLine();
}
}
}
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int[][] getTwoDintArray() {
return twoDintArray;
}
public String[][] getTwoDStringArray() {
return twoDStringArray;
}
public int[] getOneDintArray() {
int [] oneDintArray = new int [width * height];
for (int a = 0; a < width; a++) {
for (int b = 0; b < height; b++) {
int temp = twoDintArray[a];
Color tempColor = new Color(temp,temp,temp);
oneDintArray [(b*width) + a] = tempColor.getRGB();
}
}
return oneDintArray;
}
public String[] getOneDStringArray() {
String[] oneDStringArray = new String [width * height];
for (int a = 0; a < width; a++) {
for (int b = 0; b < height; b++) {
oneDStringArray [(b*width) + a] = twoDStringArray[a];
}
}
return oneDStringArray;
}
}
As part of the requirements of the programme I am making, the colours
associated with each land-use object have to be set via a point class
which uses get and set methods such as the one below (I realise that what
follows is not right – what I am basically trying to do is to set each of
the values in the landgrey.raster file to an object type – i.e int
buildings = 133; which can then be called in the OpenRasterListnr.java
file using a get/set method – whichever is appropriate – to display each of
the above landuses in the following colours
Buildings = red (255, 0, 0)
Cars = black (0, 0, 0)
Roads = grey (192, 192, 192)
Grass = green (0, 255, 0)
Trees = dark green (0, 128, 0)
In a kind of garbled way I have attempted the code to do this though it is
obviously wrong. I know that I will need a new class – say MakeColour.java
– which will essentially be the same as OpenRaster.Listnr.java but will
have the additional code to display the image in colour instead of
greyscale. The code I have attempted to write so far is in lines 69 - 96
of the following code given below:
package Java;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
class MakeColour implements ActionListener {
private ImagePanel panel = null;
MakeColour (ImagePanel pn) {
panel = pn;
}
public void actionPerformed (ActionEvent ae) {
FileDialog openDialog = new FileDialog(new Frame(), "Open file",
FileDialog.LOAD);
openDialog.show();
File file = new File(openDialog.getDirectory() + openDialog.getFile());
if ((openDialog.getDirectory() == null) || (openDialog.getFile() ==
null)) return;
//-------------------
//FileDialog saveDialog = new FileDialog(new Frame(), "Save file",
FileDialog.SAVE);
//saveDialog.show();
String fileName = openDialog.getDirectory() + openDialog.getFile();
//File fileName = new File (saveDialog.getDirectory() +
saveDialog.getFile());
//Image ourImage = panel.getDisplayImage();
int width = 300;//ourImage.getWidth(panel);
int height = 300;//ourImage.getHeight(panel);
int x = 0;
int y = 0;
int[] pixels = new int[width * height];
//try
//{
//---------------------
//FileWriter fw = new FileWriter(fileName);
File f = null;
ReaderThing r = null;
// Code to read integer arrays.
f = new File("c:/Java/landgrey.raster");
//f = new File(file);
r = new ReaderThing(f, true);
//int [][] d = r.getTwoDintArray();
int [] a = r.getOneDintArray();
//----------------------
//sets yourpixelarray to same length as pixels
//int[] a = new int[width * height];
int [][] d = new int[width][height];
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
Point secondColor = new Point (pixels[j * width + i]);
int a = secondColor.setBuilding(Color.RED); // make buildings red
int b = secondColor.setCars(Color.BLACK); // make cars black
int c = secondColor.setRoads(Color.GRAY); //make roads grey
int d = secondColor.setGrass(Color.GREEN); //make grass green
int e = secondColor.setTrees(Color.YELLOW); //make trees yellow
double tempt = (double)a + (double)b + (double)c + (double)d +
(double)e;
secondColor = new Color((int)tempt,(int)tempt,(int)tempt, (int)tempt,
(int)tempt);
int compressedInt2 = secondColor.getRGB();
String notgrey = String.valueOf(tempt);
fw.write(notgrey + " ");
// put int back in pixels
pixels[j * width + i] = compressedInt2;
String grey = String.valueOf(temp);
//fw.write(grey + " ");
// put int back in pixels
a[j * width + i] = compressedInt2;
d[j]=compressedInt2;
}
}
//fw.close();
Image ourImage = panel.getToolkit().createImage(new
MemoryImageSource(width, height, a, 0, width));
panel.setDisplayImage(ourImage);
panel.repaint();
//}catch (IOException e){
//e.printStackTrace();
//}
}
}
any help on this one would be greatly greatly appreciated - if anyone
wants the rest of the code to see how bits are working email me!
stu
x