I have a question about to add a "Pixel" in a LinkedList.
There is class for a black and white picture (class called "Bild"). When u create an object of the picture u have to say how the height and the width of the picture is. With the width u also say how big the pixel-array in the picture object is. So the pictures width is as long as the length of the array!
In every element of the array should be the columns of the picture as a linked list of "Pixel". But in the linked list are only the black pixels safed! Every "Pixel" has a int for the row where it is placed in the picture. The white pixels are not safed!
finished methods:
public Pixel pixelAt(int zeile, int spalte) This method gives back a back "Pixel" in the row/column of the "Picture". If there is no black "Pixel" it returns null.
public void printBild() is printing a "*" for a black pixel and a "-" when there is a white pixel.
not finished method:
- public void pixelOn(int zeile, int spalte)
This method should check if there is a black pixel in that row/column. here i can use the method pixelAt(). A white pixel should be changed to a black pixel, so there is a pixel added to the linked list. If the pixel is already black nothing should happen.
After i call printBild() again a white pixel "_" is still white and not black "*".
...
public class Pixel {
private int zeile;
private Pixel next;
public Pixel ( int zeile, Pixel p ) {
this.zeile = zeile;
this.next = p;
}
public int getZeile() {
return zeile;
}
public Pixel getNext() {
return next;
}
// Setze Verweis auf nächsten schwarzen Pixel
public void setNext( Pixel p) {
next = p;
}
}
public class Bild {
private Pixel [] data;
private int hoehe, breite;
public Bild (int hoehe, int breite) {
this.data = new Pixel [breite];
this.hoehe = hoehe;
this.breite = breite;
}
public void setData(Pixel [] temp) {
this.data = temp;
}
public Pixel pixelAt(int zeile, int spalte) {
Pixel iterator = data[spalte];
if(iterator.getZeile() == zeile) {
return iterator;
}
else {
while (iterator.getNext() != null) {
iterator = iterator.getNext();
if(iterator.getZeile() == zeile) {
return iterator;
}
}
}
return null;
}
public void printBild() {
String leer = "";
String [] hi = new String [hoehe];
for (int k = 0; k < hoehe; k++) {
hi[k] = leer;
}
for (int i = 0; i < data.length; i++) {
Pixel iterator = data[i];
for (int j = 0; j < hoehe ; j++) {
if (iterator == null) {
hi[j] += " - ";
}
else {
if (iterator.getZeile() == j) {
hi[j] += " * ";
iterator = iterator.getNext();
}
else {
hi[j] += " - ";
}
}
}
}
for (int q = 0; q < hi.length; q++) {
System.out.println(hi[q]);
}
}
public void pixelOn(int zeile, int spalte) {
if (pixelAt(zeile, spalte) == null) {
Pixel it = data[spalte];
for (int i = 1; i < zeile; i++) {
it = it.getNext();
}
Pixel tmp = it.getNext();
Pixel neu = new Pixel(zeile, null);
it.setNext(neu);
neu.setNext(tmp);
}
}
}
public class TestBild {
public static void main(String[] args) {
Pixel a1 = new Pixel(2, null);
Pixel b1 = new Pixel(0, null);
Pixel b2 = new Pixel(1,null);
b1.setNext(b2);
Pixel b3 = new Pixel (3, null);
b2.setNext(b3);
Pixel c1 = new Pixel(1, null);
Pixel c2 = new Pixel(2,null);
c1.setNext(c2);
Pixel [] temp = new Pixel [3];
temp[0] = a1;
temp[1] = b1;
temp[2] = c1;
Bild BW = new Bild(4,3);
BW.setData(temp);
BW.printBild();
BW.pixelOn(2, 0);
System.out.println();
BW.printBild();
}
}
...
Thank you!