Changeset 8edd04e in lost-haven for gamegui/Animation.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (4 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/Animation.java
r155577b r8edd04e 1 1 package gamegui; 2 2 3 import java.awt. *;4 import java.awt.image. *;5 import java.util. *;3 import java.awt.Graphics; 4 import java.awt.image.BufferedImage; 5 import java.util.ArrayList; 6 6 7 public class Animation extends Member 8 { 9 ArrayList<BufferedImage> frames; 10 int currentFrame; 11 int drawInterval; 12 long lastFrameChange; 13 14 public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval) 15 { 16 super(newName, newX, newY, newWidth, newHeight); 17 18 frames = new ArrayList<BufferedImage>(); 19 currentFrame = 0; 20 drawInterval = newInterval; 21 lastFrameChange = 0; 22 } 23 24 public void addFrame(BufferedImage newFrame) 25 { 26 frames.add(newFrame); 27 } 28 29 public void draw(Graphics g) 30 { 31 if(System.currentTimeMillis() - lastFrameChange > drawInterval) 32 { 33 currentFrame++; 34 if(currentFrame >= frames.size()) 35 currentFrame = 0; 36 37 lastFrameChange = System.currentTimeMillis(); 38 } 39 40 g.drawImage(frames.get(currentFrame), getX(), getY(), null); 41 } 7 public class Animation extends Member { 8 9 public ArrayList<BufferedImage> frames; 10 public long drawInterval; 11 public boolean wrap; 12 13 int currentFrame; 14 long lastFrameChange; 15 boolean reachedEnd; 16 17 public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval, boolean wrap) { 18 super(newName, newX, newY, newWidth, newHeight); 19 this.frames = new ArrayList<BufferedImage>(); 20 this.currentFrame = 0; 21 this.drawInterval = newInterval; 22 this.lastFrameChange = 0L; 23 this.reachedEnd = false; 24 this.wrap = wrap; 25 } 26 27 public Animation(Animation copy) { 28 super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight()); 29 this.frames = copy.frames; 30 this.currentFrame = copy.currentFrame; 31 this.drawInterval = copy.drawInterval; 32 this.lastFrameChange = 0L; 33 this.reachedEnd = false; 34 this.wrap = copy.wrap; 35 } 36 37 public void addFrame(BufferedImage newFrame) { 38 this.frames.add(newFrame); 39 setWidth(newFrame.getWidth()); 40 setHeight(newFrame.getHeight()); 41 } 42 43 public void draw(Graphics g) { 44 if (this.lastFrameChange == 0L) 45 this.lastFrameChange = System.currentTimeMillis(); 46 if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) { 47 this.currentFrame++; 48 if (this.currentFrame >= this.frames.size()) { 49 if (this.wrap) { 50 this.currentFrame = 0; 51 } else { 52 this.currentFrame--; 53 } 54 this.reachedEnd = true; 55 } 56 this.lastFrameChange = System.currentTimeMillis(); 57 } 58 g.drawImage(this.frames.get(this.currentFrame), getX(), getY(), null); 59 } 60 61 public void draw(Graphics g, int x, int y) { 62 if (this.lastFrameChange == 0L) 63 this.lastFrameChange = System.currentTimeMillis(); 64 if (System.currentTimeMillis() - this.lastFrameChange > this.drawInterval) { 65 this.currentFrame++; 66 if (this.currentFrame >= this.frames.size()) { 67 if (this.wrap) { 68 this.currentFrame = 0; 69 } else { 70 this.currentFrame--; 71 } 72 this.reachedEnd = true; 73 } 74 this.lastFrameChange = System.currentTimeMillis(); 75 } 76 g.drawImage(this.frames.get(this.currentFrame), getX() + x - ((BufferedImage)this.frames.get(this.currentFrame)).getWidth() / 2, getY() + y - ((BufferedImage)this.frames.get(this.currentFrame)).getHeight(), null); 77 } 78 79 public boolean reachedEnd() { 80 return this.reachedEnd; 81 } 82 83 public void reset() { 84 this.reachedEnd = false; 85 this.currentFrame = 0; 86 this.lastFrameChange = 0L; 87 } 88 89 public int getWidth() { 90 return ((BufferedImage)this.frames.get(this.currentFrame)).getWidth(); 91 } 92 93 public int getHeight() { 94 return ((BufferedImage)this.frames.get(this.currentFrame)).getHeight(); 95 } 42 96 }
Note:
See TracChangeset
for help on using the changeset viewer.