- #1
iamjon.smith
- 117
- 3
JAVA Animated Logo **DUE TODAY** HELP!
My instructor has provided the code to use, we were told to simply modify the images for the program to work with our logo. The code comes straight from the textbook.
I have followed the steps of my instructor but when I run the program I get the following error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at LogoAnimatorJPanel.<init>(LogoAnimatorJPanel.java:39)
at LogoAnimator.main(LogoAnimator.java:19)
Java Result: 1
Code is as follows:
class
main class:
I am using a folder named Images to contain all of the images for the animation. The images are named "1" through "113" respectively, for a total of 113 images. They were created in paint and are .png images. I have included the first three images as attachments for you to use.
Please, help me as soon as possible. I just need to be pointed in the right direction. I have run the debugger and set breakpoints to try and figure out what is wrong, but I am lost in the sauce.
My instructor has provided the code to use, we were told to simply modify the images for the program to work with our logo. The code comes straight from the textbook.
I have followed the steps of my instructor but when I run the program I get the following error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at LogoAnimatorJPanel.<init>(LogoAnimatorJPanel.java:39)
at LogoAnimator.main(LogoAnimator.java:19)
Java Result: 1
Code is as follows:
class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jon and Jessica
*/
// Fig. 21.2: LogoAnimatorJPanel.java
// Animation of a series of images.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class LogoAnimatorJPanel extends JPanel
{
private final static String IMAGE_NAME = "1"; // base image name
protected ImageIcon images[]; // array of images
private final int TOTAL_IMAGES = 112; // number of images
private int currentImage = 0; // current image index
private final int ANIMATION_DELAY = 50; // millisecond delay
private int width; // image width
private int height; // image height
private Timer animationTimer; // Timer drives animation
// constructor initializes LogoAnimatorJPanel by loading images
public LogoAnimatorJPanel()
{
images = new ImageIcon[ TOTAL_IMAGES ];
// load 30 images
for ( int count = 0; count < images.length; count++ )
images[ count ] = new ImageIcon( getClass().getResource(
"Images/" + IMAGE_NAME + count + ".png" ) );
// this example assumes all images have the same width and height
width = images[ 0 ].getIconWidth(); // get icon width
height = images[ 0 ].getIconHeight(); // get icon height
} // end LogoAnimatorJPanel constructor
// display current image
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass paintComponent
images[ currentImage ].paintIcon( this, g, 0, 0 );
// set next image to be drawn only if Timer is running
if ( animationTimer.isRunning() )
currentImage = ( currentImage + 1 ) % TOTAL_IMAGES;
} // end method paintComponent
// start animation, or restart if window is redisplayed
public void startAnimation()
{
if ( animationTimer == null )
{
currentImage = 0; // display first image
// create timer
animationTimer =
new Timer( ANIMATION_DELAY, new TimerHandler() );
animationTimer.start(); // start Timer
} // end if
else // animationTimer already exists, restart animation
{
if ( ! animationTimer.isRunning() )
animationTimer.restart();
} // end else
} // end method startAnimation
// stop animation Timer
public void stopAnimation()
{
animationTimer.stop();
} // end method stopAnimation
// return minimum size of animation
public Dimension getMinimumSize()
{
return getPreferredSize();
} // end method getMinimumSize
// return preferred size of animation
public Dimension getPreferredSize()
{
return new Dimension( width, height );
} // end method getPreferredSize
// inner class to handle action events from Timer
private class TimerHandler implements ActionListener
{
// respond to Timer's event
public void actionPerformed( ActionEvent actionEvent )
{
repaint(); // repaint animator
} // end method actionPerformed
} // end class TimerHandler
} // end class LogoAnimatorJPanel
/**************************************************************************
* (C) Copyright 1992-2007 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
main class:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jon and Jessica
*/
// Fig. 21.3: LogoAnimator.java
// Animation of a series of images.
import javax.swing.JFrame;
public class LogoAnimator
{
// execute animation in a JFrame
public static void main( String args[] )
{
LogoAnimatorJPanel animation = new LogoAnimatorJPanel();
JFrame window = new JFrame( "Animator test" ); // set up window
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.add( animation ); // add panel to frame
window.pack(); // make window just large enough for its GUI
window.setVisible( true ); // display window
animation.startAnimation(); // begin animation
} // end main
} // end class LogoAnimator
/**************************************************************************
* (C) Copyright 1992-2007 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
I am using a folder named Images to contain all of the images for the animation. The images are named "1" through "113" respectively, for a total of 113 images. They were created in paint and are .png images. I have included the first three images as attachments for you to use.
Please, help me as soon as possible. I just need to be pointed in the right direction. I have run the debugger and set breakpoints to try and figure out what is wrong, but I am lost in the sauce.