b***i 发帖数: 3043 | 1 我有一个变量needsUpdated,在paint()中被查询,如果是true,就更新旋转后的icon
。如下
public void paint(Graphics g){
...
if (theIcon.needsUpdated){
theIcon.rotatedIcon = createRotatedImage(this,...);
theIcon.needsUpdated = false;
}
theIcon.rotatedIcon.paintIcon(this, g,...);
...
}
以上是查询,下面是主动的旋转函数,为了加快速度,我不是每次都更新icon,只作了
标志,每次旋转后第一次画的时候才更新。
public void turn(double angle){
theIcon.rotation +=angle;
theIcon.needsUpdated=true;
}
但是,有的时候可以看到,动画停止的时候,屏幕上的icon的方向是不对的。有没有可
能paint()正进行的时候,turn同时被调 |
c*****t 发帖数: 1879 | 2 You should NOT do calculation routines in paint (). That would slow
your program down.
Instead, you call repaint () (various flavors) after you did an update.
Just be careful of concurrent access. Use lockless mechanisms. Reading
and writing a pointer / 32-bit integer is usually atomic.
icon
【在 b***i 的大作中提到】 : 我有一个变量needsUpdated,在paint()中被查询,如果是true,就更新旋转后的icon : 。如下 : public void paint(Graphics g){ : ... : if (theIcon.needsUpdated){ : theIcon.rotatedIcon = createRotatedImage(this,...); : theIcon.needsUpdated = false; : } : theIcon.rotatedIcon.paintIcon(this, g,...); : ...
|
b***i 发帖数: 3043 | 3 多谢,那我就在每次调用repaint之前,加入了createRotatedImage,这样,即使paint
里面发现turn了,但没有旋转后的图标,也只是很少的情况了。
不过,还是不清楚,是不是paint发生的时候,turn也可以发生?系统什么时候自动
paint呢?
【在 c*****t 的大作中提到】 : You should NOT do calculation routines in paint (). That would slow : your program down. : Instead, you call repaint () (various flavors) after you did an update. : Just be careful of concurrent access. Use lockless mechanisms. Reading : and writing a pointer / 32-bit integer is usually atomic. : : icon
|
c*****t 发帖数: 1879 | 4 一种办法
void paint ()
{
Image img = instanceImageIcon; // instanceImageIcon is volatile instance
// variable.
// pointer read access is atomic
... // draw image using "img" variable.
if (img != instanceImageIcon)
repaint ();
}
repaint 一般是在 screen refresh (比如 move, resize, restore, uncovered
等)的时候调用。你也可以用个 timer thread 调用。
paint
【在 b***i 的大作中提到】 : 多谢,那我就在每次调用repaint之前,加入了createRotatedImage,这样,即使paint : 里面发现turn了,但没有旋转后的图标,也只是很少的情况了。 : 不过,还是不清楚,是不是paint发生的时候,turn也可以发生?系统什么时候自动 : paint呢?
|
s******e 发帖数: 493 | 5 assuming you are talking about swing.
A swing application will have at least two threads, main thread and event
dispatch thread. All the event handling and painting job will be handled by
event dispatch thread by wrapping the code into one of two flavors of
invokelater. The job will be queued in event dispatch thread.
but at the same time the main thread is free to do anything else.
I believe that calling the built-in repaint will casue the code to be
executed in the event dispatch thread. any l |
b***i 发帖数: 3043 | 6 原来如此。
其实我的原始方案很简单,不是每次转向都更新图标。而是每次只是做个标记,到
paint()里面再更新,就出现了这个问题:有的时候paint的时候,turn正在设新的角度
,刚标记完,paint又把标记去掉了。就出现了图标方向不对的情况了。
我准备把方案改成:turn的时候,不做标记了,改为设立updto变量来记住下一个角度
,而updfrom为上一次生成图标的角度。那么,paint里面如果发现updto/=updfrom则按
照updto来生成图标,并修改updfrom。这样,updto只在turn的时候改变,turnfrom只
在生成图标的时候改变。这样该可以了吧。
当然,最好是paint的时候不要生成图标,而是生成一个线程来做这个事情,这个线程
生成图标后再repaint()就可以了。行不行?
by
name
【在 s******e 的大作中提到】 : assuming you are talking about swing. : A swing application will have at least two threads, main thread and event : dispatch thread. All the event handling and painting job will be handled by : event dispatch thread by wrapping the code into one of two flavors of : invokelater. The job will be queued in event dispatch thread. : but at the same time the main thread is free to do anything else. : I believe that calling the built-in repaint will casue the code to be : executed in the event dispatch thread. any l
|
s******e 发帖数: 493 | 7 why not just try it. As long as you can avoid race condition, a solution
whould work for you. |
b***i 发帖数: 3043 | 8 做好了,把原来一个变量needsUpdate换成两个变量,original, updated, 这样一个函
数里面改变updated,而paint()里面比较两个变量,如果不同,就调用
createRotatedImage(){产生新的旋转位图,并更新original;}。这样,可以保证不会
漏掉一个旋转的信息。
倒是想做一个线程,不停更新所有的旋转的位图,然后repaint,1秒钟10次,不过目前
更新挺快,系统挺流畅,就再说吧。
【在 s******e 的大作中提到】 : why not just try it. As long as you can avoid race condition, a solution : whould work for you.
|