SlideShare une entreprise Scribd logo
1  sur  10
Télécharger pour lire hors ligne
A-PDF Split DEMO : Purchase from www.A-PDF.com to remove the watermark




                  Appendix A:      *
           Final Code Kitchen

                          *

                                       Chris: groove2 revised
                                       NIgel: dance beat




FinaLLy, the complete version 01 the BeatBox!
It connects to a simple MusicServer so that you can
send and receive beat patterns with other clients.




                                                                this is a new appendix 649
final BeatBox code


Final BeatBox client program
Most of this code is the same as the code from the CodeKitchens in the previous
chapters, so we don't annotate the whole thing again. The new parts include:
GUI - two new components are added for the text area that displays incoming
messages (actually a scrolling list) and the text field .

NETWORKING - just like the SimpleChatClient in this chapter, the BeatBox now
connects to the server and gets an input and output stream.

THREADS - again, just like the SimpleChatClient, we start a 'reader' class that
keeps looking for incoming messages from the server. But instead of just text, the
messages coming in include TWO objects: the String message and the serialized
ArrayList (the thing that holds the state of all the checkboxes.)


import   java.awt.* ;
import   javax .swing .*;
import   java.io.*;
import   javax. sound. midi . *;
import   java.util .*;
import   java.awt.event.*;
import   java.net.*;
import   javax.swing .event.*;

public class BeatBoxFinal

        JFrame theFrame;
        JPanel mainPanel ;
        JList incomingList ;
        JTextField userMessage;
        ArrayList<JCheckBox> checkboxList;
        int nextNum;
        Vector<String> listVector = new Vector<String>() ;
        String userName;
        ObjectOUtputStream out;
        ObjectInputStream in;
        HashMap<String, boolean[]> otherSeqsMap   new HashMap<String, boolean[]>();

        Sequencer sequencer;
        Sequence sequence;
        Sequence mySequence = null;
        Track track;


        String[] instrumentNames = {~Bass Drum", ~Closed Hi-Hat", ~Open Hi-Hat","Acoustic
        Snare", ~Crash Cymbal", ~Hand Clap", ~High Tom", ~ H i Bongo", ~Maracas", ~Whistle",
        ~Low Conga", ~Cowbell", ~Vibraslap", ~Low-mid Tom", ~High Aqogo", ~Open Hi Conga");


        int[] instruments       = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67 ,63);


650    appendi x A
appendix A Fina l Code Kitchen

public static void main (String[] args) (
   new BeatBoxFinal() .startUp(args[O]); II args[O] is your user ID/screen name
                                    fI                                              l .f   J0~V- stv-eeYl Yla",e·
                                      ""----- AdO. a t..o",....aYlo-liYle av-~~",eYl1;. ov- I
public void startup(String name) {
    userName = name;
                                              c          yI ' 01 'ava 0eatBo'l-f=iYlal thef=lash
                                              ...'/.cl.... e· ID j    [}
    II open connection to the server
    try (
      Socket sock = new Socket("l27 .0 .0.l", 4242);
      out = new ObjectOutputStream(sock.getOUtputStream(»;
      in = new ObjectInputStream(sock.getInputStream(»;
      Thread remote = new Thread(new RemoteReader(»;
      remote . start 0 ;
      catch(Exception ex) (
          System.out.println("couldn't connect - you'll have to play alone .") ;
     }
     setupMidi 0 ;
     buildGUI () ;
  II close s ta rtUp

 public void buildGUI ()                                                      ~lA.1 tode, YlothiYl~ Yle-." hev-e

     theFrame = new JFrame("Cyber BeatBox");
     BorderLayout layout = new BorderLayout() ;
     JPanel background = new JPanel(layout) ;
     background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10»;

     checkboxList      = new   ArrayList<JCheckBox>() ;

     Box buttonBox = new Box(BoxLayout.Y_AXIS);
     JButton start = new JButton("Start");
     start.addActionListener(new MyStartListener(»;
     buttonBox.add(start);

     JButton stop = new JButton("Stop") ;
     stop.addActionListener(new MyStopListener(»;
     buttonBox.add(stop);

     JButton upTempo = new JButton("Tempo Up");
     upTempo.addActionListener(new MyUpTempoListener(»;
     buttonBox.add(upTempo);

     JButton downTempo = new JButton("Tempo Down") ;
     downTempo.addActionListener(new MyDownTempoListener(»;
     buttonBox.add(downTempo) ;

     JButton sendlt = new JButton("sendIt");
     sendlt.addActionListener(new MySendListener(»;
     buttonBox.add(sendIt);

     userMessage      = new   JTextField();



                                                                                               you are here ~      651
final BeatBox code

           buttonBox.add(userMessage):




           Box namaBox ::; new Box (BoxLayout. 'i_AXIS) i
           for (int i = 0; i < 16; i++) {
               nameBox. aeld(naw Label (i.nst%UmentNames [i]» ;


           background.. add (Bord.erLayout . EAST , buttonBox) i
           background.. add (Bord.erLayou t. WEST, nameBox);

           theFrame. qetContentpane () . add. (background) ;
           GridLayout grid'" new GridLayout(16,16);
           grid. aetVgap (1) ;
           grid.setHqap(2):
           mainPanel '" new .Ji'anel (grid) :
           background. add (BorderLayout. CENTER, UlA1nPanel);

           for (int i '" 0; i < 256; i++) (
                JCheckBox c '" new JCheck.Box () ;
               c .setse1ected(false);
               checkboxList.add.(c) ;
               DIlU.nPanel. add. (c) ;
             II end loop
           theFrAmQ.aetBounds(50,50,300,300);
           theFrAmQ .pack () ;
           theFrame.setVisible(true);
           II close buildGUI

         public void. setUpMidi ()
            try (
            sequencer  IS MidiSystem .getSequencer () i

            sequencer.open();
            sequence IS new sequence (Saquence.PPQ,4) ;
            track '" sequance.areateTrack();
            sequencer.setTempolnBPM(120);
          catch (Exception e) (e .printstackTrace () ;)

      II close setUpMldi




652   appendix. A
appendix A Final Code Kitche n




public void buildTrackAndStart() {
   ArrayList<Integer> trackList = null ; II this will hold the instruments for each
   sequence .deleteTrack(track);                                            h the t..het.k'ooi-tS
   track = sequence.createTrack();                     '0 'fIoki'~ thY"o~ t t to a'
                                                      ?J",M 0 kO,t.kSJtel oYlO .,..aYYMi~~ ,chl/~Ylt koY"
                                                                l
                                                                                                             .o.    L
   for (int i       = 0;   i < 16; i++) {             to «jtt "flt C'0 ",okiYl~ the      . 1~1r;,. ACTL.:'! oSi"
                                                      i'stv-",,,,e't ~. • oe1'o, b",t It IS ~)< L oY"el/iol.lS
                                                               . et,,1 ..0"',                   Y"eteY" "IP ,
        trackList     = new   ArrayList<Integer>();
                                                                                      L
                                                       Toils 'S yY" ' r . t.hay"eY"s, so               L'
                                                              . the yyel/lOI.lS L LL f' "' ei-yaYlo"IOYl
                                                                                                               a~aiYl.
                                                        'fIoS 1' •        to ~e" "fie -t
        for (int j = 0 ; j < 16; j++) {                  Code~ltt..he'lS
           JCheckBox jc = (JCheckBox) checkboxList.get(j + (16*i»;
           if (jc.isSelected(» (
              int key = instruments[i];
              trackList.add(new Integer(key»;
             else {
             trackList.add(null); II because this slot should be empty in the track
           }
        } II   c lose inner l oo p
        makeTracks{trackList);
   } II   c los e o ute r l oop
   track.add(makeEvent{192,9,1,0,15»; II - so we always go to full 16 beats
   try {
      sequencer.setSequence(sequence);
      sequencer . setLoopCount (sequencer. LOOP_CONTINUOUSLY) ;
      sequencer .start();
      sequencer.setTempolnBPM{120);
  } catch(Exception e) {e.printStackTrace();}
 } II   close me tho d

public class MyStartListener implements ActionListener
  public void actionPerformed(ActionEvent a) {
      buildTrackAndStart() ;
    } II c lose actionPe r f o rmed
  II close inner cl as s

public class MyStopListener implements ActionListener
  public void actionPerformed(ActionEvent a) (
      sequencer . stop () ;
  } II c l o se a c t ionPerformed
  II close inne r c lass

public class MyUpTempoListener implements ActionListener
   public void actionPerformed(ActionEvent a) {
      float tempoFactor = sequencer.getTempoFactor();
      sequencer.setTempoFactor«float) (tempoFactor * 1.03»;
  } II close act i o nPer f o rmed
 II c l o se inner c l ass



                                                                                              you are here .             653
final BeatBox code


       public class MyDownTempoListener ~lements ActionListener
           public void actionPerformed(ActionEvent a) {
             fioat tempoFactor = sequencer .getTempoFactor();
             sequencer.setTempoFactor«fioat) (tempoFactor * .97»;



      public class MySendListener implements ActionListener (
        public void actionPerformed(ActionEvent a) {
             II make an arraylist of just the STATE of the checkboxes
              boolean[] checkboxState = new boolean[256] ;
             for (int i = 0; i < 256; i++) {
                JCheckBox check = (JCheckBox)
                if (check.isSelected(» {
                  checkboxState[i] = true;
                   }
                 } II close l oop
             String messageToSend = null;
             try {
                out.writeObject(userName + nextNum++ + ". " + userMessage.getText(» ;
                out .writeObject(checkboxState);
               catch(Exception ex) {
                 System.out.println("Sorry dude. Could not send it to the server.") ;
             }
             userMessage.setText(" ");
          } I I c l ose action Pe r f o r med
         II c l os e i nne r c l a s s

      public class MyListSelectionListener implements ListSelectionListener
          public void valueChanged(ListSelectionEvent le) {
            if (!le .getValueIsAdjusting(» {
              String selected = (String) incomingList .getselectedValue();
              if (selected != null) (
                  II now go to the map, and change the sequence
                   boolean[] selectedState = (boolean[]) otherSeqsMap.get(selected);
                   changeSequence(selectedState);
                   sequencer.stop();
                   buildTrackAndStart();

             }
           II close valueCha nged
      I I c l os e i nne r class




654     append ix A
appendix A Final Code Kitchen
 public class RemoteReader implements Runnable {
     boolean[] checkboxState = null;
     String nameToShow = null ;
    Object obj = null;
     public void rune) {
              try {
                      while«obj=in.readObject(» != nul l ) {
                        System.out.println("got an object from
                        System.out.println(obj .getClass(»;
                        String nameToShow = (String) obj ;
                                                                                W~~II a ~esS4
                        checkboxState = (boolean[]) in.readObject();
                        otherSeqsMap.put(nameToShow, checkboxState) ;          (deseriaJi~) {h~
                                                                               ~~ssage alld.L
                                                                                                      t:ob    ill, lIIe read
                        listVector .add(nameToShow) ;         t hetkb.   ,;he Arra
                                                                                                 e
                                                                                               rs the
                                                             the JL~~ stau vallAesf~ Ids+, 0+ Boo/eall
                        incomingList.setListData(listVector) ;
                                                                                                       1110
                                                                                                              c
                                                                                                              :ie t L   (


            } I I close wh i l e                            .
                                                                       U Ohe n,;. Add'11tidd I,; 1;0
                                                                  J C to~p
                                                           IS a 1;111
                                                                                   .L I'l          ·.L J
           catch(Exception ex) {ex.printStackTrace();}     0+ the!:;ts p thil'l.9: 'lOlA k 1 9 to a JList
        I I close run                                     fashioned A data !Vetto :~p a Vettor
   II close inner c lass                                 JList to rrayLisi) a ; an oJd_                        't.
                                                        .for wh t lAse thai. Ve~k hen t~1J t h
 public class MyPlayMineListener implements ActionListener { a to display' .L as it's SOIA e
    public void actionPerformed(ActionEvent a) {                                III ,;h~ lisi..       rt~
              i f (mySequence != null) {
                  sequence = mySequence ;      II restore to my original
              }
    } II cl ose ac tionPerformed
   II close inner class                                     This Ift~thod is tailed h t
                                                                                         i
                                                            sOlftethin~ .ft-Olft th I' ~ he lASer seletts
public void changeSequence(boolean[]              checkboxSta~,n~ the patter: ;;.fh e /NI'j1hf;[)/ATE.LY
   for (int i = 0; i < 256; i++) {                                                           e one .. ey selet ted.
       JCheckBox check = (JCheckBox) checkboxList.get(i) ;
        i f (checkboxState[i]) {
             check .setSelected(true) ;
           else {
              check .setSelected(false) ;
                                                                   All the MIDI sh H is ~uttl~ the SaM as it
                  }
        II c l ose l oop                                                                              e
  I I close change Sequence                                        was in the yYe'JiolAS 'Jet-Slon.

public void makeTracks(ArrayList list)
   Iterator it = list . iterator();
   for (int i = 0; i < 16 ; i++) {
      Integer num = (Integer) it.next();
      if (num != null) {
         int numKey = num.intValue() ;
         track.add(makeEvent(144 ,9 ,numKey, 100, i» ;
         track .add(makeEvent(128,9 ,numKey,100, i + 1»;
                  }
           } I I clo se loop
        I I close makeTrac ks()




                                                                                                  you are he re.               655
   ._     -       - - - - - - - -
                  -
final BeatBox code

         public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
            MidiEvent event = null;
             try {
               ShortMessage a = new ShortMessage();
               a. setMessage (comd, chan, one, two);
               event = new MidiEvent(a, tick);
             }catch (Exception e} { }                               i~e the last ~e'f"si()'l·
             return event;                      Not'''~ "eVl· J~t
        } II close rnakeEvent

} II c l os e c l a s s




          ~yourpenCii
                          What are some of the ways you can improve this program?
                          Here are a few ideas to get you started :
                          1) Once you select a pattern, whatever current pattern was playing is blown
                          away. If that was a new pattern you were working on (or a modification of
                          another one), you're out of luck.You might want to pop up a dialog box that
                          asksthe user if he'd like to save the current pattern.


                          2) If you fail to type in a command-line argument, you just get an exception
                          when you run it! Put something in the main method that checks to see if
                          you've passed in a command-line argument. If the user doesn't supply one,
                          either pick a default or print out a message that saysthey need to run it
                          again, but this time with an argument for their screen name.


                          3) It might be nice to have a feature where you can click a button and it
                          will generate a random pattern for you.You might hit on one you really like.
                          Better yet, have another feature that lets you load in existing 'foundation'
                          patterns, like one for jazz, rock, reggae, etc. that the user can add to.
                          You can find existing patterns on the Head First Java web start.




656     appen dix A
appendix A Final Code Kitchen

Final BeatBox server program
Most of this code is identical to the SimpleChatServer we made in the
Networking and Threads chapter. The only difference , in fact, is that this server
receives, and then re-sends, two serialized objects instead of a plain String
(although one of the serialized objects happens to be a String).




     import java.io.*;
     import java.net.*;
     import java.util.*;

     public class MusicServer

          ArrayList<ObjectOutputStream> clientOutputStreams;

          public static void main (String[] args) {
               new MusicServer() .go();


          public class ClientHandler implements Runnable {

                 ObjectlnputStream in;
                 Socket clientSocket;

                 public ClientHandler(Socket socket) (
                   try {
                     clientSocket = socket;
                      in = new ObjectlnputStream(clientSocket .getlnputStream(»;

                       catch(Exception ex) (ex.printStackTrace();}
                   } II clos e const ruc t or


                public void run {} {
                     Object 02 = null ;
                     Object 01 = null;
                   try {

                       while ((01 = in.readObject(»              != null) (

                           02   = in .readObject();
                           System .out .println("read two objects") ;
                           tellEveryone(01, 02);
                           I I close wh ile

                       catch(Exception ex) (ex.printStackTrace();}
                 I I cl os e run
           II c lo s e inn e r c lass


                                                                                               you are here ~   657
final BeatBox code


public void qo() {
  clientOutputStreams          = new   ArrayList<ObjectOutputStream>();

          try {
            ServerSocket serverSock       = new   ServerSocket(4242);

           while (true) {
              Socket clientSocket = serverSock.accept();
             ObjectOutputStream out = new ObjectOutputStream(clientSocket.qetOutputStream(»
             clientOutputStreams.add(out);

                Thread t = new Thread(new ClientHandler(clientSocket»;
                t. start() ;

                System.out.println("qot a connection");
            }
      }catch(Exception ex) {
        ex.printStackTrace();
      }
   II c lose go

public void tellEveryone(Object one , Object two)
   Iterator it = clientOutputStreams.iterator();
  while(it .hasNext(» {
     try {
        ObjectOutputStream out = (ObjectOutputStream) it .next();
        out.writeObject(one) ;
        out.writeObject(two) ;
      }catch(Exception ex) {ex.printStackTrace() ;}

   II close t e l l Everyone

} I I close class




658   appendix A

Contenu connexe

Tendances

Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersIan Barber
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidSomenath Mukhopadhyay
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinAhmad Arif Faizin
 
The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88Mahmoud Samir Fayed
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.UA Mobile
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲームNoritada Shimizu
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new featuresGephenSG
 
The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184Mahmoud Samir Fayed
 
Non stop random2b
Non stop random2bNon stop random2b
Non stop random2bphanhung20
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutineDaehee Kim
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxEleanor McHugh
 
The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88Mahmoud Samir Fayed
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]Baruch Sadogursky
 
Parse Everything With Elixir
Parse Everything With ElixirParse Everything With Elixir
Parse Everything With ElixirGabriele Lana
 

Tendances (20)

Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in Android
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
 
The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88The Ring programming language version 1.3 book - Part 17 of 88
The Ring programming language version 1.3 book - Part 17 of 88
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
 
smtlecture.9
smtlecture.9smtlecture.9
smtlecture.9
 
The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184The Ring programming language version 1.5.3 book - Part 46 of 184
The Ring programming language version 1.5.3 book - Part 46 of 184
 
Non stop random2b
Non stop random2bNon stop random2b
Non stop random2b
 
Developing iOS apps with Swift
Developing iOS apps with SwiftDeveloping iOS apps with Swift
Developing iOS apps with Swift
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into Coroutine
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88The Ring programming language version 1.3 book - Part 36 of 88
The Ring programming language version 1.3 book - Part 36 of 88
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]
 
Parse Everything With Elixir
Parse Everything With ElixirParse Everything With Elixir
Parse Everything With Elixir
 
The Magic Of Elixir
The Magic Of ElixirThe Magic Of Elixir
The Magic Of Elixir
 
Ecma script 5
Ecma script 5Ecma script 5
Ecma script 5
 

Similaire à Pdr ppt

JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfkokah57440
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxDIPESH30
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdfinfo30292
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Kuldeep Jain
 
Fee managment system
Fee managment systemFee managment system
Fee managment systemfairy9912
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 

Similaire à Pdr ppt (20)

Awt
AwtAwt
Awt
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
662305 11
662305 11662305 11
662305 11
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
 
Java File
Java FileJava File
Java File
 
C program
C programC program
C program
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Java practical
Java practicalJava practical
Java practical
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Utility.ppt
Utility.pptUtility.ppt
Utility.ppt
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
Fee managment system
Fee managment systemFee managment system
Fee managment system
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Notepad
NotepadNotepad
Notepad
 

Dernier

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 

Dernier (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Pdr ppt

  • 1. A-PDF Split DEMO : Purchase from www.A-PDF.com to remove the watermark Appendix A: * Final Code Kitchen * Chris: groove2 revised NIgel: dance beat FinaLLy, the complete version 01 the BeatBox! It connects to a simple MusicServer so that you can send and receive beat patterns with other clients. this is a new appendix 649
  • 2. final BeatBox code Final BeatBox client program Most of this code is the same as the code from the CodeKitchens in the previous chapters, so we don't annotate the whole thing again. The new parts include: GUI - two new components are added for the text area that displays incoming messages (actually a scrolling list) and the text field . NETWORKING - just like the SimpleChatClient in this chapter, the BeatBox now connects to the server and gets an input and output stream. THREADS - again, just like the SimpleChatClient, we start a 'reader' class that keeps looking for incoming messages from the server. But instead of just text, the messages coming in include TWO objects: the String message and the serialized ArrayList (the thing that holds the state of all the checkboxes.) import java.awt.* ; import javax .swing .*; import java.io.*; import javax. sound. midi . *; import java.util .*; import java.awt.event.*; import java.net.*; import javax.swing .event.*; public class BeatBoxFinal JFrame theFrame; JPanel mainPanel ; JList incomingList ; JTextField userMessage; ArrayList<JCheckBox> checkboxList; int nextNum; Vector<String> listVector = new Vector<String>() ; String userName; ObjectOUtputStream out; ObjectInputStream in; HashMap<String, boolean[]> otherSeqsMap new HashMap<String, boolean[]>(); Sequencer sequencer; Sequence sequence; Sequence mySequence = null; Track track; String[] instrumentNames = {~Bass Drum", ~Closed Hi-Hat", ~Open Hi-Hat","Acoustic Snare", ~Crash Cymbal", ~Hand Clap", ~High Tom", ~ H i Bongo", ~Maracas", ~Whistle", ~Low Conga", ~Cowbell", ~Vibraslap", ~Low-mid Tom", ~High Aqogo", ~Open Hi Conga"); int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67 ,63); 650 appendi x A
  • 3. appendix A Fina l Code Kitchen public static void main (String[] args) ( new BeatBoxFinal() .startUp(args[O]); II args[O] is your user ID/screen name fI l .f J0~V- stv-eeYl Yla",e· ""----- AdO. a t..o",....aYlo-liYle av-~~",eYl1;. ov- I public void startup(String name) { userName = name; c yI ' 01 'ava 0eatBo'l-f=iYlal thef=lash ...'/.cl.... e· ID j [} II open connection to the server try ( Socket sock = new Socket("l27 .0 .0.l", 4242); out = new ObjectOutputStream(sock.getOUtputStream(»; in = new ObjectInputStream(sock.getInputStream(»; Thread remote = new Thread(new RemoteReader(»; remote . start 0 ; catch(Exception ex) ( System.out.println("couldn't connect - you'll have to play alone .") ; } setupMidi 0 ; buildGUI () ; II close s ta rtUp public void buildGUI () ~lA.1 tode, YlothiYl~ Yle-." hev-e theFrame = new JFrame("Cyber BeatBox"); BorderLayout layout = new BorderLayout() ; JPanel background = new JPanel(layout) ; background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10»; checkboxList = new ArrayList<JCheckBox>() ; Box buttonBox = new Box(BoxLayout.Y_AXIS); JButton start = new JButton("Start"); start.addActionListener(new MyStartListener(»; buttonBox.add(start); JButton stop = new JButton("Stop") ; stop.addActionListener(new MyStopListener(»; buttonBox.add(stop); JButton upTempo = new JButton("Tempo Up"); upTempo.addActionListener(new MyUpTempoListener(»; buttonBox.add(upTempo); JButton downTempo = new JButton("Tempo Down") ; downTempo.addActionListener(new MyDownTempoListener(»; buttonBox.add(downTempo) ; JButton sendlt = new JButton("sendIt"); sendlt.addActionListener(new MySendListener(»; buttonBox.add(sendIt); userMessage = new JTextField(); you are here ~ 651
  • 4. final BeatBox code buttonBox.add(userMessage): Box namaBox ::; new Box (BoxLayout. 'i_AXIS) i for (int i = 0; i < 16; i++) { nameBox. aeld(naw Label (i.nst%UmentNames [i]» ; background.. add (Bord.erLayout . EAST , buttonBox) i background.. add (Bord.erLayou t. WEST, nameBox); theFrame. qetContentpane () . add. (background) ; GridLayout grid'" new GridLayout(16,16); grid. aetVgap (1) ; grid.setHqap(2): mainPanel '" new .Ji'anel (grid) : background. add (BorderLayout. CENTER, UlA1nPanel); for (int i '" 0; i < 256; i++) ( JCheckBox c '" new JCheck.Box () ; c .setse1ected(false); checkboxList.add.(c) ; DIlU.nPanel. add. (c) ; II end loop theFrAmQ.aetBounds(50,50,300,300); theFrAmQ .pack () ; theFrame.setVisible(true); II close buildGUI public void. setUpMidi () try ( sequencer IS MidiSystem .getSequencer () i sequencer.open(); sequence IS new sequence (Saquence.PPQ,4) ; track '" sequance.areateTrack(); sequencer.setTempolnBPM(120); catch (Exception e) (e .printstackTrace () ;) II close setUpMldi 652 appendix. A
  • 5. appendix A Final Code Kitche n public void buildTrackAndStart() { ArrayList<Integer> trackList = null ; II this will hold the instruments for each sequence .deleteTrack(track); h the t..het.k'ooi-tS track = sequence.createTrack(); '0 'fIoki'~ thY"o~ t t to a' ?J",M 0 kO,t.kSJtel oYlO .,..aYYMi~~ ,chl/~Ylt koY" l .o. L for (int i = 0; i < 16; i++) { to «jtt "flt C'0 ",okiYl~ the . 1~1r;,. ACTL.:'! oSi" i'stv-",,,,e't ~. • oe1'o, b",t It IS ~)< L oY"el/iol.lS . et,,1 ..0"', Y"eteY" "IP , trackList = new ArrayList<Integer>(); L Toils 'S yY" ' r . t.hay"eY"s, so L' . the yyel/lOI.lS L LL f' "' ei-yaYlo"IOYl a~aiYl. 'fIoS 1' • to ~e" "fie -t for (int j = 0 ; j < 16; j++) { Code~ltt..he'lS JCheckBox jc = (JCheckBox) checkboxList.get(j + (16*i»; if (jc.isSelected(» ( int key = instruments[i]; trackList.add(new Integer(key»; else { trackList.add(null); II because this slot should be empty in the track } } II c lose inner l oo p makeTracks{trackList); } II c los e o ute r l oop track.add(makeEvent{192,9,1,0,15»; II - so we always go to full 16 beats try { sequencer.setSequence(sequence); sequencer . setLoopCount (sequencer. LOOP_CONTINUOUSLY) ; sequencer .start(); sequencer.setTempolnBPM{120); } catch(Exception e) {e.printStackTrace();} } II close me tho d public class MyStartListener implements ActionListener public void actionPerformed(ActionEvent a) { buildTrackAndStart() ; } II c lose actionPe r f o rmed II close inner cl as s public class MyStopListener implements ActionListener public void actionPerformed(ActionEvent a) ( sequencer . stop () ; } II c l o se a c t ionPerformed II close inne r c lass public class MyUpTempoListener implements ActionListener public void actionPerformed(ActionEvent a) { float tempoFactor = sequencer.getTempoFactor(); sequencer.setTempoFactor«float) (tempoFactor * 1.03»; } II close act i o nPer f o rmed II c l o se inner c l ass you are here . 653
  • 6. final BeatBox code public class MyDownTempoListener ~lements ActionListener public void actionPerformed(ActionEvent a) { fioat tempoFactor = sequencer .getTempoFactor(); sequencer.setTempoFactor«fioat) (tempoFactor * .97»; public class MySendListener implements ActionListener ( public void actionPerformed(ActionEvent a) { II make an arraylist of just the STATE of the checkboxes boolean[] checkboxState = new boolean[256] ; for (int i = 0; i < 256; i++) { JCheckBox check = (JCheckBox) if (check.isSelected(» { checkboxState[i] = true; } } II close l oop String messageToSend = null; try { out.writeObject(userName + nextNum++ + ". " + userMessage.getText(» ; out .writeObject(checkboxState); catch(Exception ex) { System.out.println("Sorry dude. Could not send it to the server.") ; } userMessage.setText(" "); } I I c l ose action Pe r f o r med II c l os e i nne r c l a s s public class MyListSelectionListener implements ListSelectionListener public void valueChanged(ListSelectionEvent le) { if (!le .getValueIsAdjusting(» { String selected = (String) incomingList .getselectedValue(); if (selected != null) ( II now go to the map, and change the sequence boolean[] selectedState = (boolean[]) otherSeqsMap.get(selected); changeSequence(selectedState); sequencer.stop(); buildTrackAndStart(); } II close valueCha nged I I c l os e i nne r class 654 append ix A
  • 7. appendix A Final Code Kitchen public class RemoteReader implements Runnable { boolean[] checkboxState = null; String nameToShow = null ; Object obj = null; public void rune) { try { while«obj=in.readObject(» != nul l ) { System.out.println("got an object from System.out.println(obj .getClass(»; String nameToShow = (String) obj ; W~~II a ~esS4 checkboxState = (boolean[]) in.readObject(); otherSeqsMap.put(nameToShow, checkboxState) ; (deseriaJi~) {h~ ~~ssage alld.L t:ob ill, lIIe read listVector .add(nameToShow) ; t hetkb. ,;he Arra e rs the the JL~~ stau vallAesf~ Ids+, 0+ Boo/eall incomingList.setListData(listVector) ; 1110 c :ie t L ( } I I close wh i l e . U Ohe n,;. Add'11tidd I,; 1;0 J C to~p IS a 1;111 .L I'l ·.L J catch(Exception ex) {ex.printStackTrace();} 0+ the!:;ts p thil'l.9: 'lOlA k 1 9 to a JList I I close run fashioned A data !Vetto :~p a Vettor II close inner c lass JList to rrayLisi) a ; an oJd_ 't. .for wh t lAse thai. Ve~k hen t~1J t h public class MyPlayMineListener implements ActionListener { a to display' .L as it's SOIA e public void actionPerformed(ActionEvent a) { III ,;h~ lisi.. rt~ i f (mySequence != null) { sequence = mySequence ; II restore to my original } } II cl ose ac tionPerformed II close inner class This Ift~thod is tailed h t i sOlftethin~ .ft-Olft th I' ~ he lASer seletts public void changeSequence(boolean[] checkboxSta~,n~ the patter: ;;.fh e /NI'j1hf;[)/ATE.LY for (int i = 0; i < 256; i++) { e one .. ey selet ted. JCheckBox check = (JCheckBox) checkboxList.get(i) ; i f (checkboxState[i]) { check .setSelected(true) ; else { check .setSelected(false) ; All the MIDI sh H is ~uttl~ the SaM as it } II c l ose l oop e I I close change Sequence was in the yYe'JiolAS 'Jet-Slon. public void makeTracks(ArrayList list) Iterator it = list . iterator(); for (int i = 0; i < 16 ; i++) { Integer num = (Integer) it.next(); if (num != null) { int numKey = num.intValue() ; track.add(makeEvent(144 ,9 ,numKey, 100, i» ; track .add(makeEvent(128,9 ,numKey,100, i + 1»; } } I I clo se loop I I close makeTrac ks() you are he re. 655 ._ - - - - - - - - - -
  • 8. final BeatBox code public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) { MidiEvent event = null; try { ShortMessage a = new ShortMessage(); a. setMessage (comd, chan, one, two); event = new MidiEvent(a, tick); }catch (Exception e} { } i~e the last ~e'f"si()'l· return event; Not'''~ "eVl· J~t } II close rnakeEvent } II c l os e c l a s s ~yourpenCii What are some of the ways you can improve this program? Here are a few ideas to get you started : 1) Once you select a pattern, whatever current pattern was playing is blown away. If that was a new pattern you were working on (or a modification of another one), you're out of luck.You might want to pop up a dialog box that asksthe user if he'd like to save the current pattern. 2) If you fail to type in a command-line argument, you just get an exception when you run it! Put something in the main method that checks to see if you've passed in a command-line argument. If the user doesn't supply one, either pick a default or print out a message that saysthey need to run it again, but this time with an argument for their screen name. 3) It might be nice to have a feature where you can click a button and it will generate a random pattern for you.You might hit on one you really like. Better yet, have another feature that lets you load in existing 'foundation' patterns, like one for jazz, rock, reggae, etc. that the user can add to. You can find existing patterns on the Head First Java web start. 656 appen dix A
  • 9. appendix A Final Code Kitchen Final BeatBox server program Most of this code is identical to the SimpleChatServer we made in the Networking and Threads chapter. The only difference , in fact, is that this server receives, and then re-sends, two serialized objects instead of a plain String (although one of the serialized objects happens to be a String). import java.io.*; import java.net.*; import java.util.*; public class MusicServer ArrayList<ObjectOutputStream> clientOutputStreams; public static void main (String[] args) { new MusicServer() .go(); public class ClientHandler implements Runnable { ObjectlnputStream in; Socket clientSocket; public ClientHandler(Socket socket) ( try { clientSocket = socket; in = new ObjectlnputStream(clientSocket .getlnputStream(»; catch(Exception ex) (ex.printStackTrace();} } II clos e const ruc t or public void run {} { Object 02 = null ; Object 01 = null; try { while ((01 = in.readObject(» != null) ( 02 = in .readObject(); System .out .println("read two objects") ; tellEveryone(01, 02); I I close wh ile catch(Exception ex) (ex.printStackTrace();} I I cl os e run II c lo s e inn e r c lass you are here ~ 657
  • 10. final BeatBox code public void qo() { clientOutputStreams = new ArrayList<ObjectOutputStream>(); try { ServerSocket serverSock = new ServerSocket(4242); while (true) { Socket clientSocket = serverSock.accept(); ObjectOutputStream out = new ObjectOutputStream(clientSocket.qetOutputStream(» clientOutputStreams.add(out); Thread t = new Thread(new ClientHandler(clientSocket»; t. start() ; System.out.println("qot a connection"); } }catch(Exception ex) { ex.printStackTrace(); } II c lose go public void tellEveryone(Object one , Object two) Iterator it = clientOutputStreams.iterator(); while(it .hasNext(» { try { ObjectOutputStream out = (ObjectOutputStream) it .next(); out.writeObject(one) ; out.writeObject(two) ; }catch(Exception ex) {ex.printStackTrace() ;} II close t e l l Everyone } I I close class 658 appendix A