IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

[Débutant][Appli]Appeller une classe.


Sujet :

Langage Java

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut [Débutant][Appli]Appeller une classe.
    Bonjour,

    J'ai une classe "Ouvrir" qui ouvre une petite fenetre avec un bouton. Jusque la pas de probleme, mais je voudrais appeller une deuxième classe "sound" (qui elle lit un son) via la classe Ouvrir.
    J'avais pensé à un code de ce genre:
    sound sound = new sound();
    Mais eclipse me trouve une erreur!
    Y a -t-il une autre facon d'appeller la classe sound via la classe Ouvrir ?
    Merci d'avance, je débute

  2. #2
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2006
    Messages
    380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2006
    Messages : 380
    Points : 314
    Points
    314
    Par défaut
    En général les noms des classes commencent avec une majuscule...
    et je ne crois pas que ce soit très correct ta déclaration avec le même nom pour la classe que pour son instance...

  3. #3
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Oui c'est vrai, j'ai fait ca vite; mais la majuscule ne va rien changer.

  4. #4
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2006
    Messages
    380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2006
    Messages : 380
    Points : 314
    Points
    314
    Par défaut
    Eh bien écris voir le message d'erreur

  5. #5
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Message d'erreur:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor sound() is undefined

    at Ouvrir.(init)(Ouvrir.java:48)
    at Ouvrir.main(Ouvrir.java:65)
    Classe Ouvrir:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.io.*; 
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.io.*; 
    import javax.sound.sampled.*; 
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.io.*; 
     
    public class Ouvrir extends JFrame implements ActionListener{ 
     
            JButton open = new JButton("Lecture en cours..."); //nouveau bouton open 
            JTextField status = new JTextField("PlayerSound V1"); //nouveau champs de texte
            JButton fin = new JButton("Lecture terminée..."); //nouveau bouton open
     
           public Ouvrir() { 
           super("PlayerSound V1"); //titre 
           setSize(300,70); //taille 
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//fermeture 
        status.setEditable(false); 
        open.addActionListener(this);//ajout d'un actionlistener 
        JPanel pane = new JPanel(); 
            BorderLayout bord = new BorderLayout(); 
            pane.setLayout(bord); 
            //pane.add("Center", status); 
            pane.add("Center", open); 
            pane.add("Center", fin);
            setContentPane(pane); 
     
            setVisible(true);
            sound sound = new sound();
        } 
     
        public static void main(String[] arguments) { 
        try{ 
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
        } 
        catch (Exception e) 
        { 
        } 
            Ouvrir index = new Ouvrir(); 
            } 
     
        public void actionPerformed(ActionEvent evt) {     
        //JFileChooser chooser = new JFileChooser();//création dun nouveau filechosser 
        //chooser.setApproveButtonText("Choix du fichier..."); //intitulé du bouton 
        //chooser.showOpenDialog(null); //affiche la boite de dialogue 
        //if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
            {     
        //status.setText(chooser.getSelectedFile().getAbsolutePath()); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte 
        } 
          } 
    }
    Classe sound:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.io.*; 
    import javax.sound.sampled.*; 
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.io.*; 
     
    //public class sound extends JFrame implements ActionListener{ 
     
       public class sound { 
        private AudioFormat format; 
        private byte[] samples; 
     
        public sound(String filename){ 
            try{ 
                AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename)); 
                format = stream.getFormat(); 
                samples = getSamples(stream); 
            } 
            catch (UnsupportedAudioFileException e){ 
                e.printStackTrace(); 
        } 
        catch (IOException e){ 
                e.printStackTrace(); 
            } 
        } 
     
        public byte[] getSamples(){ 
            return samples; 
        } 
     
        public byte[] getSamples(AudioInputStream stream){ 
            int length = (int)(stream.getFrameLength() * format.getFrameSize()); 
            byte[] samples = new byte[length]; 
            DataInputStream in = new DataInputStream(stream); 
            try{ 
                in.readFully(samples); 
            } 
            catch (IOException e){ 
                e.printStackTrace(); 
            } 
            return samples; 
        } 
     
        public void play(InputStream source){ 
            // 100 ms buffer for real time change to the sound stream 
            int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); 
            byte[] buffer = new byte[bufferSize]; 
            SourceDataLine line; 
            try{ 
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 
                line = (SourceDataLine)AudioSystem.getLine(info); 
                line.open(format, bufferSize); 
            } 
            catch (LineUnavailableException e){ 
                e.printStackTrace(); 
                return; 
            } 
            line.start(); 
            try{ 
                int numBytesRead = 0; 
                while (numBytesRead != -1){ 
                    numBytesRead = source.read(buffer, 0, buffer.length); 
                    if (numBytesRead != -1) 
                        line.write(buffer, 0, numBytesRead); 
                } 
            } 
            catch (IOException e){ 
                e.printStackTrace(); 
            } 
            line.drain(); 
            line.close(); 
        } 
     
        public static void main(String[] args){ 
            sound player = new sound("beep.wav"); 
            InputStream stream = new ByteArrayInputStream(player.getSamples()); 
            player.play(stream); 
            System.exit(0); 
        } 
    }
    Voila, j'ai donné le maximum.

    Edit

  6. #6
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2006
    Messages
    380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2006
    Messages : 380
    Points : 314
    Points
    314
    Par défaut
    Eh bien c'est très simple....
    Tu n'a pas créé de constructeur vide, donc tu ne peux pas appeler ta classe sans rien entre les parenthèses... Ta classe demande un string...
    Il faudrait par exemple écrire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    //je ne met pas de majuscule puisque tu n'en a pas...
    sound sound=new sound("nomdefichier.mp3");
    mais mieux serait que tu corrige tes majuscules aussi, ça serait plus clair !!!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Sound sound= new Sound("nom de fichier");

  7. #7
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    J'ai essayé ton code, c'est ca qu'il faut utiliser.
    Mais dans l'autre classe (sound) quelle code dois-je mettre pour que ca lise le fichier entre parenthèse ?

  8. #8
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2006
    Messages
    380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2006
    Messages : 380
    Points : 314
    Points
    314
    Par défaut
    Cette fois ci je ne peux plus t'aider... Le truc à l'air d'ouvrir le fichier avec le AudioInputStream... Mais après je ne sais pas comment cela fonctionne
    A+

  9. #9
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    OK, merci quand même.

  10. #10
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    64
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 64
    Points : 50
    Points
    50
    Par défaut Ajout
    j'ajout a ce que tralloc dir que t'as oublier de mettre l'instruction :
    pour la classe Sound puisqu'elle hérite de JFrame.

  11. #11
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    En fait, mon code était bon mais il fallait enveler la classe "main" de sound. Maintenant, le programme fonctionne.

Discussions similaires

  1. Réponses: 14
    Dernier message: 15/12/2005, 18h46
  2. Réponses: 4
    Dernier message: 08/11/2005, 15h10
  3. Réponses: 2
    Dernier message: 15/08/2005, 20h54
  4. [Débutant(e)]Instancier une classe connaissant son nom (String)
    Par Invité dans le forum API standards et tierces
    Réponses: 5
    Dernier message: 17/06/2005, 11h05
  5. [débutant]Faire appel à une action d'une ActionList
    Par petitours dans le forum C++Builder
    Réponses: 6
    Dernier message: 12/03/2004, 22h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo