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):
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:
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"); } } } }