java jpanel实现动画

地瓜地瓜我是土豆876 发布于 2017/01/19 12:41
阅读 516
收藏 0
大家好,请看问题,我的代码大致是这样的,可为什么不能实现动画效果,我有20张图片,我发现无论如何它只是显示第20张图片,可测试发现它的的确确一直在循环0到20 contentPane = new MJPanel(this); new MThread(contentPane).start(); class MJPanel extends JPanel{ Students students; public MJPanel(Students students) { // TODO 自动生成的构造函数存根 this.students=students; } public void paintComponent(Graphics g){ super.paintComponent(g); for(int x=0;x<20;x++) g.drawImage(students.backbound[x], 0, 0, 1000, 600, this); } } class MThread extends Thread{ MJPanel contentPane; MThread(MJPanel contentPane){ this.contentPane=contentPane; } public void run(){ while(true){ try { sleep(100); contentPane.repaint(); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }
加载中
0
黑牙菜菜
黑牙菜菜

public void run(){

while(true){

try {

sleep(100);

SwingUtilities.invokeLater(new runable(){contentPane.repaint();});

} catch (InterruptedException e){}

}

0
地瓜地瓜我是土豆876
地瓜地瓜我是土豆876

引用来自“黑牙菜菜”的评论

public void run(){

while(true){

try {

sleep(100);

SwingUtilities.invokeLater(new runable(){contentPane.repaint();});

} catch (InterruptedException e){}

}

还是没有动
0
黑牙菜菜
黑牙菜菜
class MJPanel extends JPanel {
	Students students;
	private int index = -1;

	public MJPanel(Students students) { // TODO 自动生成的构造函数存根
		this.students = students;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		g.drawImage(students.backbound[index], 0, 0, 1000, 600, this);

	}
}

class MThread extends Thread {
	MJPanel contentPane

	MThread(MJPanel contentPane) {
		this.contentPane = contentPane;
	}

	public void run() {
		final Runnable refresh = new Runnable() {
			public void run() {
				contentPane.repaint();

			}
		};
		while (true) {
			try {
				for (int i = 0; i < 20; i++) {
					contentPane.setIndex(i);
					SwingUtilities.invokeLater(refresh);

					sleep(100);
				}

			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
}



0
地瓜地瓜我是土豆876
地瓜地瓜我是土豆876

引用来自“黑牙菜菜”的评论

class MJPanel extends JPanel {
	Students students;
	private int index = -1;

	public MJPanel(Students students) { // TODO 自动生成的构造函数存根
		this.students = students;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		g.drawImage(students.backbound[index], 0, 0, 1000, 600, this);

	}
}

class MThread extends Thread {
	MJPanel contentPane

	MThread(MJPanel contentPane) {
		this.contentPane = contentPane;
	}

	public void run() {
		final Runnable refresh = new Runnable() {
			public void run() {
				contentPane.repaint();

			}
		};
		while (true) {
			try {
				for (int i = 0; i < 20; i++) {
					contentPane.setIndex(i);
					SwingUtilities.invokeLater(refresh);

					sleep(100);
				}

			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
}



很遗憾,还是没用,我要疯了,如果不画图片,只画圆,正方形什么的就可以动,偏偏图片就是不动,一直显示只最后一张
0
地瓜地瓜我是土豆876
地瓜地瓜我是土豆876

引用来自“黑牙菜菜”的评论

public void run(){

while(true){

try {

sleep(100);

SwingUtilities.invokeLater(new runable(){contentPane.repaint();});

} catch (InterruptedException e){}

}

麻烦了,昨晚在网上找到原因了,完美解决
k
kisn888
然后呢?
返回顶部
顶部