Advertisement
G-Rath

Character

Mar 14th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.awt.Toolkit;
  8.  
  9. import java.io.*;
  10. import javax.imageio.*;
  11.  
  12. import java.awt.Toolkit;
  13. import java.awt.event.ActionEvent;
  14.  
  15. import java.awt.event.ActionListener;
  16.  
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JPanel;
  19. import javax.swing.Timer;
  20.  
  21. public class Character extends JPanel   implements ActionListener
  22. {
  23.     /** Timer used for sprite animation */
  24.     private Timer timer;
  25.  
  26.     /** The width of each individual sprite on the sprite sheet */
  27.     private int spriteWidth = 32;
  28.     /** The height of each individual sprite on the sprite sheet */
  29.     private int spriteHeight = 48;
  30.  
  31.     /** The current animation frame we are up to */
  32.     private int iCurrentAnimationImage = 0;
  33.  
  34.     /** Path for the location of this charactors sprite sheet */
  35.     private String spriteSheetFilePath = "James.png";
  36.     private BufferedImage[] charRight;
  37.     private BufferedImage charStill;
  38.    
  39.     private boolean bIsMoving;
  40.  
  41.  
  42.     /**
  43.      * Constructor for objects of class Character
  44.      */
  45.     public Character()
  46.     {
  47.         loadImage();
  48.         initCharacter();
  49.     }
  50.  
  51.     private void loadImage()
  52.     {
  53.         charRight   = spriteSheetRead( 2, 4 );
  54.         charStill   = getSingleSprite( 0, 0 );
  55.     }
  56.  
  57.     private void initCharacter()
  58.     {
  59.         Color c = new Color( 0,0,0,0 );
  60.         setBackground( c );
  61.  
  62.         setDoubleBuffered( true );
  63.  
  64.         timer = new Timer( 200, this ); //Create a new timer for use with the characters sprite animation
  65.         timer.start();
  66.     }
  67.  
  68.     public void actionPerformed( ActionEvent e )
  69.     {
  70.         iCurrentAnimationImage++;
  71.  
  72.         if( iCurrentAnimationImage > 3 )
  73.             iCurrentAnimationImage = 0;
  74.  
  75.         repaint();
  76.  
  77.     }
  78.  
  79.     public BufferedImage getSingleSprite( int rowNumber, int colNumber )
  80.     {
  81.         try
  82.         {
  83.             int rows = 4;
  84.             int cols = 4;
  85.  
  86.             BufferedImage spriteSheet = ImageIO.read( new File( spriteSheetFilePath ) );
  87.  
  88.             BufferedImage sprite = spriteSheet.getSubimage( colNumber * spriteWidth, rowNumber * spriteHeight, spriteWidth, spriteHeight );
  89.  
  90.             return sprite;
  91.         }
  92.         catch( IOException e )
  93.         {
  94.             System.out.println( "ERROR!" );
  95.             e.printStackTrace();
  96.         }
  97.  
  98.         return null; //If we catch an exception, we dont have anything to return but SOMETHING is expected to be returned, so return null
  99.     }
  100.  
  101.     public BufferedImage[] spriteSheetRead( int rowNumber, int colCount ) //reads in image file and divides it into the sprites  
  102.     {  
  103.         //32 x 48
  104.  
  105.         try
  106.         {  
  107.             int rows = 4;  
  108.             int cols = 4;
  109.             BufferedImage[] sprites = new BufferedImage[ cols ];
  110.  
  111.             BufferedImage spriteSheet = ImageIO.read( new File( spriteSheetFilePath ) );  
  112.  
  113.             {  
  114.                 //BufferedImage[] sprites = new BufferedImage[cols];  
  115.                 for( int j = 0; j < cols; j++ )  
  116.                 {  
  117.                     //System.out.println( j );
  118.                     sprites[ j ] = spriteSheet.getSubimage( j * spriteWidth, rowNumber * spriteHeight, spriteWidth, spriteHeight );  
  119.                 }
  120.                 return sprites;  
  121.             }  
  122.  
  123.         }
  124.         catch( IOException e )
  125.         {
  126.             System.out.println( "ERROR!" );
  127.             e.printStackTrace();  
  128.         }  
  129.  
  130.         return null; //If we catch an exception, we dont have anything to return but SOMETHING is expected to be returned, so return null
  131.     }  
  132.  
  133.     @Override
  134.     public void paintComponent( Graphics g )
  135.     {                
  136.         //System.out.println( "paint comp" );
  137.         super.paintComponent( g );
  138.         drawCharactor(g);
  139.     }  
  140.  
  141.     private void drawCharactor( Graphics g )
  142.     {
  143.         Graphics2D g2d = (Graphics2D) g;
  144.  
  145.         if( bIsMoving )
  146.             g2d.drawImage( charRight[ iCurrentAnimationImage ], 0, 0, this);
  147.         else
  148.             g2d.drawImage( charStill, 0, 0, this );
  149.  
  150.         Toolkit.getDefaultToolkit().sync();
  151.         //g.dispose();
  152.         //repaint();
  153.     }
  154.  
  155.     public void stopMoving()
  156.     {
  157.         bIsMoving = false;
  158.     }
  159.    
  160.     public void startMoving()
  161.     {
  162.         bIsMoving = true;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement