Results 1 to 6 of 6
  1. #1
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Question Java Sprite animation

    Hello i was wondering how i can animate using the drawImage of 3 images

    so i have image1.png image2.png and image3.png

    the images have a stick man on it walking the images contain transparency as well

    any ideas?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Java Sprite animation

    A suggestion is to have an array of images, and a counter which goes through them (this may not be the best approach, but it's my thought)

    Java Code:
    private Image[] images = new Image[3];
    private int current = 0;
    public Image getNextImage(){
      if(current==images.length){
        current=0;
      }
      return images[current++];
    }

  3. #3
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: Java Sprite animation

    ok so how could i do this in a loop?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Java Sprite animation

    Each time you want to draw to the image/panel/whatever you are drawing to, you can call this method. Maybe use a Swing Timer and have it continuously draw the next method.

  5. #5
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: Java Sprite animation

    ok got it sorted thanks i used a swing timer to switch between frames

  6. #6
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: Java Sprite animation

    Ok so the swing timer is taking up to much memory in terms of file space and the ram memory how can i load an animated gif on the drawimage thing i have used imageIcon but it fails to load lots of images at the same time ..

    here is my scenario i have a player class and is created by another class serval times

    Java Code:
    import java.io.*;
    import javax.imageio.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class player
    {
    
        private Image walk_0,walk_1,walk_2,walk_3;
        private Image still_0,still_1,still_2,still_3;
    
        private Image playerImage_walk;
        private Image playerImage_still;
    
        public boolean walking = true;
    
        public int direction = 2;
    
        //set Player Database Stuff
    
        public String playername = "dave";
        private String gender = "";
        public int mapid = 1;
    
        public int x_pos = 150;
        public int y_pos = 150;
    
        public player()
        {
            getPlayerimages();
        }
    
        public void getPlayerimages(){
    
            try{
                //Walking Images
                
                ImageIcon walk0file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_0.gif"));
                walk_0 = walk0file.getImage();
    
                ImageIcon walk1file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_1.gif"));
                walk_1 = walk1file.getImage();
    
                ImageIcon walk2file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_2.gif"));
                walk_2 = walk2file.getImage();
    
                ImageIcon walk3file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_3.gif"));
                walk_3 = walk3file.getImage();
    
                ImageIcon still0file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_0.png"));
                still_0 = still0file.getImage();
    
                ImageIcon still1file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_1.png"));
                still_1 = still1file.getImage();
    
                ImageIcon still2file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_2.png"));
                still_2 = still2file.getImage();
    
                ImageIcon still3file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_3.png"));
                still_3 = still3file.getImage();
    
            }catch(Exception e){
                e.printStackTrace();
            }
    
        }
    
        public void drawPlayer(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            Font f = new Font("SansSerif", Font.BOLD, 11);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setFont(f);
            g2d.drawString(playername,x_pos,y_pos-4);
    
            if(walking == true){
                if(direction == 0){
                    playerImage_walk = walk_0;
                }else if(direction == 1){
                    playerImage_walk = walk_1;
                }else if(direction == 2){
                    playerImage_walk = walk_2;
                }else if(direction == 3){
                    playerImage_walk = walk_3;
                }
    
                g.drawImage(playerImage_walk, x_pos, y_pos,32,48, null);
            }else{
                if(direction == 0){
                    playerImage_still = still_0;
                }else if(direction == 1){
                    playerImage_still = still_1;
                }else if(direction == 2){
                    playerImage_still = still_2;
                }else if(direction == 3){
                    playerImage_still = still_3;
                }
                g.drawImage(playerImage_still, x_pos, y_pos,32,48, null);
            }
        }
    }

Similar Threads

  1. import sprite
    By Bimz in forum New To Java
    Replies: 0
    Last Post: 08-28-2011, 12:15 PM
  2. Sprite-sheet vs individual images
    By David M. in forum Java Gaming
    Replies: 9
    Last Post: 08-13-2011, 08:02 PM
  3. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 06:16 AM
  4. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  5. [j2me] sprite rotation with degrees
    By Rooneyz in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-06-2009, 03:40 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
  •