Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. package com.mathmaurer.jeu;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6.  
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JPanel;
  9.  
  10.  
  11. @SuppressWarnings("serial")
  12. public class Scene extends JPanel{
  13.    
  14.    
  15.     // Image de fond imgFond fait 740 sur 360
  16.     private ImageIcon icoFond;
  17.     private Image imgFond1;
  18.     private Image imgFond2;
  19.    
  20.     // image panneauDépart
  21.    
  22.     private ImageIcon icoDepart;
  23.     private Image imgDepart;
  24.    
  25.     // Image personnage
  26.    
  27.     private ImageIcon icoMario;
  28.     private Image imgMario;
  29.    
  30.     //
  31.    
  32.     private int xFond1; // abscisse coin supérieur haut
  33.     private int xFond2;
  34.     private int dx; // deplace l'écran verticalement
  35.     private int xPos; // permet de laisser fi
  36.    
  37.     /* CONSTRUCTEUR */
  38.    
  39.     public Scene(){
  40.         super();
  41.         this.xFond1 = -50; // déborde de chaque coté
  42.         this.xFond2 = 690; // -50 + 740            
  43.         this.dx = 0;
  44.         this.xPos = -1;
  45.        
  46.         // Image de fond
  47.        
  48.         icoFond = new ImageIcon(getClass().getResource("/images/fondEcran.png"));
  49.         this.imgFond1 = this.icoFond.getImage();
  50.         this.imgFond2 = this.icoFond.getImage();
  51.         icoMario = new ImageIcon(getClass().getResource("/images/persoNormal.png"));
  52.         this.imgMario = this.icoMario.getImage();
  53.        
  54.         // Image départ
  55.        
  56.         this.icoDepart = new ImageIcon(getClass().getResource("/images/panneauDepart.png"));
  57.         this.imgDepart = this.icoDepart.getImage();
  58.        
  59.         // Infos par rapport à la fenetre
  60.        
  61.         this.setFocusable(true); //
  62.         this.requestFocusInWindow(); // pour être sûr de récuperer le focus
  63.         this.addKeyListener(new Clavier()); // ecoute les evenements au clavier de la scéne
  64.        
  65.         Thread chronoEcran = new Thread(new Chrono()); //
  66.         chronoEcran.start();
  67.     }
  68.    
  69.     /* GETTERS */
  70.    
  71.     public int getDx() {return dx;}
  72.    
  73.     public int getxPos() {return xPos;}
  74.  
  75.        
  76.     /* SETTERS */
  77.  
  78.     public void setDx(int dx) {this.dx = dx;}
  79.    
  80.     public void setxPos(int xPos) {this.xPos = xPos;}
  81.    
  82.     public void setxFond1(int xFond1) {this.xFond1 = xFond1;}
  83.  
  84.     public void setxFond2(int xFond2) {this.xFond2 = xFond2;}
  85.  
  86.     /* METHODES */
  87.    
  88.     public void deplacementFond(){
  89.        
  90.         if(this.xPos >= 0){ // si xPos sup à 0 alors on peut bouger et si xpos égal -1 on pourra pas aller vers la gauche (bloquer)
  91.             this.xPos = this.xPos + this.dx;
  92.             this.xFond1 = this.xFond1 - this.dx; // la position va être mise a jour avec la pos de dx en moins
  93.             this.xFond2 = this.xFond2 - this.dx;
  94.         }      
  95.         // pour mettre l'image de fond  à la suite
  96.         if(this.xFond1 == -740){ this.xFond1 = 740; }
  97.         else if(this.xFond2 == -740){ this.xFond2 = 740; }
  98.         else if(this.xFond1 == 740){ this.xFond1 = -740; }
  99.         else if(this.xFond2 == 740){ this.xFond2 = -740; }
  100.     }
  101.    
  102.  
  103.     public void paintComponent(Graphics g){ // insére les images
  104.        
  105.         super.paintComponent(g); // méthode héridité donc super méthode
  106.         Graphics g2 = (Graphics2D)g;
  107.        
  108.         this.deplacementFond();
  109.        
  110.         // Image de fond
  111.        
  112.         g2.drawImage(this.imgFond1, this.xFond1, 0, null); // Dessin de l'image de fond
  113.         g2.drawImage(this.imgFond2, this.xFond2, 0, null);
  114.         g2.drawImage(this.imgMario, 150, 280, null); // provisoire
  115.        
  116.         // Image de départ
  117.        
  118.         g2.drawImage(this.imgDepart, 35 - this.xPos, 285, null);
  119.        
  120.        
  121.     }
  122.  
  123. }
  124.  
  125.  
  126. // Clavier.java
  127.  
  128. package com.mathmaurer.jeu;
  129.  
  130. import java.awt.event.KeyEvent;
  131. import java.awt.event.KeyListener;
  132.  
  133. public class Clavier implements KeyListener{
  134.  
  135.     @Override
  136.     public void keyPressed(KeyEvent e) { // appuye sur la touche
  137.        
  138.         if(e.getKeyCode() == KeyEvent.VK_RIGHT){
  139.             // Annule le décalage de 1 créé par deplacementFond
  140.             if(Main.scene.getxPos() == -1){
  141.                 Main.scene.setxPos(0); // Réinitialisation de setxPOS
  142.                 Main.scene.setxFond1(-50); // Réinitialisation de xFond 1
  143.                 Main.scene.setxFond1(690); // Réinitialisation de xFond 2
  144.             }
  145.             Main.scene.setDx(1);
  146.         } else if(e.getKeyCode() == KeyEvent.VK_LEFT){
  147.             Main.scene.setDx(-1);
  148.         }
  149.            
  150.        
  151.     }
  152.  
  153.     @Override
  154.     public void keyReleased(KeyEvent e) {Main.scene.setDx(0);} // relache la touche
  155.  
  156.  
  157.     @Override
  158.     public void keyTyped(KeyEvent e) {
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement