Advertisement
AlphaShadows77

Son en java

Jul 27th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. //Main.java
  2. import java.io.File;
  3.  
  4. public class Main {
  5.     private static boolean versionJar;
  6.     public static void main(String[] args) {
  7.         if (!new File(Frame.class.getProtectionDomain().getCodeSource().getLocation().getFile()).isDirectory())
  8.         {versionJar = true;}
  9.         else {versionJar = false;}
  10.         new Frame();
  11.     }
  12.    
  13.     public static boolean isVersionJar()
  14.     {
  15.         return versionJar;
  16.     }
  17.    
  18.    
  19.  
  20. }
  21.  
  22. //Frame.java
  23. import java.awt.Color;
  24.  
  25. import javax.swing.JFrame;
  26. import javax.swing.JPanel;
  27.  
  28. public class Frame extends JFrame{
  29.     static Panel panel = new Panel();
  30.     static Clavier kl = new Clavier();
  31.     public static boolean music = false;
  32.     public static boolean musicIsPlaying = false;
  33.     public static boolean isFlying = false;
  34.     public Frame()
  35.     {
  36.         this.setTitle("Test de la gravité !");
  37.         this.setSize(800,500);
  38.         this.setLocationRelativeTo(null);
  39.         this.setContentPane(panel);
  40.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  41.         this.addKeyListener(kl);
  42.         this.setVisible(true);
  43.         Trame();
  44.     }
  45.    
  46.     public void Trame()
  47.     {
  48.         while(true)
  49.         {
  50.             panel.repaint();
  51.             try {Thread.sleep(10);}
  52.             catch (InterruptedException e) {e.printStackTrace();}
  53.             if (music == true && !musicIsPlaying)
  54.             {
  55.                 System.out.println("Avant chargement musique");
  56.                 String chemin = "sons/NCS.wav";
  57.                 if (Main.isVersionJar())
  58.                 {
  59.                     UnZip.unZip(Frame.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "sons/");
  60.                 }
  61.                 else
  62.                 {
  63.                     chemin = Frame.class.getResource(chemin).getFile();
  64.                 }
  65.                 Musique NCS = new Musique(chemin);
  66.                 NCS.start();
  67.                 music = false;
  68.             }
  69.             if (isFlying) {fly();}
  70.         }
  71.     }
  72.    
  73.     public void fly()
  74.     {
  75.         int i = 10;
  76.         while (i > 1)
  77.         {
  78.             Panel.personnage.setY(Panel.personnage.getY() - i);
  79.             i /= 2;
  80.             panel.repaint();
  81.             try { Thread.sleep(100); }
  82.             catch (InterruptedException e) { e.printStackTrace(); }
  83.             System.out.println(Panel.personnage.getY());
  84.         }
  85.         while (i < 10)
  86.         {
  87.             Panel.personnage.setY(Panel.personnage.getY() + i);
  88.             i *= 2;
  89.             panel.repaint();
  90.             try { Thread.sleep(100); }
  91.             catch (InterruptedException e) { e.printStackTrace(); }
  92.             System.out.println(Panel.personnage.getY());
  93.         }
  94.         Panel.personnage.setY(Panel.personnage.getY() + 2);
  95.         isFlying = false;
  96.     }
  97. }
  98.  
  99. //Panel.java
  100. import java.awt.Color;
  101. import java.awt.Graphics;
  102.  
  103. import javax.swing.ImageIcon;
  104. import javax.swing.JPanel;
  105.  
  106. public class Panel extends JPanel{
  107.     static Personnage personnage;
  108.     public void paintComponent(Graphics g)
  109.     {
  110.         g.setColor(Color.WHITE);
  111.         g.fillRect(0, 0, this.getWidth(), this.getHeight());
  112.        
  113.         if (personnage == null) {personnage = new Personnage(Color.BLUE);}
  114.         g.setColor(personnage.getColor());
  115.        
  116.         g.drawImage(new ImageIcon(getClass().getResource("/images/personnage.png")).getImage(), personnage.getX(), personnage.getY(), personnage.getTaille(), personnage.getTaille(), null);
  117.        
  118.     }  
  119. }
  120.  
  121. //Personnage.java
  122. import java.awt.Color;
  123.  
  124. public class Personnage {
  125.     private int taille;
  126.     private Color couleur;
  127.     private int x;
  128.     private int y;
  129.     public Personnage(Color pCouleur)
  130.     {
  131.         this.taille = 200;
  132.         this.couleur = pCouleur;
  133.         this.x = Frame.panel.getWidth()/2-this.taille;
  134.         this.y = Frame.panel.getHeight()/2-this.taille;
  135.     }
  136.    
  137.     public int getTaille() { return this.taille; }
  138.    
  139.     public Color getColor() { return this.couleur; }
  140.    
  141.     public int getX() { return this.x; }
  142.    
  143.     public int getY() { return this.y; }
  144.    
  145.     public void setX(int x) { this.x = x; }
  146.    
  147.     public void setY(int y) { this.y = y; }
  148. }
  149.  
  150. //Clavier.java
  151. import java.awt.event.KeyEvent;
  152. import java.awt.event.KeyListener;
  153.  
  154. public class Clavier implements KeyListener {
  155.    
  156.    
  157.    
  158.     public void keyTyped(KeyEvent e) {
  159.        
  160.     }
  161.  
  162.     public void keyPressed(KeyEvent e) {
  163.         if (e.getKeyCode() == 38) {
  164.             if (!Frame.musicIsPlaying)
  165.             {
  166.                 Frame.music = true;
  167.             }
  168.             Frame.isFlying = true;
  169.         }
  170.     }
  171.  
  172.     public void keyReleased(KeyEvent e) {
  173.        
  174.     }
  175.  
  176. }
  177.  
  178. //Musique.java
  179. import java.io.File;
  180. import java.io.IOException;
  181. import java.net.URL;
  182. import java.util.ArrayList;
  183.  
  184. import javax.sound.sampled.AudioFormat;
  185. import javax.sound.sampled.AudioInputStream;
  186. import javax.sound.sampled.AudioSystem;
  187. import javax.sound.sampled.DataLine;
  188. import javax.sound.sampled.LineUnavailableException;
  189. import javax.sound.sampled.SourceDataLine;
  190. import javax.sound.sampled.UnsupportedAudioFileException;
  191.  
  192. public class Musique extends Thread{
  193.     static private String chemin;
  194.     static private boolean isSet = false;
  195.     static private ArrayList<Musique> instances = new ArrayList<>();
  196.    
  197.     public Musique(String chemin)
  198.     {
  199.         this.chemin = chemin;
  200.         this.isSet = true;
  201.     }
  202.    
  203.     public void run()
  204.     {
  205.         if(isSet)
  206.         {
  207.             Frame.musicIsPlaying = true;
  208.             SourceDataLine ligne;
  209.             File file;
  210.             AudioInputStream audioInput;
  211.             AudioFormat formatAudio;
  212.             file = new File(this.chemin);
  213.             if (Main.isVersionJar())
  214.             {
  215.                 file.deleteOnExit();
  216.             }
  217.             try {
  218.                 audioInput = AudioSystem.getAudioInputStream(file);
  219.             } catch (UnsupportedAudioFileException e) {
  220.                 // TODO Auto-generated catch block
  221.                 e.printStackTrace();
  222.                 return;
  223.             } catch (IOException e) {
  224.                 // TODO Auto-generated catch block
  225.                 e.printStackTrace();
  226.                 return;
  227.             }
  228.             formatAudio = audioInput.getFormat();
  229.             DataLine.Info info = new DataLine.Info(SourceDataLine.class, formatAudio);
  230.             try {
  231.                 ligne = (SourceDataLine) AudioSystem.getLine(info);
  232.             } catch (LineUnavailableException e) {
  233.                 // TODO Auto-generated catch block
  234.                 e.printStackTrace();
  235.                 return;
  236.             }
  237.             try {
  238.                 ligne.open(formatAudio);
  239.             } catch (LineUnavailableException e) {
  240.                 // TODO Auto-generated catch block
  241.                 e.printStackTrace();
  242.                 return;
  243.             }
  244.             ligne.start();
  245.             try{
  246.                 byte bytes[] = new byte[1024];
  247.                 System.out.println("Test");
  248.                 int bytesRead = 0;
  249.                 while ((bytesRead = audioInput.read(bytes, 0, bytes.length)) != 1)
  250.                 {
  251.                     ligne.write(bytes, 0, bytesRead);
  252.                 }
  253.             } catch (IOException e) {
  254.                 // TODO Auto-generated catch block
  255.                 e.printStackTrace();
  256.                 return;
  257.             }
  258.             ligne.close();
  259.            
  260.         }
  261.         Frame.musicIsPlaying = false;
  262.     }
  263.    
  264.     public String getChemin()
  265.     {
  266.         return this.chemin;
  267.     }
  268.    
  269.     public static ArrayList<Musique> getInstances()
  270.     {
  271.         return instances;
  272.     }
  273. }
  274.  
  275. //UnZip.java
  276. import java.io.BufferedInputStream;
  277. import java.io.BufferedOutputStream;
  278. import java.io.File;
  279. import java.io.FileInputStream;
  280. import java.io.FileNotFoundException;
  281. import java.io.FileOutputStream;
  282. import java.io.IOException;
  283. import java.util.zip.ZipEntry;
  284. import java.util.zip.ZipInputStream;
  285.  
  286. public class UnZip {
  287.     private static final int BUFFER = 2048;
  288.  
  289.     public static void unZip(String archive, String dossier)
  290.     {
  291.         System.out.println("Décompression de ressources");
  292.         byte data[] = new byte[BUFFER];
  293.         BufferedOutputStream dest = null;
  294.         FileInputStream fis;
  295.         try {
  296.             fis = new FileInputStream(archive);
  297.             BufferedInputStream buffi = new BufferedInputStream(fis);
  298.             ZipInputStream zis = new ZipInputStream(buffi);
  299.             try {
  300.                 ZipEntry entry;
  301.                 while ((entry = zis.getNextEntry()) != null)
  302.                 {
  303.                     if (entry.getName().contains(dossier))
  304.                     {
  305.                         if (entry.isDirectory())
  306.                         {
  307.                             System.out.println("Extraction de " + entry.getName());
  308.                             new File(entry.getName()).mkdir();
  309.                         }
  310.                        
  311.                         else
  312.                         {
  313.                             System.out.println("Extraction de " + entry.getName());
  314.                             FileOutputStream fos = new FileOutputStream(entry.getName());
  315.                             dest = new BufferedOutputStream(fos, BUFFER);
  316.                             int count;
  317.                             while ((count = zis.read(data, 0, BUFFER)) != -1)
  318.                             {
  319.                                 dest.write(data, 0, count);
  320.                             }
  321.                         }
  322.                     }
  323.                 }
  324.                 zis.close();
  325.             } catch (IOException e) {
  326.                 // TODO Auto-generated catch block
  327.                 e.printStackTrace();
  328.             }
  329.         } catch (FileNotFoundException e) {
  330.             // TODO Auto-generated catch block
  331.             e.printStackTrace();
  332.         }
  333.         System.out.println("Extraction terminée !");
  334.     }
  335.    
  336.     public static void unZipAll(String archive)
  337.     {
  338.         unZip(archive, "");
  339.     }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement