avatar
一个基本问题。# Java - 爪哇娇娃
g*g
1
一个演示我的疑问的简单程序,点击button改变填充Rectangle大小。
假如我想做一个animation让矩形慢慢变大。期间我并不想让其他
控件响应。为什么下面的code不行呢?我知道implements Runnable
就可以,但我想知道为什么一个线程不行?
size++;
repaint();
try {
Thread.sleep(10);
} catch (Exception ex) {
ex.printStackTrace();
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class AnimationApplet extends JApplet implements ActionListener {
avatar
a*e
2
sleep(1000) 呢?

【在 g*****g 的大作中提到】
: 一个演示我的疑问的简单程序,点击button改变填充Rectangle大小。
: 假如我想做一个animation让矩形慢慢变大。期间我并不想让其他
: 控件响应。为什么下面的code不行呢?我知道implements Runnable
: 就可以,但我想知道为什么一个线程不行?
: size++;
: repaint();
: try {
: Thread.sleep(10);
: } catch (Exception ex) {
: ex.printStackTrace();

avatar
m*t
3

Because when you call repaint(), it doesn't really repaint
right away. Instead it waits until the end of the event loop
and do all the screen updates in one shot.

【在 g*****g 的大作中提到】
: 一个演示我的疑问的简单程序,点击button改变填充Rectangle大小。
: 假如我想做一个animation让矩形慢慢变大。期间我并不想让其他
: 控件响应。为什么下面的code不行呢?我知道implements Runnable
: 就可以,但我想知道为什么一个线程不行?
: size++;
: repaint();
: try {
: Thread.sleep(10);
: } catch (Exception ex) {
: ex.printStackTrace();

avatar
g*g
4
So what's your solution to do a simple job like this:
1.Click a button, which consequently
disables itself(and other buttons/block inputs) and trigger an animation
2.Enable the button so you can do it again.
Use a thread or a Swing timer I can do the animation without problem.
But then I have to synchronize the two threads.
Since only one is working at a time, it does look unneccesarily
complicate.

【在 m******t 的大作中提到】
:
: Because when you call repaint(), it doesn't really repaint
: right away. Instead it waits until the end of the event loop
: and do all the screen updates in one shot.

avatar
t*5
5
you can only manipulate swing component in the event thread.
any code in event thread has to be fast, for a responsive UI.
your slow long-run operation has to be in another thread...

【在 g*****g 的大作中提到】
: 一个演示我的疑问的简单程序,点击button改变填充Rectangle大小。
: 假如我想做一个animation让矩形慢慢变大。期间我并不想让其他
: 控件响应。为什么下面的code不行呢?我知道implements Runnable
: 就可以,但我想知道为什么一个线程不行?
: size++;
: repaint();
: try {
: Thread.sleep(10);
: } catch (Exception ex) {
: ex.printStackTrace();

avatar
t*5
6
someone tried to simplify the exact same problem.
to make the code more intuitive. things like spin.sf.net
I remember I've seen more alternatives, but couldn't find out.

【在 g*****g 的大作中提到】
: So what's your solution to do a simple job like this:
: 1.Click a button, which consequently
: disables itself(and other buttons/block inputs) and trigger an animation
: 2.Enable the button so you can do it again.
: Use a thread or a Swing timer I can do the animation without problem.
: But then I have to synchronize the two threads.
: Since only one is working at a time, it does look unneccesarily
: complicate.

avatar
m*t
7

I can't think of any way simpler than using a timer task and SwingUtils.
As T91105 already pointed out, I'm sure there are packages that wrap up
this in a generic way, but in the end you can't get around the limitation
imposed by the event loop itself.
I'm not following you on this - why do you need to synchronize them?

【在 g*****g 的大作中提到】
: So what's your solution to do a simple job like this:
: 1.Click a button, which consequently
: disables itself(and other buttons/block inputs) and trigger an animation
: 2.Enable the button so you can do it again.
: Use a thread or a Swing timer I can do the animation without problem.
: But then I have to synchronize the two threads.
: Since only one is working at a time, it does look unneccesarily
: complicate.

avatar
g*g
8
OK, assume you disable the button, and start the animation until it finishes.
Now you want to enable the button. How are you going to do that?
One option is to pass the button to animation, and enable it at the end of
animation. But it's against design pattern. Why should an animation knows
who triggers it?
If everything can be done in the actionPerform method in sequence, I can
disable the button, trigger the animation until it finishes, then enable the
button.
It's natural for a caller to cont

【在 m******t 的大作中提到】
:
: I can't think of any way simpler than using a timer task and SwingUtils.
: As T91105 already pointed out, I'm sure there are packages that wrap up
: this in a generic way, but in the end you can't get around the limitation
: imposed by the event loop itself.
: I'm not following you on this - why do you need to synchronize them?

avatar
g*g
9
I made a listener and callback to get around, not an elegant solution though.

.

【在 g*****g 的大作中提到】
: OK, assume you disable the button, and start the animation until it finishes.
: Now you want to enable the button. How are you going to do that?
: One option is to pass the button to animation, and enable it at the end of
: animation. But it's against design pattern. Why should an animation knows
: who triggers it?
: If everything can be done in the actionPerform method in sequence, I can
: disable the button, trigger the animation until it finishes, then enable the
: button.
: It's natural for a caller to cont

avatar
m*t
10

.
Actually I think it _is_ an elegant solution - at least that's how I would
have done it, too. 8-) As you said earlier, passing the button directly to
the animation thread is bad (not only anti-pattern, but also actual Swing-
related
issues due to the same reason that you can't directly update any screen
element
from a background thread).

【在 g*****g 的大作中提到】
: I made a listener and callback to get around, not an elegant solution though.
:
: .

avatar
h*a
11
replace repaint() with paint(getGraphics()); although not suggested, but will
do in your case. repaint() just signal system should paint(), but the
underline optimizer decide when and where to paint.

.

【在 g*****g 的大作中提到】
: I made a listener and callback to get around, not an elegant solution though.
:
: .

avatar
g*g
12
I believe that will work but lead to unpleasant flashes.
I won't mess up awt with swing.

will

【在 h******a 的大作中提到】
: replace repaint() with paint(getGraphics()); although not suggested, but will
: do in your case. repaint() just signal system should paint(), but the
: underline optimizer decide when and where to paint.
:
: .

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