01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

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.

finish the story<br />

And now the GUI work...<br />

We need one more piece <strong>of</strong> code to get this story together. We need to add a<br />

button to the GUI that lets the user actually send the Poke. Here’s the code to take<br />

care <strong>of</strong> that task:<br />

// The code below goes in BeatBox.java,<br />

// in the buildGUI() method<br />

JButton sendIt = new JButton("sendIt");<br />

sendIt.addActionListener(new MySendListener());<br />

buttonBox.add(sendIt);<br />

JButton sendPoke = new JButton("Send Poke");<br />

sendPoke.addActionListener(new MyPokeListener());<br />

buttonBox.add(sendPoke);<br />

userMessage = new JTextField();<br />

buttonBox.add(userMessage);<br />

// Below is new code we need to add, also to BeatBox.java<br />

public class MyPokeListener implements ActionListener {<br />

}<br />

public void actionPerformed(ActionEvent a) {<br />

// We'll create an empty state array here<br />

boolean[] checkboxState = new boolean[255];<br />

}<br />

182 Chapter 6<br />

try {<br />

out.writeObject(POKE_START_SEQUENCE);<br />

out.writeObject(checkboxState);<br />

} catch (Exception ex) {<br />

System.out.println("Failed to poke!");<br />

}<br />

Download at WoweBook.Com<br />

Finally, add the button to the box<br />

holding the other buttons.<br />

Task 5 <br />

Add button to GUI<br />

to send Poke sequence<br />

to other BeatBox<br />

instances.<br />

First we need to create a new<br />

button for our Poke feature.<br />

Then we set up a<br />

listener so we can react<br />

when it’s clicked.<br />

Here we create an array <strong>of</strong><br />

booleans for our state. We can<br />

leave them all false because the<br />

receiving side ignores them when<br />

it gets the POKE command.<br />

Here’s the magic: to send a poke we send the<br />

magic POKE_START_SEQUENCE and our array<br />

<strong>of</strong> booleans to the server. The server will relay<br />

our magic sequence to the other clients, and<br />

they’ll beep at the user because <strong>of</strong> the earlier<br />

code we wrote (back on page 180).<br />

.5

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

Saved successfully!

Ooh no, something went wrong!