DocEW
2007-10-16, 10:37:58
Hallo zusammen,
ich habe ein kleines (Verständnis?-)Problem mit dem java.awt.image.PixelGrabber.
Folgender Code sollte m.E. zwei horizontale Linien (aus '0' und '1' bestehend) in der Konsole ausgeben, tut er aber nicht. :(
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
public class PixelGrabberTest {
public void rasterize(int totalWidth, int totalHeight) {
Image image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
paint(graphics);
int[] pixels = new int[totalWidth*totalHeight];
PixelGrabber pg = new PixelGrabber(image, 0, 0, totalWidth, totalHeight, pixels, 0, totalWidth);
try {
pg.grabPixels();
if (pixels != null) {
for(int i = 0; i < totalHeight; i++) {
for (int j = 0; j < totalWidth; j++) {
System.out.print((pixels[j+i*totalHeight] & 1));
}
System.out.println();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawLine(0,0,10,0);
g.drawLine(0,2,10,2);
}
public static void main(String[] args) {
PixelGrabberTest pixelGrabberTest = new PixelGrabberTest();
pixelGrabberTest.rasterize(20, 5);
}
}
Die Ausgabe ist merkwürdig verschoben:
11111111111000000000
11111100000000000000
10000000000000000000
00000000000000000000
00000000000000000000
Ich habe schon einiges ge-Google-d und getestet, krieg's aber nicht hin.
Für Hilfe wäre ich sehr dankbar!
DocEW
ich habe ein kleines (Verständnis?-)Problem mit dem java.awt.image.PixelGrabber.
Folgender Code sollte m.E. zwei horizontale Linien (aus '0' und '1' bestehend) in der Konsole ausgeben, tut er aber nicht. :(
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
public class PixelGrabberTest {
public void rasterize(int totalWidth, int totalHeight) {
Image image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
paint(graphics);
int[] pixels = new int[totalWidth*totalHeight];
PixelGrabber pg = new PixelGrabber(image, 0, 0, totalWidth, totalHeight, pixels, 0, totalWidth);
try {
pg.grabPixels();
if (pixels != null) {
for(int i = 0; i < totalHeight; i++) {
for (int j = 0; j < totalWidth; j++) {
System.out.print((pixels[j+i*totalHeight] & 1));
}
System.out.println();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawLine(0,0,10,0);
g.drawLine(0,2,10,2);
}
public static void main(String[] args) {
PixelGrabberTest pixelGrabberTest = new PixelGrabberTest();
pixelGrabberTest.rasterize(20, 5);
}
}
Die Ausgabe ist merkwürdig verschoben:
11111111111000000000
11111100000000000000
10000000000000000000
00000000000000000000
00000000000000000000
Ich habe schon einiges ge-Google-d und getestet, krieg's aber nicht hin.
Für Hilfe wäre ich sehr dankbar!
DocEW