Danh mục tài liệu

Java Programming for absolute beginner- P23

Số trang: 20      Loại file: pdf      Dung lượng: 296.79 KB      Lượt xem: 18      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Programming for absolute beginner- P23:Hello and welcome to Java Programming for the Absolute Beginner. You probablyalready have a good understanding of how to use your computer.These days it’s hard to find someone who doesn’t, given the importanceof computers in today’s world. Learning to control your computer intimatelyis what will separate you from the pack! By reading this book, you learnhow to accomplish just that through the magic of programming.
Nội dung trích xuất từ tài liệu:
Java Programming for absolute beginner- P23 JavaProgAbsBeg-11.qxd 2/25/03 8:57 AM Page 398 398 Registering PlayAreaListeners Java Programming for the Absolute Beginner The registering of PlayAreaListeners takes place in the PlayArea class. You won’t see the entire source listing for PlayArea.java until a bit later, but I feel it is most relevant to discuss how it registers listeners here, and how it fires events in the next section, “Firing PlayAreaEvents.” Registering the listeners is not that com- plicated. Basically, PlayArea uses a Vector to maintain a dynamic list of PlayAre- aListeners: protected Vector listeners; The PlayArea class also provides two methods for updating this Vector object, addPlayAreaListener(PlayAreaListener) and removePlayAreaListener(Pla- yAreaListener). addPlayAreaListener(PlayAreaListener) adds the passed Pla- yAreaListener, any class that implements the PlayAreaListener interface, to listeners and removePlayAreaListener(PlayAreaListener) removes the speci- fied PlayAreaListener. Here is what they look like: public void addPlayAreaListener(PlayAreaListener pal) { listeners.addElement(pal); } public void removePlayAreaListener(PlayAreaListener pal) { listeners.removeElement(pal); } You can see that they work just by calling the Vector class’s methods. That’s all it takes. Now you have a list of nosy classes that you need to notify when you fire PlayAreaEvents. Firing PlayAreaEvents You fire PlayAreaEvents from the PlayArea class by creating an instance of the PlayAreaEvent class and then calling the registered PlayAreaListeners’ block- Landed(BlockEvent) methods. The PlayArea class has a private method for doing this: private void fireBlockLanded(PlayAreaEvent pae) { for (int l = 0; l < listeners.size(); l++) { ((PlayAreaListener)listeners.elementAt(l)).blockLanded(pae); } } fireBlockLanded(PlayAreaEvent) loops on the elements in the listeners Vector and calls their blockLanded(PlayAreaEvent) methods, passing in the given pae reference. As you can see, the PlayAreaEvent object must be created prior to call- ing fireBlockLanded(PlayAreaEvent). It does this by counting the number of rows completed and checking whether the block is out of area and passing those values along with this as the source of the event (meaning this PlayArea object TEAM LinG - Live, Informative, Non-cost and Genuine!Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-11.qxd 2/25/03 8:57 AM Page 399 399 was the source of the event) and passing these values to the PlayAreaEvent con- structor method. I’ll talk more about the details of creating and firing Pla- Chapter 11 yAreaEvents, such as firing them in their own threads, in the next section, “Creating the PlayArea Class.” Creating the PlayArea Class The PlayArea class is the most significant part of this project. It extends the ...

Tài liệu được xem nhiều:

Tài liệu có liên quan: