Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: SWING ANIMATIONS

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default SWING ANIMATIONS

    (i dont know if this is the proper place to post this, i think the problem can be categorize as SWING problems please move if innapropriate)

    hello everyone
    i am just trying to code a simple shooting game. the code works properly so far(except for a bug on the first shoot). my problem is after few shoots the animation of the target becomes slow. i dont know what is the source of this problem? could anyone look at my code and tell me whats wrong?? thanks.

    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
     
    public class FrameMain extends javax.swing.JFrame {
        final int speed = 5;
        int dirx = speed;
        int diry = speed;
     
        /** Creates new form FrameMain */
        public FrameMain() {
            initComponents();
            setLocationRelativeTo(null);
            bullet.setVisible(false);
     
            //ANIMATE THE TARGET
            Timer t = new Timer(1, new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
                    if(obj1.getLocation().x>=500)dirx = -dirx;
                    if(obj1.getLocation().x<=0)dirx = speed;
                    obj1.setLocation(obj1.getLocation().x+dirx, obj1.getLocation().y);
     
                }
            });
     
            t.start();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            obj = new javax.swing.JLabel();
            obj1 = new javax.swing.JLabel();
            bullet = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setMinimumSize(new java.awt.Dimension(500, 500));
            setResizable(false);
            addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseMoved(java.awt.event.MouseEvent evt) {
                    formMouseMoved(evt);
                }
            });
            addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    formKeyPressed(evt);
                }
            });
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
     
            obj.setBackground(new java.awt.Color(0, 0, 0));
            obj.setText("jLabel1");
            obj.setOpaque(true);
            getContentPane().add(obj, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 370, 20, 20));
     
            obj1.setBackground(new java.awt.Color(0, 0, 0));
            obj1.setText("jLabel1");
            obj1.setOpaque(true);
            getContentPane().add(obj1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 70, 20, 20));
     
            bullet.setText("*");
            getContentPane().add(bullet, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 370, 10, -1));
     
            pack();
        }// </editor-fold>
     
        private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
     
        }                               
     
       //KEY LISTENERS
        private void formKeyPressed(java.awt.event.KeyEvent evt) {
     
     
           if(evt.getKeyCode()==KeyEvent.VK_RIGHT)
           obj.setLocation(obj.getLocation().x+2, obj.getLocation().y);
           if(evt.getKeyCode()==KeyEvent.VK_LEFT)
           obj.setLocation(obj.getLocation().x-2, obj.getLocation().y);
     
           if(evt.getKeyCode()==KeyEvent.VK_SPACE)
           checkCollision();
        }
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FrameMain().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JLabel bullet;
        private javax.swing.JLabel obj;
        private javax.swing.JLabel obj1;
        // End of variables declaration
     
    //RELEASE BULLET AND CHECK FOR COLLISION
        private void checkCollision() {
            obj.setLocation(obj.getLocation().x, obj.getLocation().y);
            bullet.setLocation(obj.getLocation());
            bullet.setVisible(true);
            bullet.setVisible(true);
            Timer t = new Timer(5, new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
                    bullet.setLocation(bullet.getLocation().x, bullet.getLocation().y-5);
                    Rectangle r1 = new Rectangle(obj1.getLocation().x, obj1.getLocation().y, obj1.getWidth(), obj1.getHeight());
                    Rectangle r = new Rectangle(bullet.getLocation().x, bullet.getLocation().y, bullet.getWidth(), bullet.getHeight());
                    if(r.intersects(r1)){
                        bullet.setVisible(false);
                        obj1.setVisible(false);
                        JOptionPane.showMessageDialog(null, "THIS IS SPARTA!!");
                    }
                }
            });
            t.start();
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: SWING ANIMATIONS

    the animation of the target becomes slow.
    Is this because the time between moving the target gets longer or the paint method is called less frequently or the distance the target is moved is less or ????

    Try debugging your code by Adding some print outs to the code to show what the cause of the slow down is. Print out the values of all the variables that control how fast the target moves.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SWING ANIMATIONS

    Are the animations intended to be continual? I don't see where they are ever stopped, so over time you may accumulate bunches of animation Timers who end up all competing for the same thread

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: SWING ANIMATIONS

    i see the problem now.. i am running 2 timers simultaneously.. i put the animation on the same timers and its working perfectly now...

Similar Threads

  1. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  2. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM
  3. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  4. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM
  5. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM