final int X = 2*w.getX();
final int Y = 2*w.getY();
final int width = 2*w.getWidth();
final int height = 2*w.getHeight();
// File f = new File("F:\\photos\\0d338744ebf81a4c6b38cb8bd72a6059252da66d.jpg");
// Image currentImage = null;
// try {
// currentImage = ImageIO.read(f);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// g2d.drawImage(currentImage, X, Y, width, height,null);
final ArrayList<LocalFile> listLocalFile = AndroidEditor.instance().getPlayList().listLocalFile;
new Thread(new Runnable() {
@Override
public void run() {
File f = null;
Image currentImage = null;
while(true){
if(listLocalFile.isEmpty()){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
continue;
}
for(LocalFile lf:listLocalFile){
if("Image_File".equals(lf.fileType)){
f = new File(lf.filePath);
try {
currentImage = ImageIO.read(f);
System.out.println(X+":"+Y+":"+width+":"+height);
g2d.drawImage(currentImage, X, Y, width, height,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
为什么多线程中的 g2d.drawImage(currentImage, X, Y, width, height,null);方法画不出来东西?而换成注释的代码
// File f = new File("F:\\photos\\0d338744ebf81a4c6b38cb8bd72a6059252da66d.jpg");
// Image currentImage = null;
// try {
// currentImage = ImageIO.read(f);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// g2d.drawImage(currentImage, X, Y, width, height,null);
就能画出来,大侠们有没有知道的?
3 个解决方案
#1
画完是不是要通知repaint
#2
是这样的,Graphics2D的draw方法就是启动线程,并且不是线程安全的
#3
你可以使用Thread.sleep()来保证多线程的稳定运行
#1
画完是不是要通知repaint
#2
是这样的,Graphics2D的draw方法就是启动线程,并且不是线程安全的
#3
你可以使用Thread.sleep()来保证多线程的稳定运行