我正在开发一个Java程序,它接收大量文件(最多3000个),并且相关的数组为1/0。目前我有一个数组的可视化,其中有一个网格,其中每个框填充为黑色为1或白色为0.当绘制它运行良好但需要大约一分钟才能完全加载(并且可能在此期间锁定计算机。 )有没有办法可以:1,在完成之前不显示窗口
I am working on a Java program that takes in a large amount of files (3000 max) with an associated array of 1/0's. Currently I have a visualization of the array where there is a grid where each box is filled black for 1 or white for 0. When drawn it runs well but takes around a minute to fully load (and potentially locks the computer up in the meantime.) Is there a way I can: 1, not display the window till it is done
(即JFrame创建,
(i.e JFrame create,
//绘制窗口
frame.setVisible(true))
frame.setVisible(true))
和2,跟踪进度这个过程让我可以使用进度条吗?
and 2, track the progress of the process so that I can use a progress bar with it?
编辑:我可以运行一个线程来绘制它,然后简单地做一个while循环来只显示它线程完成后?
edit: Can I run a thread to draw it and then simply make a while loop to only display it once the thread is completed?
推荐答案在下面的例子中,一个 SwingWorker 设置 BufferedImage中的像素基于从随机文件中读取的数据。请注意, Thread.sleep()用于模拟延迟;否则不需要。您可以添加 JProgressBar ,如此处所示。
In the example below, a SwingWorker sets pixels in a BufferedImage based on the data read from a random file. Note that Thread.sleep() is used to simulate latency; it is otherwise not required. You can add a JProgressBar as shown here.
有没有更好的方法来获得简单的彩色框?
是的。在下面的示例中,每个像素代表一个单元格。对于较大的框,返回图像大小的倍数,例如
Yes. In the example below, each pixel represents one cell. For larger boxes, return a multiple of the image size, e.g.
@Override public Dimension getPreferredSize() { return new Dimension(2 * N, 2 * N); }import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.FileInputStream; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingWorker; /** * @see stackoverflow/a/25043676/230513 */ public class WorkerTest { private static final int N = 256; private final BooleanPanel panel = new BooleanPanel(); private class BooleanPanel extends JPanel { private BufferedImage image; public void setImage(BufferedImage bi) { this.image = bi; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(image, 0, 0, getWidth(), getHeight(), null); } @Override public Dimension getPreferredSize() { return new Dimension(N, N); } } private class BufferedImageWorker extends SwingWorker<BufferedImage, BufferedImage> { @Override protected BufferedImage doInBackground() throws Exception { BufferedImage image = new BufferedImage(N, N, BufferedImage.TYPE_INT_ARGB); try (DataInputStream dis = new DataInputStream( new BufferedInputStream(new FileInputStream("/dev/random")))) { for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { image.setRGB(col, row, dis.readByte() < 0 ? 0xffffffff : 0xff000000); } Thread.sleep(40); // ~25 Hz publish(image); } return image; } } @Override protected void process(List<BufferedImage> list) { for (BufferedImage bi : list) { panel.setImage(bi); panel.repaint(); } } } private void display() { JFrame f = new JFrame("WorkerTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(panel); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); new BufferedImageWorker().execute(); } public static void main(String[] args) { EventQueue.invokeLater(() -> { new WorkerTest().display(); }); } }
更多推荐
阻止窗口显示直到完全绘制?
发布评论