package AppendixA;
// chapter 14
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import javax.sound.midi.*;
import java.util.*;
import java.awt.event.*;
import java.net.*;
public class BeatBoxFinal { // implements MetaEventListener
JPanel mainPanel;
JList incomingList;
JTextField userMessage;
ArrayList<JCheckBox> checkboxList;
int nextNum;
ObjectInputStream in;
ObjectOutputStream out;
Vector<String> listVector = new Vector<String>();
String userName ;
HashMap<String, boolean[]> otherSeqsMap = new HashMap<String, boolean[]>();
Sequencer sequencer;
Sequence sequence;
Sequence mySequence = null;
Track track;
JFrame theFrame;
String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat",
"Open Hi-Hat","Acoustic Snare", "Crash Cymbal", "Hand Clap",
"High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga",
"Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo",
"Open Hi Conga"};
int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
public static void main (String[] args) {
new BeatBoxFinal().startUp(args[0]);
}
public void startUp(String name) {
userName = name;
try {
Socket sock = new Socket("127.0.0.1", 4242);
out = new ObjectOutputStream(sock.getOutputStream());
in = new ObjectInputStream(sock.getInputStream());
Thread remote = new Thread(new RemoteReader());
remote.start();
}
catch (Exception ex) {
System.out.println("couldn't connect - you'll have to play alone.");
}
setUpMidi();
buildGUI();
}
public void buildGUI() {
theFrame = new JFrame("Cyber BeatBox");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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 sendIt = new JButton("sendIt");
sendIt.addActionListener(new MySendListener());
buttonBox.add(sendIt);
JButton saveIt = new JButton("Serialize It"); // new button
saveIt.addActionListener(new MySendListener());
buttonBox.add(saveIt);
userMessage = new JTextField();
buttonBox.add(userMessage);
incomingList = new JList();
incomingList.addListSelectionListener(new MyListSelectionListener());
incomingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane theList = new JScrollPane(incomingList);
buttonBox.add(theList);
incomingList.setListData(listVector);
Box nameBox = new Box(BoxLayout.Y_AXIS);
for (int i = 0; i < 16; i++) {
nameBox.add(new Label(instrumentNames[i]));
}
background.add(BorderLayout.EAST, buttonBox);
background.add(BorderLayout.WEST, nameBox);
theFrame.getContentPane().add(background);
GridLayout grid = new GridLayout(16,16);
grid.setVgap(1);
grid.setHgap(2);
mainPanel = new JPanel(grid);
background.add(BorderLayout.CENTER, mainPanel);
for (int i = 0; i < 256; i++) {
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
} // end loop
theFrame.setBounds(50,50,300,300);
theFrame.pack();
theFrame.setVisible(true);
} // close method
public void setUpMidi() {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
// sequencer.addMetaEventListener(this);
sequence = new Sequence(Sequence.PPQ,4);
track = sequence.createTrack();
sequencer.setTempoInBPM(120);
} catch(Exception e) {e.printStackTrace();}
} // close method
public void buildTrackAndStart()
{
// this will hold the instruments for each vertical column,
// in other words, each tick (may have multiple instruments)
ArrayList<Integer> trackList = null;
sequence.deleteTrack(track);
track = sequence.createTrack();
for (int i = 0; i < 16; i++){
trackList = new ArrayList<Integer>();
for (int j = 0; j < 16; j++){
JCheckBox jc = (JCheckBox) checkboxList.get(j + (16 * i));
if (jc.isSelected()){
int key = instruments[i];
trackList.add(key);
}
else
{
trackList.add(null);
}
} // close inner
makeTracks(trackList);
} // close outer
track.add(makeEvent(192,9,1,0,15)); // - so we always go to full 16 beats
try {
sequencer.setSequence(sequence);
sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
sequencer.start();
sequencer.setTempoInBPM(120);
} catch(Exception e) {e.printStackTrace();}
} // close method
//============================================================== inner class listeners
public class MyStartListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
buildTrackAndStart();
}
}
public class MyStopListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
sequencer.stop();
}
}
public class MyUpTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
float tempoFactor = sequencer.getTempoFactor();
sequencer.setTempoFactor((float)(tempoFactor * 1.03));
}
}
public class MyDownTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
float tempoFactor = sequencer.getTempoFactor();
sequencer.setTempoFactor((float)(tempoFactor * .97));
}
}
public class MySendListener implements ActionListener { // new - save
public void actionPerformed(ActionEvent a) {
// 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) checkboxList.get(i);
if (check.isSelected()) {
checkboxState[i] = true;
}
}
try {
out.writeObject(userName + nextNum++ + ": " +
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Head First Java Code(配套《Head First Java》的例程代码) (208个子文件)
BeatBoxFinal.class 9KB
BeatBoxSaveOnly.class 6KB
BeatBox.class 6KB
QuizCardReader.class 5KB
QuizCardBuilder.class 5KB
SimpleChatClient.class 3KB
DotComBust.class 3KB
MiniMusicPlayer3.class 3KB
ServiceBrowser.class 2KB
MiniMiniMusicCmdLine.class 2KB
SimpleChatClientA.class 2KB
Jukebox5.class 2KB
GameHelper.class 2KB
Jukebox6.class 2KB
Jukebox8.class 2KB
MusicServer.class 2KB
GameSaverTest.class 2KB
VerySimpleChatServer.class 2KB
RyanAndMonicaJob.class 2KB
BeatBoxFinal$MySendListener.class 2KB
ServiceServerImpl.class 2KB
PhraseOMatic2.class 2KB
Jukebox3.class 2KB
MiniMusicService$PlayItListener.class 2KB
MiniMiniMusicApp.class 2KB
GuessGame.class 2KB
DailyAdviceServer.class 2KB
DayOfTheWeekService.class 2KB
BeatBoxFinal$RemoteReader.class 2KB
TwoButtons.class 2KB
BeatBoxSaveOnly$MyReadInListener.class 2KB
DayOfTheWeekService$DoItListener.class 2KB
PhraseOMatic.class 2KB
Jukebox1.class 2KB
VerySimpleChatServer$ClientHandler.class 2KB
MiniMusicService$MyDrawPanel.class 2KB
MiniMusicService.class 2KB
DiceService$RollEmListener.class 2KB
KathyServlet.class 1KB
BeatBoxSaveOnly$MySendListener.class 1KB
DailyAdviceClient.class 1KB
QuizCardBuilder$SaveMenuListener.class 1KB
Game.class 1KB
QuizCardReader$NextCardListener.class 1KB
MiniMusicPlayer3$MyDrawPanel.class 1KB
DungeonTest.class 1KB
QuizCard.class 1KB
BeatBoxFinal$MyListSelectionListener.class 1KB
MusicServer$ClientHandler.class 1KB
SimpleChatClient$IncomingReader.class 1KB
TestLifeSupportSim.class 1KB
Dog.class 1KB
RunThreads.class 1KB
DiceService.class 1KB
Song.class 1KB
MyRunnable.class 1KB
DotCom.class 1KB
SimpleChatClientA$SendButtonListener.class 1KB
SimpleChatClient$SendButtonListener.class 1KB
QuizCardBuilder$NextCardListener.class 1KB
GameCharacter.class 1KB
FullMoons.class 1KB
SimpleAnimation.class 1KB
Of76.class 1KB
Animate.class 1KB
SimpleDotCom.class 1KB
QuizCardReader$OpenMenuListener.class 1KB
BeerSong.class 957B
Jukebox5$ArtistCompare.class 939B
PoorDogTestDrive.class 935B
BeatBoxSaveOnly$MyDownTempoListener.class 914B
ServiceBrowser$MyListListener.class 911B
BeatBoxFinal$MyDownTempoListener.class 911B
BeatBoxSaveOnly$MyUpTempoListener.class 908B
BeatBoxFinal$MyUpTempoListener.class 905B
MyDrawPanel.class 867B
BeatBox$MyDownTempoListener.class 866B
BeatBox$MyUpTempoListener.class 860B
GoodDogTestDrive.class 850B
QuizCardBuilder$NewMenuListener.class 834B
SimpleAnimation$MyDrawPanel.class 808B
TwoButtons$LabelButtonListener.class 795B
BeatBoxSaveOnly$MyStopListener.class 788B
BeatBoxFinal$MyStopListener.class 785B
TwoButtons$ColorButtonListener.class 758B
Player.class 746B
BeatBox$MyStopListener.class 740B
SimpleDotComTester.class 736B
BeatBoxSaveOnly$MyStartListener.class 715B
BeatBoxFinal$MyStartListener.class 712B
Animate$MyDrawP.class 709B
V2Radiator.class 686B
Boat.class 683B
TestBoat.class 680B
BeatBox$MyStartListener.class 667B
GoodDog.class 657B
DungeonGame.class 642B
SimUnit.class 551B
V3Radiator.class 538B
PoorDog.class 513B
共 208 条
- 1
- 2
- 3
资源评论
- chengjie11212017-07-27还不错,里面的代码可以参考
与向日葵有个约会
- 粉丝: 6
- 资源: 48
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功