Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with Java Swing

Options
  • 18-03-2012 9:07pm
    #1
    Closed Accounts Posts: 2,087 ✭✭✭


    Hi all. Any help much appreciated.

    I've just re-typed the following code I took from this YouTube video.

    I'm using NetBeans and have 7 .png files in the src folder in the project folder. When I run the main class, I get the JFrame but the pictures do not display, never mind animate. I've looked over it loads of times and either I'm blind or it's re-typed word for word. We've only begun using Swing in college so I'm pretty new to some of the code.

    Does anyone know why it's not working?
    package zetcode;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class second extends JPanel implements ActionListener {
        private Timer animator;
        private ImageIcon imageArray[];
        private int delay = 40, totalFrames = 7, currentFrame = 0;
        
            public second(){
                imageArray = new ImageIcon[totalFrames];
                for (int i = 0; i < imageArray.length; i++){
                    imageArray[i] = new ImageIcon("Wave "+i+".png");
                }
                animator = new Timer(delay, this);
                animator.start();
            }
    
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                if (currentFrame >= imageArray.length){
                    currentFrame = 0;
                }
                currentFrame++;
                imageArray[currentFrame].paintIcon(this, g, 0, 0);
                
    
            }
            public void actionPerformed(ActionEvent e){
                repaint();
            }
    }
    
    
    package zetcode;
    
    import javax.swing.JFrame;
    
    public class Animate01 {
        public static void main (String args[]){
            JFrame f = new JFrame();
            second s = new second();
            f.add(s);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(800,200);
        }
    }
    


Comments

  • Closed Accounts Posts: 2,087 ✭✭✭Clanket


    Figured it out eventually. Changed the following line in the 'second' constructor from:
    imageArray[i] = new ImageIcon("Wave "+i+".png");
    
    to this:
    imageArray[i] = new ImageIcon(this.getClass().getResource("Wave "+i+".png"));
    
    Can anyone tell me how it ran on his machine without the "this.getClass().getResource" part? I'm working in NetBeans on a Windows PC whereas he is using Eclipse on a Mac. Would it be something to do with how the pictures are saved in the project folder?

    Again, thanks for any help.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    imageArray[i] = new ImageIcon("Wave "+i+".png");
    

    this worked on mac because Java's default paths are compatible with mac.
    this.getClass().getResource
    

    this returns the path for the specific platform of the JVM

    read up on the "java PathSeperator". It helps when developing cross platform.


Advertisement