Bonjour j'ai fait une Frame avec un boutton Button qui permet d'ouvrir une nouvelle Frame et j'ai ajouter l'interface WindowListener pour fermer les frames

Mon probleme c'est que je veux fermer la Frame interne sans fermer la Frame principale

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
 
//package inf2120.tp3;
*
*
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Frame;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.awt.*;
*
public class Principal extends Frame implements ActionListener, WindowListener {
*
****private static final long serialVersionUID = 1L;
*****
****public static final int FREQUENCE_ECHANTILLONAGE = 44100;
****public static final int NB_CANALS*************** = 1;
****public static final int OCTETS_PAR_ECHANTILLON** = 2;
****public static final int TAILLE_CADRE************ = NB_CANALS * OCTETS_PAR_ECHANTILLON;
****public static final int NB_OCTETS*************** = FREQUENCE_ECHANTILLONAGE * TAILLE_CADRE;
****public static final int AMPLITUDE_MAXIMUM******* = (int)( Math.pow( 2, OCTETS_PAR_ECHANTILLON * 8 - 1 ) - 1 );
*
****public static final int BORDURE_X = 14;
****public static final int BORDURE_Y = 40;
*****
****protected static Graphic dessin;
*****
****@Override
****public void windowActivated(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****@Override
****public void windowClosed(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****@Override
****public void windowClosing(WindowEvent arg0) {
********System.exit(0);
****}
*
****@Override
****public void windowDeactivated(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****@Override
****public void windowDeiconified(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****@Override
****public void windowIconified(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****@Override
****public void windowOpened(WindowEvent arg0) {
********// TODO Auto-generated method stub
*********
****}
*
****public static Onde nouvelleOnde( TypeOnde typeOnde, double frequence ) {
********Onde onde = null;
*********
********switch( typeOnde ) {
********case SIN :
************onde = new Sinusoidale( FREQUENCE_ECHANTILLONAGE, frequence );
************break;
********case CARRE :
************onde = new Carre( FREQUENCE_ECHANTILLONAGE, frequence );
************break;
********case SCIE_D :
************onde = new ScieD( FREQUENCE_ECHANTILLONAGE, frequence );
************break;
********case SCIE_M :
************onde = new ScieM( FREQUENCE_ECHANTILLONAGE, frequence );
************break;
********case TRI :
************onde = new Triangle( FREQUENCE_ECHANTILLONAGE, frequence );
************break;
********case BRUIT :
************onde = new Bruit();
************break;
********}
*********
********return onde;
****}
*****
****public static Onde construireOnde() {
********Onde ondeO;
*
********Onde onde1 = nouvelleOnde( typeOnde1, frequence1 );
*********
********if( utiliseOnde2 ) {
************Onde onde2 = nouvelleOnde( typeOnde2, frequence2 );
************ondeO = new Mixe( onde1, ratioVolume, onde2, 1.0 - ratioVolume );
********} else {
************ondeO = onde1;
********}
*
********Onde filtre = new ADSR( FREQUENCE_ECHANTILLONAGE,
********************attaque_filtre, declain_filtre, maintien_filtre, relache_filtre );
********Onde onde = new Filtre( filtre, ondeO );
*
********onde.setDure( dureNote );
*********
********return onde;
****}
*****
****public static void jouerNote( Onde onde )
****{
********AudioFormat format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
****************FREQUENCE_ECHANTILLONAGE, OCTETS_PAR_ECHANTILLON * 8, NB_CANALS,
****************OCTETS_PAR_ECHANTILLON * NB_CANALS, FREQUENCE_ECHANTILLONAGE, false);
*********
********SourceDataLine line;
********DataLine.Info info = new DataLine.Info(SourceDataLine.class,
************format);
********if (!AudioSystem.isLineSupported(info)) {
****************System.err.print( "Ligne non supporte par l'ordinateur." );
****************System.exit( -1 );
************}
********try {
************line = (SourceDataLine) AudioSystem.getLine(info);
************line.open(format);
*************
************byte[] buffer = new byte[NB_OCTETS];
*************
************line.start();
*
************int j = 0;
*
************for( Double a : onde )
************{
****************short amp = (short)Math.floor( AMPLITUDE_MAXIMUM * a );
****************buffer[j + 0] = (byte) (amp & 0xFF);
****************buffer[j + 1] = (byte) ((amp >>> 8) & 0xFF);
****************j += TAILLE_CADRE;
****************if( j >= NB_OCTETS )
****************{
********************line.write(buffer, 0, NB_OCTETS);
********************j = 0;
****************}
************}
*************
************if( j != 0 )
************{
****************line.write(buffer, 0, j);
************}
*************
************line.drain();
************line.stop();
************line.close();
********} catch (LineUnavailableException ex) {
************System.err.print( "Ligne non disponible." );
************System.exit( -1 );
********}
********line = null;
****}
*****
/** **************************************************
** Ici commence l'information importante pour le tp3 :
*************************************************** */
*****
****public enum TypeOnde {
********SIN, CARRE, SCIE_D, SCIE_M, TRI, BRUIT
****}
*****
****/**
****** dure de la note en seconde, entre 0 et 3.
******/
****public static double dureNote;
*****
****/**
****** Type de la premiere onde.
******/
****public static TypeOnde typeOnde1;
*****
****/**
****** Frequence de la premiere onde, entre 20 et 20000.
******/
****public static double frequence1;
*
****/**
****** a 'true' si nous voulons utiliser la deuxieme onde.***
******/
****public static boolean utiliseOnde2;
*
****/**
****** Type de la deuxieme onde.
******/
****public static TypeOnde typeOnde2;
*****
****/**
****** Frequence de la deuxieme onde, entre 20 et 20000.
******/
****public static double frequence2;
*****
****/**
****** Rapport de volume entre les deux ondes, entre 0 et 1.
******/
****public static double ratioVolume;
*****
****/**
****** Variable decrivant le filtre :
****** attaque_filtre : temps d'attaque, entre 0 et 1.
****** declain_filtre : temps du declain de la note, entre 0 et 1.
****** maintien_filtre : volume auquel la note est maintenue, entre 0 et 1.
****** relache_filtre : temps de relache de la note, entre 0 et 1.
******/
****public static double attaque_filtre;
****public static double declain_filtre;
****public static double maintien_filtre;
****public static double relache_filtre;
*****
****public Principal()
****{
*********
********super("Synthetiseur");
*********
********//boutton play
********play = new Button("Play");
********add(play,BorderLayout.SOUTH);
********addWindowListener(this);
********play.addActionListener(this);
*********
********//Label choix onde 1
********onde1 = new Label("Onde principal");
********onde1.setBounds(50,50,100,20);
********add(onde1);
*********
********dessin = new Graphic();
********creationMenu();
********fermer.addActionListener(this);
********/**
********** Valeurs de l'onde initiale.
**********/
********dureNote = 1.0;
********typeOnde1 = TypeOnde.SIN;
********frequence1 = 440.0;
********typeOnde2 = TypeOnde.CARRE;
********frequence2 = 220.0;
********ratioVolume = 0.67;
********utiliseOnde2 = true;
********attaque_filtre = 0.1;
********declain_filtre = 0.1;
********maintien_filtre = 0.5;
********relache_filtre = 0.1;
*
********/**
********** placez vos modifications ici :
********** ( vous decidez ou vous placer le add( dessin ).
**********/
*********
********/*add( dessin,BorderLayout.WEST );*/
****}
****Button play;
****Label onde1;
****Panel pane;
****@Override
****public void actionPerformed(ActionEvent e) {
********// TODO Auto-generated method stub
********if(e.getSource()==play){
************Onde onde = construireOnde();
************frameDessin dessin1 = new frameDessin();
************dessin1.addWindowListener(this);
************add( dessin );
************dessin.setFonction(onde);
************jouerNote( onde );
************
********}
********}
*****
*****
****/*
****** Pemet de creer un menu qui contient tous les fonctions du tp
******/
****public void creationMenu(){
********bar = new MenuBar();
********setMenuBar(bar);
********onde = new Menu("Onde");
********mixe=new Menu("Mixe");
********filtre= new Menu("Filtre");
********fermer=new Menu("fermer");
********bar.add(onde);
********bar.add(mixe);
********bar.add(filtre);
********bar.add(fermer);
********Sinusoidale = new MenuItem("Sinusoidale");
********Triangle = new MenuItem("Triangle");
********ScieD = new MenuItem("ScieD");
********ScieM = new MenuItem("ScieM");
********Carre = new MenuItem("Carre");
********Carre.addActionListener(this);
********Bruit = new MenuItem("Bruit");
********onde.add(Sinusoidale);
********onde.add(Triangle);
********onde.add(ScieD);
********onde.add(ScieM);
********onde.add(Carre);
********onde.add(Bruit);
****}
*****
****//classe interne fenetre pour dessiner l'onde
****public class frameDessin extends Frame implements WindowListener
*********
*
{
********public frameDessin(){
********setSize(400,400);
********setVisible(true);
*********
*********
********}
*
********@Override
********public void windowActivated(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*
********@Override
********public void windowClosed(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*
********@Override
********public void windowClosing(WindowEvent e) {
************// TODO Auto-generated method stub
************System.exit(0);
********}
*
********@Override
********public void windowDeactivated(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*
********@Override
********public void windowDeiconified(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*
********@Override
********public void windowIconified(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*
********@Override
********public void windowOpened(WindowEvent e) {
************// TODO Auto-generated method stub
*************
********}
*********
****}
*****
****//elements que j'ai ajouter
********MenuBar bar;
********Menu onde,mixe,filtre,fermer;
********MenuItem Sinusoidale,Triangle,ScieD,ScieM,Carre,Bruit;
*
****/**
****** @param args
******/
****public static void main(String[] args) {***
********//Frame
********Frame ecran = new Principal();
********ecran.setVisible(true);
********ecran.setSize(Graphic.TAILLE_X + BORDURE_X, 2 * Graphic.TAILLE_Y + BORDURE_Y );
********//ecran.setSize(600,600);
*}
}
*
*********
****