Redian新闻
>
Java Paint()可以被中断吗?
avatar
Java Paint()可以被中断吗?# Java - 爪哇娇娃
b*i
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同时被调
avatar
c*t
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,...);
: ...

avatar
b*i
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

avatar
c*t
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呢?

avatar
s*e
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
avatar
b*i
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

avatar
s*e
7
why not just try it. As long as you can avoid race condition, a solution
whould work for you.
avatar
b*i
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。