Jump to content

Java Array input going insane, helpz Q.Q

Hi ^^

I've got the following problem. As you can see in the code below, I'm supposed to read color information off an image, and put it into an array to later rotate / permutate the image / colors.

Now I really can't get it to work, anyone has an idea what I'll have to fit in? tried so many things I really lost overview of what's pointless and what isn't so I'll input the plain example ^^

import ch.unibas.informatik.cs101.ImageWindow;import java.util.Random;public class ImageArrays {	public static void main(String[] args) {		//create & open the first window (to display the source image)		ImageWindow sourceWindow= new ImageWindow(500,500);		sourceWindow.openWindow("source",0,0);		//load the image 		sourceWindow.loadImage("horn.jpg");			//redraw to see the image		sourceWindow.redraw();		/*  TODO: create an array that is large enough to hold                 *       the complete image information                 */		/*  TODO: store the complete image information in the                 *        array you have created.                 *  HINT: int red=sourceWindow.getPixelRed(xPosition, yPosition);		 *        int green=sourceWindow.getPixelGreen(xPosition, yPosition);		 *        int blue=sourceWindow.getPixelBlue(xPosition, yPosition);	 	 */		//create & open the second windo (to draw your copy into)		ImageWindow destinationWindow= new ImageWindow(500,500);		destinationWindow.openWindow("Image rotated by 90 degree",550,0);		/*  TODO: write back your array data into the destinationWindow so that                 *        it appears to be rotated 90 degrees.                 *  HINT: destinationWindow.setPixel(xPos,yPos,red,green,blue);		 */			                // redraw to see the changed image                destinationWindow.redraw();                // Create another output window		ImageWindow destinationWindow2= new ImageWindow(500,500);		destinationWindow2.openWindow("Image with permuted color channels",0,550);                /*  TODO: call here your function which permutes the color channels                 */                // redraw to see the changed image		destinationWindow2.redraw();	}        /*  TODO: implement here the function to rotate the color channels.         *  HINT: think about the arguments you need for the function.         */}

So, I can't get the color information in or out of an array, which I'm kinda confused how large it should be O.o

[guessing 2500 as its 500x500 pixels, but really I'm kinda lost here]

 

I was really really thankful for any help, going nuts here and should finish this sometime soon ><

 

ty all <3

Frost upon these cigarettes.... lipstick on the window pane...

Link to comment
https://linustechtips.com/topic/65925-java-array-input-going-insane-helpz-qq/
Share on other sites

Link to post
Share on other sites

Could you post some screen shots of running/compiling it? Or does it not compile?

CPU: 5820k 4.5Ghz 1.28v, RAM: 16GB Crucial 2400mhz, Motherboard: Evga X99 Micro, Graphics Card: GTX 780, Water Cooling: EK Acetal CPU/GPU blocks,


240mm Magicool slim rad, 280mm Alphacool rad, D5 Vario pump, 1/4 ID 3/4 OD tubing, Noctua Redux 140/120mm fans. PSU: Evga 750w G2 SSD: Samsung 850 Pro 256GB & Seagate SSHD 2TB Audio: Sennheiser HD558s, JBL! speakers, Fiio E10k DAC/Amp Monitor: Xstar DP2710LED @ 96hz (Korean Monitor) Case: Fractal Node 804

Link to post
Share on other sites

Could you post some screen shots of running/compiling it? Or does it not compile?

 

It compiles as this, opens 3 ImageWindows, one with "horn.jpg" and two empty ones.

 

But I am supposed to make an array, which contains the color information in order to give it out. Says in the "HINTS" within the file I got. But I'm like really confused what I'll have to write so the array would get filled with the information I need to output it afterwards in the 2nd and 3rd window.

 

So I can't get the

         *  HINT: int red=sourceWindow.getPixelRed(xPosition, yPosition);		 *        int green=sourceWindow.getPixelGreen(xPosition, yPosition);		 *        int blue=sourceWindow.getPixelBlue(xPosition, yPosition);

into an array which is one-dimensional [says so in instruction sheet]

 

so I could output:

HINT: destinationWindow.setPixel(xPos,yPos,red,green,blue);

=/

Frost upon these cigarettes.... lipstick on the window pane...

Link to post
Share on other sites

I made a simple 2d array which holds the values of the image. 

import java.awt.Color;public class Test {	public static void main(String[] args) {		Color[][] pixelArray = new Color[500][500]; // Initilize a double array of 500x500, essentially a table with 500 rows and 500 columns		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column] = new Color(						sourceWindow.getPixelRed(row, column),						sourceWindow.getPixelGreen(row, column),						sourceWindow.getPixelBlue(row, column)); //Set the values			}		}	}}

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to post
Share on other sites

public class Test {	public static void main(String[] args) {		ColoredPixel[][] pixelArray = new ColoredPixel[500][500]; // Initilize a double array of 500x500, essentially a table with 500 rows and 500 columns		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column] = new ColoredPixel(						sourceWindow.getPixelRed(row, column),						sourceWindow.getPixelGreen(row, column),						sourceWindow.getPixelBlue(row, column)); //Set the values			}		}	}}/* * A simple wrapper to hold the values */class ColoredPixel {	private final int red;	private final int blue;	private final int green;	public ColoredPixel(int red, int green, int blue) {		this.red = red;		this.blue = blue;		this.green = green;	}	public int getPixelGreen() {		return green;	}	public int getPixelRed() {		return red;	}	public int getPixelBlue() {		return blue;	}}

 

I will try that, tyvm :)

Frost upon these cigarettes.... lipstick on the window pane...

Link to post
Share on other sites

I will try that, tyvm :)

 

Try my new one, made it simpler by using the Color Class >.<

 

To read the values just do the same thing.

import java.awt.Color;public class Test {	public static void main(String[] args) {				//Set the values		Color[][] pixelArray = new Color[500][500]; // Initilize a double array of 500x500, essentially a table with 500 rows and 500 columns		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column] = new Color(						sourceWindow.getPixelRed(row, column),						sourceWindow.getPixelGreen(row, column),						sourceWindow.getPixelBlue(row, column)); //Set the values			}		}						//Read the values		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column].getRed();				pixelArray[row][column].getGreen();				pixelArray[row][column].getBlue();			}		}	}}

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to post
Share on other sites

 

Try my new one, made it simpler by using the Color Class >.<

 

To read the values just do the same thing.

import java.awt.Color;public class Test {	public static void main(String[] args) {				//Set the values		Color[][] pixelArray = new Color[500][500]; // Initilize a double array of 500x500, essentially a table with 500 rows and 500 columns		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column] = new Color(						sourceWindow.getPixelRed(row, column),						sourceWindow.getPixelGreen(row, column),						sourceWindow.getPixelBlue(row, column)); //Set the values			}		}						//Read the values		for (int row = 0; row < 500; row++) {// read the rows in order			for (int column = 0; column <= 500; column++) { // read the columns in order				pixelArray[row][column].getRed();				pixelArray[row][column].getGreen();				pixelArray[row][column].getBlue();			}		}	}}

 

May I ask, I now have the value within an array.

Now I have to get them out with x / y switched.

 

How do I get:

//create & open the second windo (to draw your copy into)		ImageWindow destinationWindow= new ImageWindow(500,500);		destinationWindow.openWindow("Image rotated by 90 degree",550,0);		/*  TODO: write back your array data into the destinationWindow so that                 *        it appears to be rotated 90 degrees.                 *  HINT												 destinationWindow.setPixel(column,row,red,green,blue);:*/ 

to output the picture again, with the values out of the array?

I really tired I completely lost overview, sry to bug you that much :/

Frost upon these cigarettes.... lipstick on the window pane...

Link to post
Share on other sites

May I ask, I now have the value within an array.

Now I have to get them out with x / y switched.

 

How do I get:

//create & open the second windo (to draw your copy into)		ImageWindow destinationWindow= new ImageWindow(500,500);		destinationWindow.openWindow("Image rotated by 90 degree",550,0);		/*  TODO: write back your array data into the destinationWindow so that                 *        it appears to be rotated 90 degrees.                 *  HINT												 destinationWindow.setPixel(column,row,red,green,blue);:*/ 

to output the picture again, with the values out of the array?

I really tired I completely lost overview, sry to bug you that much :/

 

Instead of spoon feeding you the answer you would be best to research how to do it yourself so you gain a deeper understanding of the concept. You would be best to lookup "Rotating a two dimensional array by 90 degrees" or "rotating a matrix in java".

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to post
Share on other sites

Instead of spoon feeding you the answer you would be best to research how to do it yourself so you gain a deeper understanding of the concept. You would be best to lookup "Rotating a two dimensional array by 90 degrees" or "rotating a matrix in java".

 

I'm well aware of that thx. But I was doing different kinds of university work for 14h straight yesterday and decided to do the coding last, since it's the lowest priority. And due to being dead tired and like 5.5h away from getting up again, I thought I might be able to ditch the actual work, since reading up wouldn't do any good anymore =P

 

But thx for your help yesterday ^^

Frost upon these cigarettes.... lipstick on the window pane...

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×