无论基于gui的分拣机

时间:2018-02-14 20:26:51

标签: java arrays sorting user-interface arraylist

我试图编写一个程序,允许我比较图像对,以便最终对它们进行排序。为了做到这一点,我实现了一个基于gui的分类器,问题是无论我的可比函数返回什么,最终的排序都是完全一样的。

这是我的可比较功能

public class img implements Comparable<img>{

    public int compareTo(img other) {


        CompareGUI gui = new CompareGUI(this,(img)other);
        while(gui.res==0)
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println(gui.res);
        //return -1 //return 1
        return gui.res;
    }

如果我插入返回-1或返回1,结果不会改变,此函数打印输出1或-1,但最终结果始终相同。

以下是我如何使用它:

public class Sorter {

    static String loc="~/Desktop";
    public static void main (String[] args) throws IOException {

        ArrayList<img> images = loadImages();
        Arrays.sort(images.toArray());      
        for(int i=0;i<images.size();i++){
            images.get(i).rank=i;
        }
        tocsv(images);


    }

    //output function unlikely to be the issue
    public static void tocsv(List<img> images) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(loc+"evalres.txt"));
        for(img im : images) {
            String str= im.rank+",\t";
            for(Integer t:im.values) {
                str=str+t+",\t";
            }
            str.substring(0, str.length()-2);
            str=str+'\n';
            writer.write(str);
        }
        writer.flush();
        writer.close();

    }
    //loading, also not the issue
    public static ArrayList<img> loadImages() {
        File dir = new File (loc);
        ArrayList<img> ret= new ArrayList<img>();
        for (File i  :dir.listFiles()) {
            if (i.getName().endsWith("png")){
                    ret.add(new img(i.getAbsolutePath()));
            }
        }
        return ret;

    }
}

其他两个类看起来如下(不太可能包含问题,因为在比较代码中只返回-1或1绕过此代码并且行为保持不变):

public class img implements Comparable<img>{


    int rank;
    List<Integer> values= new ArrayList<Integer>();




    String path;
    public img(String path) {
        this.path=path;
        readName(new File(path).getName());

    }
    private void readName(String str) {
        System.out.println();
        System.out.println(str);

        String[] words = str.split("[^0-9']+");
        for (String word : words) {
            if(!word.isEmpty())
            values.add(Integer.parseInt(word));
        }


    }


    @Override
    public int compareTo(img other) {


        CompareGUI gui = new CompareGUI(this,(img)other);
        while(gui.res==0)
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println(gui.res);
        return gui.res;
    }
}


public class CompareGUI extends JFrame{

    int res=0;
    JFrame frame;

    public CompareGUI(img ima, img imb) {
        frame=this;
        this.setLayout(new GridLayout(0,2));
        Container cp = getContentPane();
        ImageIcon icoa=new ImageIcon(ima.path);
        ImageIcon icob=new ImageIcon(imb.path);

        JButton labela = new JButton(icoa);
        JButton labelb = new JButton(icob);
        labela.setSize(icoa.getIconWidth(), icoa.getIconHeight());
        labelb.setSize(icoa.getIconWidth(), icoa.getIconHeight());
        labela.setLocation(0, 0);
        labelb.setLocation(icoa.getIconWidth(), 0);

        cp.add(labela);
        cp.add(labelb);


        labela.setVisible(true);
        labelb.setVisible(true);
        this.setSize(icoa.getIconWidth()*2+50,icoa.getIconHeight()+50);
        this.setVisible(true);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        labela.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                res=-1;
                frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
            }
        });

        labelb.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                res=1;
                frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
            }
        });


    }


}

现在无论我点击什么图像或者如何修改最终结果,相同的图像总是以完全相同的方式排序,这表明行Arrays.sort(images.toArray());忽略了compareTo函数的结果。谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

我看到你正在对数组进行排序(从原始集合构造);但期望对原始集合进行排序。

所以,不是Arrays.sort(images.toArray());使用Collection.sort(images),而是images已排序