OVH Cloud OVH Cloud

Self-Learning - Phone Applet (Please help !)

1 réponse
Avatar
Latyr_Dev
Bonjour,

Le code ci dessous est cens=E9 "=E9muler" un telephone portable avec une=20
image de fond, un clavier et un =E9cran.
L'applet doit s'initialiser dans une fenetre avec pour titre le Modele=20
de telephone et son numero
Le Num compos=E9 doit apparaitre dans sur l'afficheur. Et l'appui sur la=20
touche Appel doit faire le son de chaque chiffre puis sonner.

1) Quand j'execute mon code, j'ai 2 fenetres au lieu d'une
-> une qui contient l'image de fond
-> l'autre qui est la fenetre avec le titre et le telephone.

2) Je n'ai que le son du 1er digit puis la sonnerie. Comment faire pour=20
entendre chaque son et la sonnerie.

Je ne veux pas utiliser dans un premier temps SWING (je veux bien=20
comprendre les principes avec AWT) quelqu'un pourrait m'expliquer ?

Merci d'avance

Latyr

import java.applet.*;
import java.awt.*;
import java.awt.event.*;


/*
* Cr=E9=E9 le 2 juil. 2004
*
* Pour changer le mod=E8le de ce fichier g=E9n=E9r=E9, allez =E0 :
* Fen=EAtre>Pr=E9f=E9rences>Java>G=E9n=E9ration de code>Code e=
t=20
commentaires
*/

/**
* @author JLF928
*
* Pour changer le mod=E8le de ce commentaire de type g=E9n=E9r=E9, allez =
=E0 :
* Fen=EAtre>Pr=E9f=E9rences>Java>G=E9n=E9ration de code>Code e=
t=20
commentaires
*/
public class Phone1 extends Applet implements ActionListener, Runnable {
=09 Button=09touche1,=20
=09=09=09touche2,=20
=09=09=09touche3,
=09=09=09touche4,
=09=09=09touche5,
=09=09=09touche6,
=09=09=09touche7,
=09=09=09touche8,
=09=09=09touche9,
=09=09=09touche0,
=09=09=09dieze,
=09=09=09etoile,
=09=09=09appel,
=09=09=09annuler,
=09=09=09menu;
=09
=09AudioClip =09son1, son2, son3, son4, son5, son6,=20
=09=09=09=09son7, son8, son9, son0, sondieze, sonetoile,
=09=09=09=09sonne;
=09=09=09=09
=09Image image;=09
=09
=09TextField ecran;
=09
=09Frame modele;
=09Panel coque;
=09Panel clavier;
=09
=09Thread monThread;
=09
=09String numero;

=09
=09public void init(){
=09=09//Initialisation des variables
=09=09/*Cr=E9ation du t=E9l=E9phone*/
=09=09numero =3D "06.03.84.47.31";
=09=09modele =3D new Frame("Siemens C60 : " + numero);
=09=09// this.add(modele);
=09=09
=09=09/* Creation des Panel*/
=09=09coque =3D new Panel(new BorderLayout());
=09=09clavier =3D new Panel(new GridLayout(5,3));
=09=09
=09=09/* Creation des Boutons */
=09=09touche1 =3D new Button("1");=20
=09=09touche2 =3D new Button("2"); =20
=09=09touche3 =3D new Button("3");
=09=09touche4 =3D new Button("4");
=09=09touche5 =3D new Button("5");
=09=09touche6 =3D new Button("6");
=09=09touche7 =3D new Button("7");
=09=09touche8 =3D new Button("8");
=09=09touche9 =3D new Button("9");
=09=09touche0 =3D new Button("0");
=09=09dieze =3D new Button("#");
=09=09etoile =3D new Button("*");
=09=09appel =3D new Button("Appel");
=09=09annuler =3D new Button("annuler");
=09=09menu =3D new Button("Menu");
=09=09
=09=09/* Creation des bips */
=09=09son1 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.1.au");
=09=09son2 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.2.au");
=09=09son3 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.3.au");
=09=09son4 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.4.au");
=09=09son5 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.5.au");
=09=09son6 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.6.au");=20
=09=09son7 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.7.au");
=09=09son8 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.8.au");
=09=09son9 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.9.au");
=09=09son0 =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.0.au");
=09=09sondieze =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.pound.au");
=09=09sonetoile =3D getAudioClip(this.getDocumentBase(),=20
"touchtone.star.au");
=09=09sonne =3D getAudioClip(this.getDocumentBase(), "ring.au");

=09=09/* Creation de l'image de fond */=09=09
=09=09image =3D getImage(this.getDocumentBase(), "img05.gif");
=09=09
=09=09/* Creation de l'ecran */
=09=09 ecran =3D new TextField();
=09=09 ecran.setEditable(false);=20
=09=09=20
=09=09//Ajout des listeners
=09=09touche1.addActionListener(this);=20
=09=09touche2.addActionListener(this); =20
=09=09touche3.addActionListener(this);
=09=09touche4.addActionListener(this);
=09=09touche5.addActionListener(this);
=09=09touche6.addActionListener(this);
=09=09touche7.addActionListener(this);
=09=09touche8.addActionListener(this);
=09=09touche9.addActionListener(this);
=09=09touche0.addActionListener(this);
=09=09dieze.addActionListener(this);
=09=09etoile.addActionListener(this);
=09=09appel.addActionListener(this);
=09=09annuler.addActionListener(this);
=09=09menu.addActionListener(this);
=09=09
=09=09//Creation de l'interface graphique
=09=09/* Creation du clavier*/
=09=09clavier.add(annuler);
=09=09clavier.add(menu);
=09=09clavier.add(appel);
=09=09clavier.add(touche1);
=09=09clavier.add(touche2);
=09=09clavier.add(touche3);
=09=09clavier.add(touche4);
=09=09clavier.add(touche5);
=09=09clavier.add(touche6);
=09=09clavier.add(touche7);
=09=09clavier.add(touche8);
=09=09clavier.add(touche9);
=09=09clavier.add(etoile);
=09=09clavier.add(touche0);
=09=09clavier.add(dieze);

=09=09/*Ecran au dessus*/
=09=09coque.add(ecran,BorderLayout.NORTH);
=09=09coque.add(clavier, BorderLayout.SOUTH);

=09=09/* Creation de la fenetre */
=09=09modele.add(coque);
=09=09//this.add(modele) //Cette ligne genere une erreur de=20
compilation
=09=09modele.setVisible(true);
=09=09
=09}
=09
=09public void actionPerformed(ActionEvent event){
=09=09String numero =3D ecran.getText();
=09=09if (event.getActionCommand().compareTo
(touche1.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche1.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche2.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche2.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche3.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche3.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche4.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche4.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche5.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche5.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche6.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche6.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche7.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche7.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche8.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche8.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche9.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche9.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(touche0.getActionCommand())=3D=3D0){
=09=09=09numero+=3Dtouche0.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(etoile.getActionCommand())=3D=3D0){
=09=09=09numero+=3Detoile.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(dieze.getActionCommand())=3D=3D0){
=09=09=09numero+=3Ddieze.getActionCommand();
=09=09=09ecran.setText(numero);
=09=09}
=09=09if (event.getActionCommand().compareTo
(annuler.getActionCommand())=3D=3D0){
=09=09=09effacer();
=09=09}
=09=09if (event.getActionCommand().compareTo
(appel.getActionCommand())=3D=3D0){
=09=09=09composer();
=09=09}
=09=09
=09=09
=09}
=09
=09public void effacer() {
=09=09ecran.setText("");
=09}
=09
=09
=09public void composer(){
=09=09String numero =3D ecran.getText();
=09=09
=09=09for(int i=3D0; i<numero.length(); i++) {
=09=09=09switch(numero.charAt(i)) {
=09=09=09=09case '1':
=09=09=09=09=09son1.play();
=09=09=09=09break;
=09=09=09=09case '2':
=09=09=09=09=09son2.play();
=09=09=09=09break;
=09=09=09=09case '3':
=09=09=09=09=09son3.play();
=09=09=09=09break;
=09=09=09=09case '4':
=09=09=09=09=09son4.play();
=09=09=09=09break;
=09=09=09=09case '5':
=09=09=09=09=09son5.play();
=09=09=09=09break;
=09=09=09=09case '6':
=09=09=09=09=09son6.play();
=09=09=09=09break;
=09=09=09=09case '7':
=09=09=09=09=09son7.play();
=09=09=09=09break;
=09=09=09=09case '8':
=09=09=09=09=09son8.play();
=09=09=09=09break;
=09=09=09=09case '9':
=09=09=09=09=09son9.play();
=09=09=09=09break;
=09=09=09=09case '0':
=09=09=09=09=09son0.play();
=09=09=09=09break;
=09=09=09=09case '*':
=09=09=09=09=09sonetoile.play();
=09=09=09=09break;
=09=09=09=09case '#':
=09=09=09=09=09sondieze.play();
=09=09=09=09break;
=09=09=09}
=09=09=09monThread.start();=09=09=09
=09=09}
=09=09sonne.play();
=09=09effacer();
=09}
=09
=09public void start(){
=09=09if (monThread =3D=3D null)
=09=09=09monThread =3D new Thread();=09=09
=09}
=09
=09public void run(){
=09=09try {
=09=09=09Thread.sleep(1000);
=09=09} catch (InterruptedException e) {
=09=09=09e.printStackTrace();
=09=09}
=09}

=09public void paint(Graphics g)
=09=09{
=09=09g.drawImage(this.image,0,0,this);
=09=09}

}

1 réponse

Avatar
Guillaume CABANAC
Salut

Pour améliorer ton code, tu pourrais stocker tes touches dans un

Button[] touches = new Button[10] ;

Et les initialiser dans une boucle :

for(int i=0, i < 10 ; i++)
touches[i] = new Button[i] ;

Idem pour les sons...

Ca améliorerait le switch qui n'aurait plus lieu d'être :

switch(numero.charAt(i)) {
case '1':
son1.play();
break;
case '2':
son2.play();


sons[numero.charAt(i)].play() ;

Et pleins d'autres choses....

Tafanor