我的窗口需要显示14个页面的缩略图,但是生成一个缩略图大于需要2s,我不希望用户一直等待14*2=28s之后才开始给用户看这些缩略图,我打算这样:
1,生成5个缩略图的时候窗口显示出来,此时用户可以看到5个缩略图
2,等用户在看前5个缩略图时,后面接着生成其他的缩略图,且推送新生成的缩略图给窗口。我现在的做法是:
int index = 1;
while (!donePrinting(index)) {
image = getPage(index);
if (image != null) {
saveImage(image, index);
image.flush();
image = null;
}
if (allImages.size() >= 5) { try {
dialog = new BookPreviewUsingPics(allImages, getNumOfPages());
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} final int realIndex = index + 1;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getLeftImages(realIndex);
}
}); return;
}
index++;
}
private void getLeftImages(int index) {
System.out.println("Start to getLastImages from index :" + index);
BufferedImage image = null;
while (!donePrinting(index)) {
image = getPage(index);
if (image != null) {
saveImageAndSend(image, index);
image.flush();
image = null;
}
index++;
} image = getBackCoverPage();
if (image != null)
saveImageAndSend(image, getNumOfPages() + 1);
}
private void saveImageAndSend(BufferedImage image, int i) {
System.out.println("Start to saveImageAndSend for index :" + i + " image :" + image);
HashMap<Integer, BufferedImage> images = new HashMap<Integer, BufferedImage>();
images.put(i, image);
if (dialog != null) {
System.out.println("--> Start to sendBufferedImage for index :" + i);
dialog.sendBufferedImage(images);
}
}但是这样的话,窗口刚起来时,点击窗口没有反应,只有等14个页面全部生成了,窗口才响应了用户的点击事件,请问各位,你们知道是什么原因吗?该如何修改啊!异步j2se
1,生成5个缩略图的时候窗口显示出来,此时用户可以看到5个缩略图
2,等用户在看前5个缩略图时,后面接着生成其他的缩略图,且推送新生成的缩略图给窗口。我现在的做法是:
int index = 1;
while (!donePrinting(index)) {
image = getPage(index);
if (image != null) {
saveImage(image, index);
image.flush();
image = null;
}
if (allImages.size() >= 5) { try {
dialog = new BookPreviewUsingPics(allImages, getNumOfPages());
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} final int realIndex = index + 1;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getLeftImages(realIndex);
}
}); return;
}
index++;
}
private void getLeftImages(int index) {
System.out.println("Start to getLastImages from index :" + index);
BufferedImage image = null;
while (!donePrinting(index)) {
image = getPage(index);
if (image != null) {
saveImageAndSend(image, index);
image.flush();
image = null;
}
index++;
} image = getBackCoverPage();
if (image != null)
saveImageAndSend(image, getNumOfPages() + 1);
}
private void saveImageAndSend(BufferedImage image, int i) {
System.out.println("Start to saveImageAndSend for index :" + i + " image :" + image);
HashMap<Integer, BufferedImage> images = new HashMap<Integer, BufferedImage>();
images.put(i, image);
if (dialog != null) {
System.out.println("--> Start to sendBufferedImage for index :" + i);
dialog.sendBufferedImage(images);
}
}但是这样的话,窗口刚起来时,点击窗口没有反应,只有等14个页面全部生成了,窗口才响应了用户的点击事件,请问各位,你们知道是什么原因吗?该如何修改啊!异步j2se
public void sendBufferedImage(final HashMap<Integer, BufferedImage> allImages) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
allBufferedImages.putAll(allImages);
System.out.println("Total num of allBufferedImages in JBookPanel is " + allBufferedImages.size());
}
});
}
图片显示时,直接从allBufferedImages里面获得BufferedImage,然后显示。多线程显示图片? panel能支持吗?
图片显示时,直接从allBufferedImages里面获得BufferedImage,然后显示。多线程显示图片? panel能支持吗?布局定义好,当然支持了。
图片显示时,直接从allBufferedImages里面获得BufferedImage,然后显示。多线程显示图片? panel能支持吗?布局定义好,当然支持了。
好的,我调查下。
图片显示时,直接从allBufferedImages里面获得BufferedImage,然后显示。多线程显示图片? panel能支持吗?布局定义好,当然支持了。你那里有实例吗?我参考下
你的意思是 用他来生成缩略图?
是。
doInBackground 方法里生成缩略图,done方法里设置显示图片
你的意思是 用他来生成缩略图?
是。
doInBackground 方法里生成缩略图,done方法里设置显示图片我记得done是在doInBackground 全部执行完了之后在执行,我现在在doInBackground 里面pulish,在process里面是设置显示的图片。这个不会有卡吨的现象。
你的意思是 用他来生成缩略图?
是。
doInBackground 方法里生成缩略图,done方法里设置显示图片我记得done是在doInBackground 全部执行完了之后在执行,我现在在doInBackground 里面pulish,在process里面是设置显示的图片。这个不会有卡吨的现象。
我意思是一个缩略图使用一个SwingWorker,你是一个SwingWorker生成所有缩略图?
你的意思是 用他来生成缩略图?
是。
doInBackground 方法里生成缩略图,done方法里设置显示图片我记得done是在doInBackground 全部执行完了之后在执行,我现在在doInBackground 里面pulish,在process里面是设置显示的图片。这个不会有卡吨的现象。
我意思是一个缩略图使用一个SwingWorker,你是一个SwingWorker生成所有缩略图?那理解错你的意思了。
我是前5个是还是我之前的办法,后面的全部放到一个Swingworker里面来生成。一个缩略图一个SwingWorker也是个好办法