public class MyDisplayPanel extends JPanel implements Runnable {
/**
*
*/
private static final long serialVersionUID = 3762463934330970468L;
// 窗体的宽与高
private static final int PLAY_DELY = 200;
public static final int WIDTH = 80;
public static final int HEIGHT = 80;
private List<Image> listImages = new ArrayList<Image>();
private Thread refreshThread;
private boolean hasPicture = false;
private int index;
private final MyCanvas canvas;
public MyDisplayPanel() {
canvas = new MyCanvas();
//
this.add(canvas);
this.setOpaque(false);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
refreshThread = new Thread(this);
refreshThread.start();
}
public boolean loadImages(String name) throws IOException {
File dir = new File(name);
if (dir.isDirectory()) {
// remove all image in the list
if (listImages.size() > 0) {
initData();
}
// load images
int num = dir.listFiles().length;
for (int i = 0; i < num; i++) {
Image image = ImageIO.read(new File(name + "/" + (i + 1)
+ ".png"));
listImages.add(image);
}
hasPicture = true;
return true;
}
return false;
}
private void initData() {
listImages.removeAll(listImages);
hasPicture = false;
index = 0;
}
private void increaseIndex() {
index++;
// restart from 0th picture
if (index == listImages.size()) {
index = 0;
}
}
@Override
public void run() {
while (true) {
if (hasPicture) {
canvas.repaint();
this.repaint();
}
try {
Thread.sleep(PLAY_DELY);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyCanvas extends Canvas {
public MyCanvas() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
@Override
public void update(Graphics g) {
if (listImages.size() > 0) {
g.drawImage(listImages.get(index), 0, 0, null);
System.out.print(index);
g.dispose();
increaseIndex();
}
}
@Override
public void paint(Graphics g) {
update(g);
}
}
}
我原来是这样写的,反而是可以运行的。后来我想把JPanel和Canvas封装在一起就成为上面的版本了,就不知道为什么不能把图片画出来了。
public class MyCanvas extends Canvas implements Runnable {
// 窗体的宽与高
private static final int PLAY_DELY = 200;
public static final int WIDTH = 80;
public static final int HEIGHT = 80;
private List<Image> listImages = new ArrayList<Image>();
private Thread refreshThread;
private boolean hasPicture = false;
private int index;
private JPanel panel;
/**
*
* @param width
* @param height
* @param flag
* @return
*/
public MyCanvas(JPanel panel) {
this.panel = panel;
// 设定初始构造时面板大小
setPreferredSize(new Dimension(WIDTH, HEIGHT));
// 初始导入一张图片
refreshThread = new Thread(this);
refreshThread.start();
}
public boolean loadImages(String name) throws IOException {
File dir = new File(name);
if (dir.isDirectory()) {
// remove all image in the list
if (listImages.size() > 0) {
initData();
}
// load images
int num = dir.listFiles().length;
for (int i = 0; i < num; i++) {
Image image = ImageIO.read(new File(name + "/" + (i + 1)
+ ".png"));
listImages.add(image);
}
hasPicture = true;
return true;
}
return false;
}
private void initData() {
listImages.removeAll(listImages);
hasPicture = false;
index = 0;
}
@Override
public void update(Graphics g) {
if (listImages.size() > 0) {
g.drawImage(listImages.get(index), 0, 0, null);
g.dispose();
increaseIndex();
}
}
private void increaseIndex() {
index++;
// restart from 0th picture
if (index == listImages.size()) {
index = 0;
}
}
@Override
public void paint(Graphics g) {
update(g);
}
@Override
public void run() {
while (true) {
if (hasPicture) {
this.repaint();
panel.repaint();
}
try {
Thread.sleep(PLAY_DELY);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}