Passing Arrays Between Methods
Background: I am new to Java OO programming and I am currently taking a course in computer graphics. I wrote the program originally within the main method and it executed without any issues. However, I am currently trying to re-write the program to call methods to practice my java programming. My re-designed program does not execute. Please help me!!!
Program function: The assignment was to write a program that drew a line using Bresenham's line algorithm, write the coordinates to an array and then write the array to an image (.png) file.
I'm writing and testing code in Eclipse.
This is the code that dose not execute that calls methods (working single method code below):
Code :
package hw2;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
class hermiteBezier {
//declare variables and arrays to be passed to different methods
int height = 256;
int width = 512;
int image[][][] = new int[3][height][width];
public void main (String[] args){
//call method to create Bresenham's line
bresenhamLine(128,128,108,148,image);
//call method to write image containing line array to file
createPNG(image);
}
//Method to write array to image file
public void createPNG (int[][][] imagePNG) {
// -- write image to file
System.out.println("writing to file");
try {
// -- move image into BufferedImage object
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int pixel = (imagePNG[0][i][j] << 16) | (imagePNG[1][i][j] << 8) | (imagePNG[2][i][j]);
bi.setRGB(j, i, pixel);
}
}
File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
System.out.println("image write error");
}
System.out.println("done");
}
public void bresenhamLine(int x0, int y0, int x1, int y1, int[][][] imageBres) {
// -- imageBres[0][][] is the red plane
// imageBres[1][][] is the green plane
// imageBres[2][][] is the blue plane
int dx;
int dy;
int sx;
int sy;
int err;
int e2;
dx = Math.abs(x1-x0);
dy = Math.abs(y1-y0);
if (x0 < x1) {sx = 1;} else {sx = -1;};
if (y0 < y1) {sy = 1;} else {sy = -1;};
err = dx-dy;
while (x0 != x1){
while (y0 != y1) {
//write location of dot to array
imageBres[0][x0][y0] = 255;
e2 = 2*err;
if (e2 > -dy){
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx){
err = err + dx;
y0 = y0 + sy;
}
}
}
image = imageBres;
}
}
This is my original working code:
Code :
package bLine;
//algorithm from wikipedia, png code from Dr. Reinhart
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
class bressLinev3 {
public static void main (String[] args){
int height = 256;
int width = 512;
// -- Image[0][][] is the red plane
// Image[1][][] is the green plane
// Image[2][][] is the blue plane
int image[][][] = new int[3][height][width];
int version = 600;
int x0 = 90;
int y0 = 256;
int x1 = 98;
int y1 = 168;
int dx;
int dy;
int sx;
int sy;
int err;
int e2;
dx = Math.abs(x1-x0);
dy = Math.abs(y1-y0);
if (x0 < x1) {sx = 1;} else {sx = -1;};
if (y0 < y1) {sy = 1;} else {sy = -1;};
err = dx-dy;
while (x0 != x1){
while (y0 != y1) {
++version;
image[0][x0][y0] = 255;
e2 = 2*err;
if (e2 > -dy){
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx){
err = err + dx;
y0 = y0 + sy;
}
// -- write image to file
System.out.println("writing to file");
try {
// -- move image into BufferedImage object
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int pixel = (image[0][i][j] << 16) | (image[1][i][j] << 8) | (image[2][i][j]);
bi.setRGB(j, i, pixel);
}
}
File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors_"+version+".png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
System.out.println("image write error");
}
System.out.println("done");
}
}
}
}
Re: Passing Arrays Between Methods
Quote:
My re-designed program does not execute.
Do you mean it compiles and then fails or are there compiler errors?
Please copy and paste the full text of the error messages here.
Re: Passing Arrays Between Methods
The pop up window error I receive reads:
"Fatal exception occurred. Program will exit."
In the Eclipse console, the error I get reads:
"java.lang.NoSuchMethodError: main
Exception in thread "main" "
Also, I'm not compiling the program. I'm running the program within the Eclipse IDE. Thanks!
Re: Passing Arrays Between Methods
Normally you have to compile a source before executing it. The IDE is hiding that from you.
The java program is looking for a static method named main that it calls to start execution.
Check that you have the correctly named method with the correct signature.
I'm surprised that the IDE doesn't warn you about it.
Re: Passing Arrays Between Methods
The IDE does not give me any errors, however if I put static in the main method, the IDE gives me an error calling the array named image. The image array is declared outside of the main method in the class, and reason I'm declaring in outside of the main method to pass it to the other methods. This is the first time I'm trying to do all of this, so I may have it wrong.
The error IDE gives me is "Cannot make a static reference to the non-static field image."
Re: Passing Arrays Between Methods
Quote:
IDE gives me an error calling the array named image.
Please copy and paste here the error messages. I can't see them from here.
Re: Passing Arrays Between Methods
These are the errors that I get in the console when I click run:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static field image
Cannot make a static reference to the non-static field image
at hw2.hermiteBezier.main(hermiteBezier.java:19)
Re: Passing Arrays Between Methods
code in static methods can not access variables and methods that only exist when an instance of the class is created.
Two solutions:
make everything static
have the main method create an instance of the class and put the method calls in the constructor of the class. This is the easier solution. It takes 3 lines of code.
Re: Passing Arrays Between Methods
Unfortunately, I do not know how to do that. Could you post the 3 lines of code required or something demonstrating what the code would look like?
Re: Passing Arrays Between Methods
Code :
public static void main (String[] args) {
new ArraysProblem(); // 1 call the constructor
} // end main() // 2
public ArraysProblem() { // 3 The class constructor
//call method to create Bresenham's line
bresenhamLine(128,128,108,148,image);
I renamed your class.
Re: Passing Arrays Between Methods
I tried implementing your code and I received the following error: ArraysProblem cannot be resolved to a type.
Therefore, I tried making everything static. I brought my image array declarations into the main method. However, I back to my original problem of not being able to pass an array. I get the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
image cannot be resolved to a variable
at hw2.hermiteBezier.bresenhamLine(hermiteBezier.java :91)
at hw2.hermiteBezier.main(hermiteBezier.java:19)
The error matches with one of the last lines of code: "image = imageBress;" This is the portion of the code that I hope copies the contents in the imageBress array to the image array.
Code :
package hw2;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
class hermiteBezier {
public static void main (String[] args){
//declare variables and arrays to be passed to different methods
int height = 256;
int width = 512;
int image[][][] = new int[3][height][width];
//call method to create Bresenham's line
bresenhamLine(128,128,108,148,image);
//call method to write image containing line array to file
createPNG(height, width, image);
}
//Method to write array to image file
public static void createPNG (int width, int height, int[][][] imagePNG) {
// -- write image to file
System.out.println("writing to file");
try {
// -- move image into BufferedImage object
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int pixel = (imagePNG[0][i][j] << 16) | (imagePNG[1][i][j] << 8) | (imagePNG[2][i][j]);
bi.setRGB(j, i, pixel);
}
}
File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
System.out.println("image write error");
}
System.out.println("done");
}
public static void bresenhamLine(int x0, int y0, int x1, int y1, int[][][] imageBres) {
// -- imageBres[0][][] is the red plane
// imageBres[1][][] is the green plane
// imageBres[2][][] is the blue plane
int dx;
int dy;
int sx;
int sy;
int err;
int e2;
dx = Math.abs(x1-x0);
dy = Math.abs(y1-y0);
if (x0 < x1) {sx = 1;} else {sx = -1;};
if (y0 < y1) {sy = 1;} else {sy = -1;};
err = dx-dy;
while (x0 != x1){
while (y0 != y1) {
//write location of dot to array
imageBres[0][x0][y0] = 255;
e2 = 2*err;
if (e2 > -dy){
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx){
err = err + dx;
y0 = y0 + sy;
}
}
}
image = imageBres;
}
}
Re: Passing Arrays Between Methods
Quote:
I received the following error: ArraysProblem cannot be resolved to a type.
I renamed your class for my tests. Replace ArraysProblem with the name that you have.
Re: Passing Arrays Between Methods
By the way, thank you for your help so far!!!
Re: Passing Arrays Between Methods
That'll be it for tonight. Back tomorrow.
Re: Passing Arrays Between Methods
Actually, that finally got everything working!!! I was able to implement code and generate a png file. Thanks for your help tonight and have a good weekend.