当前位置: 代码迷 >> Java相关 >> [求助]如何将刚画到面板上的线条以稍慢的速度重放如何绘画?
  详细解决方案

[求助]如何将刚画到面板上的线条以稍慢的速度重放如何绘画?

热度:93   发布时间:2007-03-07 11:52:50.0
确实清空了,我分着又试了下,但在清空后的下面加慢动作重绘的代码就不清楚了...很邪门
-----------------------------------------------------------------

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(Pan pan: lstPoint)
{
g2.setPaint(pan.getColor());
g2.draw(pan.getLine());
try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}//睡0.1秒画一次
System.out.println("paint内部的代码!");
}
}

红色的都去了,清空没问题,加上后就清空不了了.....而且不停的打印System.out.println("paint内部的代码!");虽然在运行,但看不出重绘的慢动作效果..
----------------解决方案--------------------------------------------------------

最好是清空以前JPanle上的内容,然后慢动作显示绘画过程...


----------------解决方案--------------------------------------------------------

不应该是你这样写的

因为在paintComponent方法返回之前,是不会画出来的

所以,你得起一个线程去控制画多少,在paintComponent里面应该尽量快,不应该占用太多的时间,否则你的程序动起来就不自然了


----------------解决方案--------------------------------------------------------
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

new Thread
(
new Runnable()
{
public void run()
{
for(Pan pan: lstPoint)
{
g2.setPaint(pan.getColor());
g2.draw(pan.getLine());
try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}//睡0.1秒画一次
}
}
}
).start();
}
这样吗?也不行..........画不出来
----------------解决方案--------------------------------------------------------
你怎么会到paint里面去起线程呢?

线程在外面起,在线程里面调用repaint,
----------------解决方案--------------------------------------------------------
在外面起个线程,怎么得到graphics对象呢?
----------------解决方案--------------------------------------------------------

你要得到对象干嘛
你只管在线程里面调用repaint,然后系统就会调用paintComponent,这样你就可以控制paintComponnet的调用速度了


----------------解决方案--------------------------------------------------------
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(55,55,100,100);
for(Pan pan: lstPoint)
{
g2.setPaint(pan.getColor());
g2.draw(pan.getLine());
try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}//睡0.1秒画一次
System.out.println("thread------paint内部的代码!");
}

}

越弄越乱了,蓝色的和红色的部分有区别吗?
如果注释掉红色的,剩下的代码正常运行,清屏且画出一个方块
如果注释掉蓝色的,红色的代码会总打出 System.out.println("thread------paint内部的代码!"); 但画不出东西

----------------解决方案--------------------------------------------------------

我不是已经说了吗?
你不要在paintComponent里面耗太多的时间,你应该在外面起一个线程去控制它画的速度,paintComponent只管画,而不管速度,


----------------解决方案--------------------------------------------------------

外面线程怎么控制里面的速度?
你能帮我写一下代码吗?


----------------解决方案--------------------------------------------------------
  相关解决方案