Mes TD de programmation orientée objet (INSA Toulouse)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sprite.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Un sprite est une image qui a vocation a se deplacer.
  3. * (bas niveau)
  4. */
  5. import java.awt.* ;
  6. import java.awt.image.* ;
  7. public class Sprite {
  8. // Position, dimension et image du sprite
  9. public int x, y ;
  10. private int width, height ;
  11. public BufferedImage image ;
  12. // ImagePanel ou se trouve le sprite
  13. private ImagePanel ip ;
  14. // Animation du robot : sequence d'images
  15. private BufferedImage[] seq ;
  16. // Index dans la sequence. -1 signifie qu'on ne la joue pas.
  17. private int indexSeq ;
  18. // Nombre d'expositions de chaque image de la sequence
  19. private final int dureeExposition = 6 ;
  20. private int compteurExposition ;
  21. private boolean repeat ;
  22. /** Renvoie la largeur du sprite */
  23. public int getLarg() { return this.width ; }
  24. /** Renvoie la hauteur du sprite */
  25. public int getHaut() { return this.height ; }
  26. public Sprite (String imgfile, int x, int y, ImagePanel ip) {
  27. this.x = x ;
  28. this.y = y ;
  29. this.seq = null ;
  30. this.indexSeq = -1 ;
  31. this.ip = ip ;
  32. this.image = Images.get(imgfile) ;
  33. this.width = this.image.getWidth() ;
  34. this.height = this.image.getHeight() ;
  35. }
  36. // Indique à l'image panel de se redessiner sur la zone du sprite.
  37. private void repaint() {
  38. this.ip.repaint (0, this.x, this.y, width, height) ;
  39. }
  40. public void moveTo (int x, int y) {
  41. // Il faudra redessiner l'endroit d'où il part
  42. this.repaint() ;
  43. this.x = x ;
  44. this.y = y ;
  45. // Et l'endroit où il arrive.
  46. this.repaint() ;
  47. }
  48. public void playSequence(BufferedImage[] seq, boolean repeat) {
  49. this.seq = seq ;
  50. this.indexSeq = 0 ;
  51. this.compteurExposition = this.dureeExposition ;
  52. this.repeat = repeat ;
  53. }
  54. public void dessine(Graphics g, ImagePanel im) {
  55. if (this.indexSeq >= 0 && this.indexSeq < this.seq.length) {
  56. this.image = this.seq[this.indexSeq] ;
  57. this.width = this.image.getWidth() ;
  58. this.height = this.image.getHeight() ;
  59. if (this.compteurExposition-- <= 0) {
  60. this.compteurExposition = this.dureeExposition ;
  61. this.indexSeq ++ ;
  62. }
  63. if (this.indexSeq >= this.seq.length && this.repeat) {
  64. this.indexSeq = 0 ;
  65. }
  66. }
  67. g.drawImage(this.image, this.x, this.y, im) ;
  68. }
  69. }