Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Error with animation using spriteSheets  (Read 464 times)
0 Members and 1 Guest are viewing this topic.
Offline redpepper303

Junior Newbie





« Posted 2012-08-24 17:49:11 »

Hello!
First of all this is my fist time using this forum so please forgive me if I do something wrong.

I started developing a game, and managed to create a world, collision detection, etc... I'm now stuck on a very important part: animation. I got a sprite sheet, and creating a class that would let me take subimages. I then created an array list of these subimages for when the player moves right for example. I then tried to create a loop, that would take each subimage, draw them to the screen, wait 30 milliseconds, then draw the next one, etc...

It all worked out, though i encountered one problem: the previous image that was drawn to the screen does not disappear. I then tried to call the repaint() method after the 30 milliseconds, though that doesn't work. I eventually told myself that the images were cycling maybe too quickly, so I created a new class, and made it 100 milliseconds, but even that didn't work.

What im asking is for someone to help me fix this. I don't know if I have to change the hole code, or just a bit. I did try to use a Timer, but that didn't really work out, as I dont exactly know how to really use it.

Thank you in advanced, and here is the code:

NOTE: There will probably be errors in this code, as i have only taken the essential parts of it. The method i need help in is the draw() method. The init() method uses methods from another class that I haven't included. Sorry to not have included everything, but it would have resulted in nearly 1000 lines of code, and complicate everything.


002   import java.awt.*;
003   import java.awt.event.*;
004   import java.awt.image.BufferedImage;
005   import java.io.IOException;
006   import java.net.URL;
007   import java.util.logging.Level;
008   import java.util.logging.Logger;
009   import javax.imageio.ImageIO;
010   import javax.swing.*;
011   import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler;
012   
013   import java.awt.*;
014   import javax.swing.*;
015   import java.awt.event.*;
016   import java.awt.image.BufferedImage;
017   import java.io.IOException;
018   import java.util.ArrayList;
019   import java.util.logging.Level;
020   import java.util.logging.Logger;
021   
022   
023   
024   public class Animation extends JFrame{
025        
026    
027       BufferedImage stillRight;
028    
029       static BufferedImage right[];
030        
031       //Double buffering
032       Image dbImage;
033       Graphics dbg;
034        
035      
036   
037       //Initializing Menu.java
038       static Menu menu = new Menu();
039        
040       //Variables for screen size
041       int
042       GWIDTH = 600,
043       GHEIGHT = 525;
044       //Dimension of GWIDTH*GHEIGHT
045       Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
046        
047      
048    
049       //Create constructor to spawn window
050       public Animation(){
051            
052           this.setTitle("LendaryCastle");
053           this.setSize(screenSize);
054           this.setResizable(false);
055           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
056           this.setVisible(true);
057           this.setBackground(Color.DARK_GRAY);
058   
059       }
060          
061        
062        
063       public static void main(String[] args){
064           Animation main = new Animation();
065          
066          
067       }
068        
069       private void init(){
070            
071            
072           BufferedImage spriteSheet = null;
073           try {
074               spriteSheet = loadImage("Monk2.png");
075           } catch (IOException ex) {
076               Logger.getLogger(Animator.class.getName()).log(Level.SEVERE, null, ex);
077           }
078           SpriteSheet ss = new SpriteSheet(spriteSheet);
079            
080      
081           right = new BufferedImage[5];
082           right[0] = ss.grabSprite(19*256, 512, 256, 256);
083           right[1] = ss.grabSprite(2*256, 512, 256, 256);
084           right[2] = ss.grabSprite(12*256, 512, 256, 256);
085           right[3] = ss.grabSprite(5*256, 512, 256, 256);
086           right[4] = ss.grabSprite(25*256, 512, 256, 256);
087          
088                
089                
090            
091            
092       }
093        
094        
095       public BufferedImage loadImage(String pathRelativeToThis) throws IOException{
096            
097           URL url = this.getClass().getResource(pathRelativeToThis);
098           BufferedImage img = ImageIO.read(url);
099           return img;
100       }
101        
102        
103       @Override
104       public void paint(Graphics g){
105           dbImage = createImage(getWidth(), getHeight());
106           dbg = dbImage.getGraphics();
107           draw(dbg);
108           g.drawImage(dbImage, 0, 25, this);
109       }
110        
111        
112       public void draw(Graphics g){
113              
114                    
115                    
116                   for(int i = 0; i<right.length; i++){
117                        
118                        
119                       stillRight = right;
120                        
121                        
122                       g.drawImage(stillRight, i*30, 0, 60, 55, null);
123                        
124                   try {
125                        
126                       Thread.sleep(100);
127                        
128                   } catch (InterruptedException ex) {
129                       Logger.getLogger(Animation.class.getName()).log(Level.SEVERE, null, ex);
130                   }
131                   }
132                    
133                
134                
135            
136                
137           }
138            
139      
140           repaint();
141       }
142   
143   }
Offline sproingie

JGO Kernel


Medals: 200



« Reply #1 - Posted 2012-08-24 18:34:51 »

Please use the [ code ] [ /code ] tags and remove the line numbers and the vast amount of vertical whitespace in the code.
Offline redpepper303

Junior Newbie





« Reply #2 - Posted 2012-08-24 18:47:15 »

NOTE: Sorry about the previous post, I didn't think to put the code tags, and I do tend to overy exagerate spaces Tongue. Here is the correct format.

Hello!
First of all this is my fist time using this forum so please forgive me if I do something wrong.

I started developing a game, and managed to create a world, collision detection, etc... I'm now stuck on a very important part: animation. I got a sprite sheet, and creating a class that would let me take subimages. I then created an array list of these subimages for when the player moves right for example. I then tried to create a loop, that would take each subimage, draw them to the screen, wait 30 milliseconds, then draw the next one, etc...

It all worked out, though i encountered one problem: the previous image that was drawn to the screen does not disappear. I then tried to call the repaint() method after the 30 milliseconds, though that doesn't work. I eventually told myself that the images were cycling maybe too quickly, so I created a new class, and made it 100 milliseconds, but even that didn't work.

What im asking is for someone to help me fix this. I don't know if I have to change the hole code, or just a bit. I did try to use a Timer, but that didn't really work out, as I dont exactly know how to really use it.

Thank you in advanced, and here is the code:

NOTE: There will probably be errors in this code, as i have only taken the essential parts of it. The method i need help in is the draw() method. The init() method uses methods from another class that I haven't included. Sorry to not have included everything, but it would have resulted in nearly 1000 lines of code, and complicate everything.

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  
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

 

  public class Animation extends JFrame{

      BufferedImage stillRight;
      static BufferedImage right[];
      //Double buffering
     Image dbImage;
      Graphics dbg;

 

      //Initializing Menu.java

      static Menu menu = new Menu();

       //Variables for screen size

      int

       GWIDTH = 600,

      GHEIGHT = 525;

       //Dimension of GWIDTH*GHEIGHT
    Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
       
     
       //Create constructor to spawn window
      public Animation(){
           
           this.setTitle("LendaryCastle");
           this.setSize(screenSize);
           this.setResizable(false);
           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           this.setVisible(true);
           this.setBackground(Color.DARK_GRAY);
   
       }
         
       public static void main(String[] args){
           Animation main = new Animation();
         
         
       }
       
       private void init(){
           
           
           BufferedImage spriteSheet = null;
           try {
               spriteSheet = loadImage("Monk2.png");
           } catch (IOException ex) {
               Logger.getLogger(Animator.class.getName()).log(Level.SEVERE, null, ex);
           }
           SpriteSheet ss = new SpriteSheet(spriteSheet);
           
     
           right = new BufferedImage[5];
           right[0] = ss.grabSprite(19*256, 512, 256, 256);
           right[1] = ss.grabSprite(2*256, 512, 256, 256);
           right[2] = ss.grabSprite(12*256, 512, 256, 256);
           right[3] = ss.grabSprite(5*256, 512, 256, 256);
           right[4] = ss.grabSprite(25*256, 512, 256, 256);
         
       }
       
       
       public BufferedImage loadImage(String pathRelativeToThis) throws IOException{
           
           URL url = this.getClass().getResource(pathRelativeToThis);
           BufferedImage img = ImageIO.read(url);
           return img;
       }
       
       @Override
       public void paint(Graphics g){
           dbImage = createImage(getWidth(), getHeight());
           dbg = dbImage.getGraphics();
           draw(dbg);
           g.drawImage(dbImage, 0, 25, this);
       }
       
       public void draw(Graphics g){
           
                   for(int i = 0; i<right.length; i++){  
                       stillRight = right;
                       g.drawImage(stillRight, i*30, 0, 60, 55, null);
                       
                   try {
                       
                       Thread.sleep(100);
                       
                   } catch (InterruptedException ex) {
                       Logger.getLogger(Animation.class.getName()).log(Level.SEVERE, null, ex);
                   }
                   }
 
           }
           
     
           repaint();
       }
   
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!