Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: v0rtex (posted 2012-10-06 06:15:58, viewed 125 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258  
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.IOException;
import java.net.*;

public class AnimatedSprite extends Entity {
   private JFrame frame;
   private Graphics2D g2d;

   private BufferedImage spriteSheet;
   private BufferedImage[] animation;
   private AffineTransform at;
   private int animDirection;
   private int frameCount, frameDelay;
   private int currentFrame, totalFrames;
   private int frameWidth, frameHeight, columns;
   private int frameX, frameY;

   AnimatedSprite(JFrame frame, Graphics2D graphics) {
      super(); // call Entity constructor and set defaults
      this.frame = frame;
      g2d = graphics;
      spriteSheet = null;
      alive = true;
      currentState = State.ALIVE;
      currentFrame = 0;
      totalFrames = 1;
      animDirection = 1;
      frameCount = 0;
      frameDelay = 0;
      frameWidth = 0;
      frameHeight = 0;
      columns = 1;

   }

   public void setAnimDirection(int animDirection) {
      this.animDirection = animDirection;
   }

   public void setFrameCount(int frameCount) {
      this.frameCount = frameCount;
   }

   public void setFrameDelay(int frameDelay) {
      this.frameDelay = frameDelay;
   }

   public void setCurrentFrame(int currentFrame) {
      this.currentFrame = currentFrame;
   }

   public void setTotalFrames(int totalFrames) {
      this.totalFrames = totalFrames;
   }

   public int getAnimDirection() {
      return animDirection;
   }

   public int getFrameCount() {
      return frameCount;
   }

   public int getFrameDelay() {
      return frameDelay;
   }

   public int getTotalFrames() {
      return totalFrames;
   }

   public int getCurrentFrame() {
      return currentFrame;
   }

   public JFrame getJFrame() {
      return frame;
   }

   public Graphics2D getGraphicsObject() {
      return g2d;
   }

   public void setGraphicsObject(Graphics2D graphics) {
      g2d = graphics;
   }

   public void setImage(BufferedImage image) {
      this.spriteSheet = image;
      double x = frame.getSize().width / 2 - getImageWidth() / 2;
      double y = frame.getSize().height / 2 - getImageHeight() / 2;
      at = AffineTransform.getTranslateInstance(x, y);
   }

   public int getImageWidth() {
      if (spriteSheet != null)
         return spriteSheet.getWidth(frame);
      else
         return 0;
   }

   public int getImageHeight() {
      if (spriteSheet != null)
         return spriteSheet.getHeight(frame);
      else
         return 0;
   }

   public double getCenterX() {
      return this.x + getImageWidth() / 2;
   }

   public double getCenterY() {
      return this.y + getImageHeight() / 2;
   }

   public Rectangle getBounds() {
      Rectangle bounds = new Rectangle((int) this.x, (int) this.y,
            (int)  frameWidth, (int)frameHeight);
      return bounds;
   }

   public void load(String filename, int columns, int totalFrames, int width,
         int height) {
      Toolkit tk = Toolkit.getDefaultToolkit();
      try {
         spriteSheet = ImageIO.read(getURL("players.png"));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      while (spriteSheet.getHeight(frame) <= 0)
         ;
      double x = frame.getSize().width / 2 - getImageWidth() / 2;
      double y = frame.getSize().height / 2 - getImageHeight() / 2;
      at = AffineTransform.getTranslateInstance(x, y);
      this.columns = columns;
      this.totalFrames = totalFrames;
      this.frameWidth = width;
      this.frameHeight = height;
      animation = new BufferedImage[this.totalFrames]; // reserve space
      getAnimation();
   }

   public void transform() {
      at.setToIdentity();
      at.translate(this.getX() +  getImageWidth() / 2, 
            this.getY() + getImageHeight() / 2);
      at.rotate(Math.toRadians(getFaceAngle()));
      at.translate(-getImageWidth() / 2, -getImageHeight() / 2);
   }

   public AffineTransform getTransform() {
      return at;
   }

   public void updateSprite() {
      // update position
      x += velX;
      y += velY;

      // update transform
      transform();

      // update rotation
      // faceAngle +=5;
      this.setFaceAngle(faceAngle);
      if (faceAngle < 0)
         faceAngle = 360 - 5;
      else if (faceAngle > 360)
         faceAngle = 5;

      // update animation
      if (totalFrames > 1) {
         frameCount++;
         if (frameCount > frameDelay) {
            frameCount = 0;
            currentFrame += animDirection;
            if (currentFrame > totalFrames - 1) {
               currentFrame = 0; // first frame
            } else if (currentFrame < 0) {
               currentFrame = totalFrames - 1; // last frame
            }
         }
      }
   }

   public Image getImage() {
      return spriteSheet;
   }

   // check for collision with a rectangular shape
   public boolean collidesWith(Rectangle rectangle) {
      return (rectangle.intersects(getBounds()));
   }

   // check for collision with another sprite
   public boolean collidesWith(AnimatedSprite sprite) {
      return (getBounds().intersects(sprite.getBounds()));
   }

   private URL getURL(String filename) {
      URL url = null;
      try {
         url = this.getClass().getResource(filename);
      } catch (Exception e) {
      }
      return url;
   }

   public BufferedImage grabTile(int xpos, int ypos, int width, int height) {
      return spriteSheet.getSubimage(xpos, ypos, width, height);
   }

   public void getAnimation() { // load animation and store in BufferedImage
                           // array

      for (int i = 0; i < totalFrames; i++) {

         BufferedImage tile = grabTile(frameX, frameY, frameWidth, // grab
                                                      // needed
                                                      // tile
               frameHeight);
         getFrame();
         currentFrame++;
         animation[i] = tile;
         System.out.println(animation[i]);
      }
      currentFrame = 0;
   }

   public void getFrame() {
      frameX = (currentFrame % columns) * frameWidth;
      frameY = (currentFrame / columns) * frameHeight;
   }

   public void drawBounds(Color c) {
      g2d.setColor(c);
      g2d.draw(getBounds());
   }

   public void draw() {

   
      updateSprite();
      getFrame();
      // draw frame
      g2d.drawImage(animation[getCurrentFrame()], at, getJFrame());

   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!