Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

Animation on a JPanel

Thread view: 
J. Babe - 28 May 2004 21:59 GMT
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...



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.