dimanche 4 décembre 2016

Comparing two pixels ( Color of them )

Im making project for my uni, and I have a problem with one thing - everything works well, but there is bug with comparing colors of two pixels. I have to count area of some figure, and I have to use MonteCarlo method. ( generate random points, count points in figure and out, calculete figure area ) And some points are counted well, some dont, I have no idea whats wrong, Im trying to solve that few hours...

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Runner extends JApplet{

private BufferedImage img;
public ArrayList<Point> w;
public ArrayList<Point> poza;
public BufferedImage output;

public void init(){

    try{
        img = ImageIO.read(new File("figura.gif"));
    } catch (IOException e){
        e.getStackTrace();
        System.err.println("Nie ma obrazkaXD");
    }

}

public void paint(Graphics g){
    w = new ArrayList<Point>();
    poza = new ArrayList<>();
    super.paint(g);
    Random random = new Random();
    int wys = img.getHeight();
    int szer = img.getWidth();
    g.drawImage(img, 0, 0, wys, szer, null);
    for (int i = 0; i < 1000; i++) {
        int x = random.nextInt(wys);
        int y = random.nextInt(szer);
        Point p = new Point(x,y);
        g.setColor(Color.GREEN);
        g.drawOval(y, x, 1, 1);
        Color c = new Color(img.getRGB(y, x));;
            if(c.equals(Color.BLACK)){
                w.add(p);
                g.setColor(Color.RED);
                g.drawOval(y, x, 1, 1);
            }else{
                poza.add(p);
            }
    }

float a = w.size();
float b = poza.size()+w.size();
float poleProstokata = wys*szer;
float pole = a/b*poleProstokata;
}




Aucun commentaire:

Enregistrer un commentaire