Hello,
I'm trying to create a file transfer window that shows a file moving
from a client computer to a server.
I have an applet that pops up a JFrame from a user action. The JFrame
is using a BorderLayout and has a JLabel at BorderLayout.NORTH, a
JLabel with an image at .WEST, another JLabel at BorderLayout.East,
and a JPanel at BorderLayout.CENTER. (A JPanel in between two images
of computers).
********************************************
What I want to do is create an animation on the JPanel that uses an
image of a file, which shows the file moving from one side of the
panel to the other and repeats itself. I was wondering how to go about
doing this.
I would like to create a new class that extends javax.swing.JPanel
that does this.
*********************************************
Thanks in advance.
Knute Johnson - 29 May 2004 00:14 GMT
> Hello,
> I'm trying to create a file transfer window that shows a file moving
[quoted text clipped - 15 lines]
>
> Thanks in advance.
There are probably lots of ways to do what you want but here is what I
would do if somebody asked me to program the middle JPanel.
1) Override paintComponent() in the JPanel to draw a series of images
of your moving file.
2) Implement Runnable and in the run() create a loop with a delay that
increments a state variable that the paintComponent() can use to redraw
the moving file image. After each delay, call repaint() to draw the new
position of the file image.
3) When you start your download, create a new Thread with the Runnable
from the middle JPanel and start it. When you are done downloading the
file, stop the thread. The easiest way to do that is to trap the
InterruptedException from the Thread.sleep() delay and exit the run().
Pseudo code:
class FilePanel extends JPanel implements Runnable {
static final int NUMBER_OF_STATES = 8;
private volatile int state;
public void run() {
while (true) {
++state;
state %= NUMBER_OF_STATES;
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
break;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
switch (state) {
case 1: // paint the file on the left
break;
case 2: // paint the file 1/4 of the way
break;
...
}
}

Signature
Knute Johnson
email s/nospam/knute/
Molon labe...