
Java Programming for absolute beginner- P23
Thông tin tài liệu:
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ìm kiếm theo từ khóa liên quan:
Nhập môn lập trình lập trình website lập trình ngôn ngữ html giáo trình php lập trình C++Tài liệu có liên quan:
-
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 357 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 204 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 188 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 143 0 0 -
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 137 0 0 -
8 trang 99 0 0
-
Bài giảng Nhập môn lập trình - Chương 12: Quản lý bộ nhớ
23 trang 69 0 0 -
Giáo trình Lập trình Website (Nghề Tin học ứng dụng - Trình độ Cao đẳng) - CĐ GTVT Trung ương I
156 trang 62 2 0 -
Báo cáo đồ án môn học II: Thiết kế website bán đồng hồ
25 trang 56 0 0 -
42 trang 53 0 0
-
Tạo mạng xã hội với PHP - part 43
10 trang 46 0 0 -
4 trang 43 0 0
-
Bài giảng Nhập môn lập trình: Bài 2 - Thuật toán
32 trang 42 0 0 -
Nhập môn lập trình (Đặng Bình Phương) - Các khái niệm cơ bản về hệ điều hành
17 trang 39 0 0 -
Một số giải pháp lập trình ASP.NET 2.0
82 trang 38 0 0 -
MỘT SỐ KHÁI NIỆM CƠ BẢN CỦA NGÔN NGỮ C
49 trang 38 0 0 -
LẬP TRÌNH C ++ QUẢN LÝ NHÀ TRỌ
12 trang 38 0 0 -
Câu hỏi bài tập nhập môn lập trình
11 trang 37 0 0 -
CÁC KHÁI NIỆM CƠ BẢN VỀ LẬP TRÌNH
21 trang 36 0 0 -
142 trang 35 0 0