由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 一个基本问题。
相关主题
Re: print problem, GUI guru please come in输入中文导致的死 机
几个问题Java练习题 10
农民运动讲习所1. Thread and paint()java ,wait ,notify notifyall
can applet implements runnable?Re: How to showStatus in Applet?
Swing helpRe: connection pool
Re: Can create a Interface object, why?ZT: 关于性爱的多线程问题研究(一)
可以帮我看看这HelloWordApplet哪错了吗?问个Thread 的问题,java certificate里的
jvm是怎么implement monitor的?●●●●紧急求助JAVA初级问题,今天project due●●●●
相关话题的讨论汇总
话题: animation话题: button话题: import话题: repaint话题: thread
进入Java版参与讨论
1 (共1页)
g*****g
发帖数: 34805
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 {
a**e
发帖数: 5794
2
sleep(1000) 呢?

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

m******t
发帖数: 2416
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();

g*****g
发帖数: 34805
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.

t****5
发帖数: 20
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();

t****5
发帖数: 20
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.

m******t
发帖数: 2416
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.

g*****g
发帖数: 34805
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?

g*****g
发帖数: 34805
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

m******t
发帖数: 2416
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.
:
: .

h******a
发帖数: 46
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.
:
: .

g*****g
发帖数: 34805
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.
:
: .

1 (共1页)
进入Java版参与讨论
相关主题
●●●●紧急求助JAVA初级问题,今天project due●●●●Swing help
能这么 create thread 吗?Re: Can create a Interface object, why?
implements runable 和 extends thread可以帮我看看这HelloWordApplet哪错了吗?
关于inner class的继承jvm是怎么implement monitor的?
Re: print problem, GUI guru please come in输入中文导致的死 机
几个问题Java练习题 10
农民运动讲习所1. Thread and paint()java ,wait ,notify notifyall
can applet implements runnable?Re: How to showStatus in Applet?
相关话题的讨论汇总
话题: animation话题: button话题: import话题: repaint话题: thread