Results 1 to 8 of 8
  1. #1
    tomas1991 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default help with Swing Timer animation

    hey guys, I have a problem.

    I've inserted a swing timer into a Swing Gui, to shift icons in a label, so that i could make a stopmotion animation. The code has no errors at all, but something is going wrong anyways. I have no clue what it is so I'll ask you.

    Here the full code:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * basementcorridor6.java
     *
     * Created on 14-03-2010, 11:59:59
     */
    
    package somethingmanor;
    
    import java.awt.event.*;
    
    
    
    
    
    
    
    /**
     *
     * @author Tomas Nielsen
     */
    public class basementcorridor6 extends javax.swing.JFrame {
    
        /** Creates new form basementcorridor6 */
        public basementcorridor6() {
            initComponents();
            
     javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
              if (Main.QT1 == 1){
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/somethingmanor/Animation  to the manor/Quicktime animations/Quicktime1-1.gif")));
    
              }
              if (Main.QT1 == 2){
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/somethingmanor/Animation  to the manor/Quicktime animations/Quicktime1-2.gif")));
    
              }
              if (Main.QT1 == 3){
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/somethingmanor/Animation  to the manor/Quicktime animations/Quicktime1-3.gif")));
    
              }
              if (Main.QT1 == 3){
                  Main.QT1 = 1;
              }
              else{
                  Main.QT1 = Main.QT1 + 1;
              }
     }
     });
    
    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() {
            java.awt.GridBagConstraints gridBagConstraints;
    
            jPanel3 = new javax.swing.JPanel();
            jTextField1 = new javax.swing.JTextField();
            jButton4 = new javax.swing.JButton();
            jTextField3 = new javax.swing.JTextField();
            jTextField7 = new javax.swing.JTextField();
            jTextField6 = new javax.swing.JTextField();
            jTextField5 = new javax.swing.JTextField();
            jTextField4 = new javax.swing.JTextField();
            jTextField11 = new javax.swing.JTextField();
            jTextField10 = new javax.swing.JTextField();
            jTextField9 = new javax.swing.JTextField();
            jTextField8 = new javax.swing.JTextField();
            jPanel1 = new javax.swing.JPanel();
            jPanel2 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel3.setBackground(new java.awt.Color(255, 255, 255));
            jPanel3.setPreferredSize(new java.awt.Dimension(600, 119));
    
            jTextField1.setText("Are all these doors cells too? Someone have been busy");
            jTextField1.setBorder(null);
    
            jButton4.setText("Character Sheet");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            jTextField3.setBorder(null);
    
            jTextField7.setText("STR");
    
            jTextField6.setText("INT");
    
            jTextField5.setText("DEX");
    
            jTextField4.setText("CHR");
    
            jTextField11.setText("jTextField11");
    
            jTextField10.setText("jTextField10");
    
            jTextField9.setText("jTextField9");
    
            jTextField8.setText("jTextField8");
    
            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 751, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 717, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                    .addContainerGap(561, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField8, 0, 0, Short.MAX_VALUE)
                        .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField9, 0, 0, Short.MAX_VALUE)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField10, 0, 0, Short.MAX_VALUE)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField11, 0, 0, Short.MAX_VALUE)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButton4))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jButton4))
                    .addContainerGap())
            );
    
            jPanel1.setLayout(new java.awt.GridBagLayout());
    
            jPanel2.setOpaque(false);
            jPanel2.setPreferredSize(new java.awt.Dimension(800, 600));
    
            jButton1.setBorder(null);
            jButton1.setContentAreaFilled(false);
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/somethingmanor/Animation  to the manor/up.gif"))); // NOI18N
            jButton2.setBorder(null);
            jButton2.setContentAreaFilled(false);
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(482, 482, 482)
                            .addComponent(jButton1))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(356, 356, 356)
                            .addComponent(jButton2)))
                    .addContainerGap(317, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addComponent(jButton1)
                    .addGap(127, 127, 127)
                    .addComponent(jButton2)
                    .addContainerGap(374, Short.MAX_VALUE))
            );
    
            jPanel1.add(jPanel2, new java.awt.GridBagConstraints());
    
            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/somethingmanor/Animation  to the manor/basementcorridor6.gif"))); // NOI18N
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            jPanel1.add(jLabel1, gridBagConstraints);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(5, 5, 5)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            new Charsheet().setVisible(true);
            this.setVisible(false);          // TODO add your handling code here:
    }                                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            if(Main.back == 2){
                Main.back = 1;
                new corridor1left().setVisible(true);
                this.setVisible(false);
    
            } else {new basemantcorridor2().setVisible(true);
            this.setVisible(false);}         // TODO add your handling code here:
    }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            if(Main.back == 2){
                Main.back = 1;
                new corridor1left().setVisible(true);
                this.setVisible(false);
    
            }
            else{
              new basementroom1().setVisible(true);
                this.setVisible(false);
            }
            // TODO add your handling code here:
    }                                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new basementcorridor6().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField10;
        private javax.swing.JTextField jTextField11;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        private javax.swing.JTextField jTextField6;
        private javax.swing.JTextField jTextField7;
        private javax.swing.JTextField jTextField8;
        private javax.swing.JTextField jTextField9;
        // End of variables declaration                   
    
    }
    I code it in Netbeans, if it help you find the problem.
    As you can see I use a global variable, to help the timer know, when it should do what.

    this is the error Netbeans gives me, when I try to run the Jframe:
    Java Code:
    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
            at somethingmanor.basementcorridor6$1.actionPerformed(basementcorridor6.java:39)
            at javax.swing.Timer.fireActionPerformed(Timer.java:271)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    It looks like your ImageIcon cannot instantiate properly (probably because it cannot find the image you're looking for). Try writing a small program that loads an image and make sure you're getting it correctly.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with iluxa. Check the spelling of your image paths. For instance, should there be an extra space between "Animation" and "to" below? Unfortunately (or fortunately?) compilers are not very forgiving in this regard.

    Java Code:
    "/somethingmanor/Animation  to the manor/Quicktime animations/Quicktime1-1.gif"

  4. #4
    tomas1991 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    I've deleted one of the spaces you spotted, but it still gives me the same error :S
    the path of the Images is:
    Java Code:
    C:\Users\Tomas Nielsen\Documents\NetBeansProjects\something manor\src\somethingmanor\Animation to the manor\Quicktime animations
    but Netbeans only need this to find an image within a project:
    Java Code:
    \somethingmanor\Animation to the manor\Quicktime animations

  5. #5
    tomas1991 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    no one can find the problem?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    This has become a NetBeans question. I suggest it be moved to the appropriate forum.

    Did you build the project? Without that the resources won't be copied to the build folder, and that's where they'll be looked for -- relative to projectRoot/build/classes

    db

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    That said, you should use getResource / getResourceAsStream so that your program will run from an executable jar and locate resources inside the jar.

    db

  8. #8
    tomas1991 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    I'm sorry, but I have no clue about what you mean?

    Every other images in the program (and there is a lot) Works without any problems, so why don't these mages work?

Similar Threads

  1. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  2. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  3. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM
  4. Timer in Swing app - refreshing label
    By qoqosz in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 02:17 AM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM

Posting Permissions

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