Hallo,
ich habe ein Programm geschrieben, das aus einem png-Bild die Randpixel eines weißen Motivs mit schwarzem Hintergrund ausliest. Alle Methoden funktionieren einwandfrei, nur paint will einfach nicht die übergebenen Punkte in mein Fenster zeichnen. Kenne mich mit Java leider nicht so gut aus und habe schon alles probiert, bekomme aber nur eine Fehlermeldung nach der anderen. Wäre sehr dankbar für einen Lösungsvorschlag.
ich habe ein Programm geschrieben, das aus einem png-Bild die Randpixel eines weißen Motivs mit schwarzem Hintergrund ausliest. Alle Methoden funktionieren einwandfrei, nur paint will einfach nicht die übergebenen Punkte in mein Fenster zeichnen. Kenne mich mit Java leider nicht so gut aus und habe schon alles probiert, bekomme aber nur eine Fehlermeldung nach der anderen. Wäre sehr dankbar für einen Lösungsvorschlag.
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Monster extends Frame {
public static int maxWidth=0;
public static int minWidth=10000;
public static int maxHeight=0;
public static int minHeight=10000;
public Monster (int width, int height) {
setSize(width, height);
setTitle("Monster");
setBackground(Color.black);
addWindowListener( new WindowAdapter() {
@Override
public void windowClosing ( WindowEvent e ) { System.exit( 0 ); }
} );
}
public static void main(String[] args) {
int counter=0;
int width=600;
int height=600;
Graphics g=null;
File file= new File("c:\\monster.png");
Image image=fileToImage(file);
int sourceWidth=image.getWidth(null);
int sourceHeight=image.getHeight(null);
checkSize(image, sourceWidth, sourceHeight);
new Monster(width, height).setVisible(true);
for (int i=0; i<sourceHeight; i++) {
for (int j=0; j<sourceWidth; j++) {
if (checkPixels(image, j, i)==true && checkNeighbourhood(image, j, i)==true) {
counter++;
try {
System.out.println("x=" + j + " y=" + i);
paint(g, j, i);
} catch (NullPointerException e) {
}
}
}
}
System.out.println("Insegesamt " + counter + " Randpunkte");
}
public static void paint (Graphics g, int x, int y) {
g.setColor(Color.green);
g.drawLine(x, y, x, y);
}
public static void checkSize(Image image, int width, int height) {
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
if (checkPixels(image, i, j)==true) {
if (i<minWidth) {
minWidth=i;
}
if (i>maxWidth) {
maxWidth=i;
}
if (j>maxHeight) {
maxHeight=j;
}
if (j<minHeight) {
minHeight=j;
}
}
}
}
}
public static boolean checkPixels(Image image, int x, int y) {
int color= ((BufferedImage) image).getRGB(x, y);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = color & 0x000000ff;
if (red==255 && green==255 && blue==255 ) {
return true;
}
return false;
}
public static Image fileToImage (File file) {
Image image = null;
try {
image = (BufferedImage) ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public static boolean checkNeighbourhood(Image image, int x, int y) {
if (( (BufferedImage) image).getRGB(x-1,y)==-16777216 ||
((BufferedImage) image).getRGB(x,y-1)==-16777216 ||
((BufferedImage) image).getRGB(x+1,y)==-16777216 ||
((BufferedImage) image).getRGB(x,y+1)==-16777216) {
return true;
}
return false;
}
}