Results 1 to 3 of 3

Thread: Draw Animation

  1. #1
    Bryan_James is offline Member
    Join Date
    Jul 2015
    Location
    Philippines
    Posts
    2
    Rep Power
    0

    Unhappy Draw Animation

    Hi! I'm currently using Java for a simple game (card game). But I have a lot of trouble with animating the elements. Right now, I want a simple 'draw card' animation. I am using Swing and NetBeans form designer. I used null layout then add a panel called "playerDrawPath". This panel is a vertically oriented panel [110, 430]. When a function is called, this panel should add a label (it's a label without text, just image) to itself. Then the label should animate bit by bit changing it's bounds lower the frame. When the bottom is reached, the panel should clear it's contents (which in my case, the only content is the label that holds the image of the card). But when I click the deck, the card won't animate (nor appear on the panel). What's wrong?

    Java Code:
        
        int bottom = 2;
        
        private void
                drawAnim(int position)
        {
            
            java.awt.Rectangle rectangle;
            javax.swing.JLabel lblBack = new javax.swing.JLabel();
            lblBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/gallery/DECK_BACK.png").getPath()));
            playerDrawPath.add(lblBack);
            playerDrawPath.repaint();
            playerDrawPath.revalidate();
            playerDrawPath.updateUI();
            
            if (position == top) {
                
            } else if (position == bottom) {
                
                for (int count = 0; count < playerDrawPath.getHeight(); count++) {
                    rectangle = lblBack.getBounds();
                    lblBack.setBounds(lblBack.getBounds().x, lblBack.getBounds().y + count, lblBack.getWidth(), lblBack.getHeight());
                    try {
                        System.out.println ("set bounds"); // check if the program reaches this code.
                        Thread.sleep (50);
                    } catch (InterruptedException ie) {
                        
                    }
                }
                
            } else if (position == left) {
                
            } else {
                
            }
            
            playerDrawPath.removeAll();
            playerDrawPath.updateUI();
        }
    Then when the deck has been clicked:
    Java Code:
         private void playerDeckMouseClicked(java.awt.event.MouseEvent evt) {                                            
            // TODO add your handling code here:
            drawAnim(bottom);
        }
    So far the code reaches the System.out.println() statement. But still it doesn't show or even animate the card. Why? What should I do?
    Thanks in advance!
    Last edited by Bryan_James; 07-17-2016 at 10:04 AM. Reason: added status checking

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Draw Animation

    Can you make a small, simple program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Draw Animation

    It looks to me like you are doing that on the Swing thread (properly known as the Event Dispatch Thread, or EDT).

    This is the thread that Swing uses to pass event information around.
    By doing all your animation work in this thread (including your sleep()) the EDT never gets a chance to actually do any of the drawing.

    Your animation should launch its own thread (possibly SwingTimer?) on which the calculations are done, and that then tells your GUI to repaint().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Animation
    By yobabs in forum Java Applets
    Replies: 4
    Last Post: 03-13-2014, 12:39 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  3. How to draw line with animation
    By hnchnc in forum Java 2D
    Replies: 1
    Last Post: 11-18-2010, 10:49 PM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •