26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22 <strong>Java</strong> Media Framework and <strong>Java</strong> Sound (on CD) 1301<br />

53<br />

54 // construc<strong>to</strong>r for MidiDemo<br />

55 public MidiDemo()<br />

56 {<br />

57 super( "MIDI Demo" );<br />

58<br />

59 container = getContentPane();<br />

60 container.setLayout( new BorderLayout() );<br />

61<br />

62 // synthesizer must be instantiated <strong>to</strong> enable synthesis<br />

63 midiSynthesizer = new MidiSynthesizer();<br />

64<br />

65 // make piano keys<br />

66 makeKeys();<br />

67<br />

68 // add control panel <strong>to</strong> frame<br />

69 controlPanel = new JPanel( new BorderLayout() );<br />

70 container.add( controlPanel, BorderLayout.NORTH );<br />

71<br />

72 makeConfigureControls();<br />

73<br />

74 // add but<strong>to</strong>n panel <strong>to</strong> frame<br />

75 but<strong>to</strong>nPanel = new JPanel( new GridLayout( 5, 1 ) );<br />

76 controlPanel.add( but<strong>to</strong>nPanel, BorderLayout.EAST );<br />

77<br />

78 // make GUI<br />

79 makePlaySaveBut<strong>to</strong>ns();<br />

80 makeRecordBut<strong>to</strong>n();<br />

81 makePianoPlayerBut<strong>to</strong>n();<br />

82<br />

83 } // end construc<strong>to</strong>r<br />

84<br />

85 // utility method making piano keys<br />

86 private void makeKeys()<br />

87 {<br />

88 // panel containing keys<br />

89 JPanel keyPanel = new JPanel( null );<br />

90 container.add( keyPanel, BorderLayout.CENTER );<br />

91<br />

92 // piano keys<br />

93 noteBut<strong>to</strong>n = new JBut<strong>to</strong>n[ MAX_KEYS ];<br />

94<br />

95 // add MAX_KEYS but<strong>to</strong>ns and what note they sound<br />

96 for ( int i = 0; i < MAX_KEYS; i++ ) {<br />

97<br />

98 final int note = i;<br />

99<br />

100 noteBut<strong>to</strong>n[ i ] = new JBut<strong>to</strong>n();<br />

101<br />

102 // setting white keys<br />

103 noteBut<strong>to</strong>n[ i ].setBackground( Color.white );<br />

104<br />

Fig. Fig. 22.10 22.10 22.10 MidiDemo provides the GUI than enables users <strong>to</strong> interact with the<br />

application (part 2 of 14).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!