tailieunhanh - head first java second edition phần 7

Bây giờ bạn đã nhìn thấy rằng I / O inJava thực sự là khá đơn giản, đặc biệt là nếu bạn dính vào các kết nối / chuỗi kết hợp phổ biến nhất. Nhưng có một vấn đề bạn thực sự có thể quan tâm đến. (không phải thoáng qua) đã thay đổi từ một đôi một String. Đó là sự nhạy cảm an toàn của loại violatesJava trong một cách lớn. | controller events Version Two registering and getting ControllerEvents _ rzed Ù implerMrrrt the lister âỏe import javax. . public class MiniMualePlayer2 implements ControllerEvantLlstener public static void main String args MinlMusicPlayer2 mini new MinlMuslcPlayor2 0 0 public void go try Sequencer sequencer int evantsIWant 127 . r Í this events IWant Sequence seq new Sequence 4 Track track for int i 5 1 60 i 4 makaEvent 144 1 1 100 i makaEvent 176 1 127 0 1 1 makeEvant 128 1 i 100 1 2 end loop piỏk up -th k jl_. type is I event number 27 Th .71 ìịỉ put i L JuS Srtt a K Hayed seq 220 catch Exception ax close public void controlchange ShortMessage event la public MidiEvent makaEvant int comd int chan int one int two int tick MidiEvent event null try ShortMassage a new ShortMessage0 comd chan one two event new MidlEvent a tick Code that s different from the previous J catch Exception e version is highlighted in gray and we re return event not running it oil within main this time II close class 390 chapter 12 getting gui Version Three drawing graphics ỉn time with the music This final version builds on version two by adding the GUI parts. We build a frame add a drawing panel to it and each time we get an event we draw a new rectangle and repaint the screen. The only other change from version two is that the notes play randomly as opposed to simply moving up the scale. The most important change to the code besides building a simple GUI is that we make the drawing panel implement the ControllerEventListener rather than the program itself. So when the drawing panel an inner class gets the event it knows how to take care of itself by drawing the rectangle. Complete code for this version is on the next page.