Java jPanel有时不会显示完整的BufferedImage

时间:2016-02-09 14:14:54

标签: java jpanel bufferedimage

我正在尝试使用drawImage向jPanel(processedPanel)显示我的聚簇图像。 这是我的代码

nt.setData(buff);
nt.starting();
BufferedImage clusterizedImage = nt.getClusterizedImage();
Graphics g = processedPanel.getGraphics();

System.out.println(clusterizedImage.getWidth()+"-"+clusterizedImage.getHeight());
g.drawImage(clusterizedImage, 0, 0, null);

try {
    File outputfile = new File("check.png");
    ImageIO.write(clusterizedImage, "png", outputfile);
} catch (IOException e) {
    System.out.println("failed");
}

NetworkTester类

public class NetworkTester {
    private BufferedImage testBuff;
    private Vector<InputSet> testingData;
    private double[] inputD;
    private double output;
    private BufferedImage buff;

    private NeuralNetwork nnValue;

    public void NetworkTester(){
        nnValue = new NeuralNetwork();
    }

    public void setData(BufferedImage testBuff){
        this.testBuff = testBuff;
    }

    public BufferedImage getClusterizedImage(){
        return buff;
    }

    private void extract(){
        int i,j;
        int[] inputs;

        System.out.println("Segmentation started");
        int fff = 0;

        SawitImage si = new SawitImage(200, 200);

        BufferedImage inputImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        //si.setImage(testBuff);
        for(i=0;i<200;i++){
            for(j=0;j<200;j++){
                si.setOutputPixel(i, j, testBuff.getRGB(i, j));
                inputImage.setRGB(i, j, testBuff.getRGB(i,j));
                //System.out.println(si.getOutputPixel(i, j));
            }
        }
        si.setImage(inputImage);
        //System.out.println(si.getWidth()+" - "+si.getHeight());
        Segmentation pp = new Segmentation(si);
        pp.rgbToLab();
        pp.KMeans();
        pp.index(pp.Kluster(pp.K1(pp.rgbToLab()), pp.K2(pp.rgbToLab()), pp.K3(pp.rgbToLab())));
        buff = pp.theFruits();

        if(fff == 0){
            System.out.println("Feature Extraction started");
            fff = 1;
        }

        HueFinder hf = new HueFinder();
        hf.HueValue(buff);
        HistogramHue hh = new HistogramHue();
        hh.hueHistogram(hf.getHueValues());
        inputs = hh.getH();
        //System.out.println("Length : "+inputs.length);
        //test image
        try {
            // retrieve image
            File outputfile = new File("test_data.png");
            ImageIO.write(buff, "png", outputfile);
        } catch (IOException e) {
            System.out.println("failed");
        }
        //end test image

        System.out.println("Segmentation done!");
        System.out.println("Feature Extraction done!");
        double a;
        InputSet tempInput = new InputSet("1");
        testingData = new Vector<>();
        for(i=0;i<inputs.length;i++){
            a = inputs[i];
            tempInput.addElement(a/360);
        }
        testingData.addElement(tempInput);

        System.out.println("Input vector done!");
    }

    private void testStart(){
        int i;

        BPTester bp = new BPTester();
        inputD = new double[testingData.get(0).size()];
        for(i=0;i<testingData.get(0).size();i++){
            inputD[i] = Double.parseDouble(testingData.get(0).get(i).toString());
            bp.setInput(inputD[i], i);
        }
        bp.readWeight();
        bp.operationOnHidden();
        bp.operationOnOutput();
        output = bp.getOutput();
    }

    public void starting(){
        extract();
        testStart();
    }

    public double getOutput(){
        return output;
    }
}

这是我的DaemonThread代码,我随着时间的推移替换 buff

class DaemonThread implements Runnable{
    protected volatile boolean runnable = false;

    @Override
    public void run(){
        synchronized(this){
            while(runnable){
                buff = cap.getFrame();

                Graphics g = videoPanel.getGraphics();
                if(g.drawImage(buff, 0, 0, 200, 200, 0, 0, 200, 200, null)){
                    if(runnable == false){
                        System.out.println("Stopped");
                    }
                }
            }
        }
    }
}

输出文件很好。这没什么不对的。问题是我的jPanel有时不会显示BufferedImage的所有像素(不是每次都显示)。

截图

enter image description here

enter image description here

我希望我的问题不违反规则。

0 个答案:

没有答案