Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.java
- import java.io.File;
- public class Main {
- private static boolean versionJar;
- public static void main(String[] args) {
- if (!new File(Frame.class.getProtectionDomain().getCodeSource().getLocation().getFile()).isDirectory())
- {versionJar = true;}
- else {versionJar = false;}
- new Frame();
- }
- public static boolean isVersionJar()
- {
- return versionJar;
- }
- }
- //Frame.java
- import java.awt.Color;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Frame extends JFrame{
- static Panel panel = new Panel();
- static Clavier kl = new Clavier();
- public static boolean music = false;
- public static boolean musicIsPlaying = false;
- public static boolean isFlying = false;
- public Frame()
- {
- this.setTitle("Test de la gravité !");
- this.setSize(800,500);
- this.setLocationRelativeTo(null);
- this.setContentPane(panel);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.addKeyListener(kl);
- this.setVisible(true);
- Trame();
- }
- public void Trame()
- {
- while(true)
- {
- panel.repaint();
- try {Thread.sleep(10);}
- catch (InterruptedException e) {e.printStackTrace();}
- if (music == true && !musicIsPlaying)
- {
- System.out.println("Avant chargement musique");
- String chemin = "sons/NCS.wav";
- if (Main.isVersionJar())
- {
- UnZip.unZip(Frame.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "sons/");
- }
- else
- {
- chemin = Frame.class.getResource(chemin).getFile();
- }
- Musique NCS = new Musique(chemin);
- NCS.start();
- music = false;
- }
- if (isFlying) {fly();}
- }
- }
- public void fly()
- {
- int i = 10;
- while (i > 1)
- {
- Panel.personnage.setY(Panel.personnage.getY() - i);
- i /= 2;
- panel.repaint();
- try { Thread.sleep(100); }
- catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println(Panel.personnage.getY());
- }
- while (i < 10)
- {
- Panel.personnage.setY(Panel.personnage.getY() + i);
- i *= 2;
- panel.repaint();
- try { Thread.sleep(100); }
- catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println(Panel.personnage.getY());
- }
- Panel.personnage.setY(Panel.personnage.getY() + 2);
- isFlying = false;
- }
- }
- //Panel.java
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- public class Panel extends JPanel{
- static Personnage personnage;
- public void paintComponent(Graphics g)
- {
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- if (personnage == null) {personnage = new Personnage(Color.BLUE);}
- g.setColor(personnage.getColor());
- g.drawImage(new ImageIcon(getClass().getResource("/images/personnage.png")).getImage(), personnage.getX(), personnage.getY(), personnage.getTaille(), personnage.getTaille(), null);
- }
- }
- //Personnage.java
- import java.awt.Color;
- public class Personnage {
- private int taille;
- private Color couleur;
- private int x;
- private int y;
- public Personnage(Color pCouleur)
- {
- this.taille = 200;
- this.couleur = pCouleur;
- this.x = Frame.panel.getWidth()/2-this.taille;
- this.y = Frame.panel.getHeight()/2-this.taille;
- }
- public int getTaille() { return this.taille; }
- public Color getColor() { return this.couleur; }
- public int getX() { return this.x; }
- public int getY() { return this.y; }
- public void setX(int x) { this.x = x; }
- public void setY(int y) { this.y = y; }
- }
- //Clavier.java
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Clavier implements KeyListener {
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == 38) {
- if (!Frame.musicIsPlaying)
- {
- Frame.music = true;
- }
- Frame.isFlying = true;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- }
- //Musique.java
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.util.ArrayList;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.DataLine;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.SourceDataLine;
- import javax.sound.sampled.UnsupportedAudioFileException;
- public class Musique extends Thread{
- static private String chemin;
- static private boolean isSet = false;
- static private ArrayList<Musique> instances = new ArrayList<>();
- public Musique(String chemin)
- {
- this.chemin = chemin;
- this.isSet = true;
- }
- public void run()
- {
- if(isSet)
- {
- Frame.musicIsPlaying = true;
- SourceDataLine ligne;
- File file;
- AudioInputStream audioInput;
- AudioFormat formatAudio;
- file = new File(this.chemin);
- if (Main.isVersionJar())
- {
- file.deleteOnExit();
- }
- try {
- audioInput = AudioSystem.getAudioInputStream(file);
- } catch (UnsupportedAudioFileException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
- }
- formatAudio = audioInput.getFormat();
- DataLine.Info info = new DataLine.Info(SourceDataLine.class, formatAudio);
- try {
- ligne = (SourceDataLine) AudioSystem.getLine(info);
- } catch (LineUnavailableException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
- }
- try {
- ligne.open(formatAudio);
- } catch (LineUnavailableException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
- }
- ligne.start();
- try{
- byte bytes[] = new byte[1024];
- System.out.println("Test");
- int bytesRead = 0;
- while ((bytesRead = audioInput.read(bytes, 0, bytes.length)) != 1)
- {
- ligne.write(bytes, 0, bytesRead);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
- }
- ligne.close();
- }
- Frame.musicIsPlaying = false;
- }
- public String getChemin()
- {
- return this.chemin;
- }
- public static ArrayList<Musique> getInstances()
- {
- return instances;
- }
- }
- //UnZip.java
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- public class UnZip {
- private static final int BUFFER = 2048;
- public static void unZip(String archive, String dossier)
- {
- System.out.println("Décompression de ressources");
- byte data[] = new byte[BUFFER];
- BufferedOutputStream dest = null;
- FileInputStream fis;
- try {
- fis = new FileInputStream(archive);
- BufferedInputStream buffi = new BufferedInputStream(fis);
- ZipInputStream zis = new ZipInputStream(buffi);
- try {
- ZipEntry entry;
- while ((entry = zis.getNextEntry()) != null)
- {
- if (entry.getName().contains(dossier))
- {
- if (entry.isDirectory())
- {
- System.out.println("Extraction de " + entry.getName());
- new File(entry.getName()).mkdir();
- }
- else
- {
- System.out.println("Extraction de " + entry.getName());
- FileOutputStream fos = new FileOutputStream(entry.getName());
- dest = new BufferedOutputStream(fos, BUFFER);
- int count;
- while ((count = zis.read(data, 0, BUFFER)) != -1)
- {
- dest.write(data, 0, count);
- }
- }
- }
- }
- zis.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Extraction terminée !");
- }
- public static void unZipAll(String archive)
- {
- unZip(archive, "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement