我正在编写一个用多边形近似图像的遗传算法。在经历不同的世代时,我想将进度输出到 JFrame。但是,似乎 JFrame 一直等到 GA 的 while 循环完成才能显示某些内容。我不相信这是像重绘这样的问题,因为一旦 while 循环退出,它最终会显示所有内容。即使 while 循环正在运行,我也希望 GUI 能够动态更新。
这是我的代码:
while (some conditions) {
//do some other stuff
gui.displayPolygon(best);
gui.displayFitness(fitness);
gui.setVisible(true);
}
public void displayPolygon(Polygon poly) {
BufferedImage bpoly = ImageProcessor.createImageFromPoly(poly);
ImageProcessor.displayImage(bpoly, polyPanel);
this.setVisible(true);
}
public static void displayImage(BufferedImage bimg, JPanel panel) {
panel.removeAll();
panel.setBounds(0, 0, bimg.getWidth(), bimg.getHeight());
JImagePanel innerPanel = new JImagePanel(bimg, 25, 25);
panel.add(innerPanel);
innerPanel.setLocation(25, 25);
innerPanel.setVisible(true);
panel.setVisible(true);
}