Created
September 26, 2015 04:46
-
-
Save danleyb2/46a7032117fd71f9acae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anim; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.io.IOException; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
@SuppressWarnings("serial") | |
public class SpriteSheetAnim extends JPanel { | |
public Image imgSprite; | |
public URL imgUrl; | |
public static SpriteSheetAnim walkingGirl; | |
public int ROWS = 4, COLUMNS = 4; | |
public int imgWidth, imgHeight; | |
public int spritePosition = 0; | |
public SpriteSheetAnim() { | |
imgUrl = getClass().getClassLoader().getResource("spritesheet.png"); | |
if (imgUrl != null) { | |
try { | |
imgSprite = ImageIO.read(imgUrl); | |
} catch (IOException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} else { | |
System.err.println("null image url"); | |
} | |
imgWidth = imgSprite.getWidth(null) / COLUMNS; | |
imgHeight = imgSprite.getHeight(null) / ROWS; | |
Thread animationThread = new Thread() { | |
@Override | |
public void run() { | |
while (true) { | |
// System.out.println("Frame number : "+spritePosition); | |
update(); | |
repaint(); | |
try { | |
Thread.sleep(50); | |
} catch (Exception e) { | |
} | |
} | |
} | |
}; | |
animationThread.start(); | |
this.setPreferredSize(new Dimension(imgWidth, imgHeight)); | |
} | |
public int getSpriteX(int position) { | |
switch (position) { | |
//TODO get values in a better way | using an algorithm | |
case 0 : | |
case 4 : | |
case 8 : | |
case 12 : | |
return 0 * imgWidth; | |
case 1 : | |
case 5 : | |
case 9 : | |
case 13 : | |
return 1 * imgWidth; | |
case 2 : | |
case 6 : | |
case 10 : | |
case 14 : | |
return 2 * imgWidth; | |
case 3 : | |
case 7 : | |
case 11 : | |
case 15 : | |
return 3 * imgWidth; | |
default : | |
return 0; | |
} | |
} | |
public int getSpriteY(int position) { | |
switch (position) { | |
//TODO get values in a better way |using an algorithm | |
case 0 : | |
case 1 : | |
case 2 : | |
case 3 : | |
return 0 * imgHeight; | |
case 4 : | |
case 5 : | |
case 6 : | |
case 7 : | |
return 1 * imgHeight; | |
case 8 : | |
case 9 : | |
case 10 : | |
case 11 : | |
return 2 * imgHeight; | |
case 12 : | |
case 13 : | |
case 14 : | |
case 15 : | |
return 3 * imgHeight; | |
default : | |
return 0; | |
} | |
} | |
@Override | |
protected void paintComponent(Graphics graphics) { | |
graphics.drawImage(imgSprite, | |
0, // destination startX | |
0, // destination startY | |
imgWidth, // img width at destination | |
imgHeight, // img height at destination | |
getSpriteX(spritePosition), // source start x | |
getSpriteY(spritePosition), // source start y | |
getSpriteX(spritePosition) + imgWidth, // source end x | |
getSpriteY(spritePosition) + imgHeight, // source end y | |
null); | |
} | |
private void update() { | |
spritePosition++; | |
if (spritePosition > 15) { | |
spritePosition = 0; | |
} | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
// TODO Auto-generated method stub | |
JFrame window = new JFrame("Walking Girl"); | |
walkingGirl = new SpriteSheetAnim(); | |
window.setContentPane(walkingGirl); | |
window.pack(); | |
window.setAlwaysOnTop(true); | |
window.setVisible(true); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.setLocationRelativeTo(null); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment