tailieunhanh - Black Art of Java Game Programming PHẦN 5

Trước hết, chúng ta hãy định nghĩa một lớp trừu tượng, diễn viên, quy định cụ thể các phương pháp thiết yếu và các biến của các đối tượng di chuyển độc lập trên màn hình. Lớp này được thể hiện trong Ví dụ 10-1. Danh sách 10-1 diễn viên Nam diễn viên phụ xuất sắc lớp lớp trừu tượng mở rộng | Black Art of Java Game Programming Advanced Techniques Previous Table of Contents Next file D Downloads Books Computer Java 20Game 20Programming ch10 5 von 5 13 18 42 Black Art of Java Game Programming Advanced Techniques Black Art of Java Game Programming by Joel Fan Sams Macmillan Computer Publishing ISBN 1571690433 Pub Date 11 01 96 Previous Table of Contents Next Creating a Simple Multithreaded Animation Let s create a multithreaded animation applet. Although this example is simple it ll highlight the differences of programming with threads. First of all let s define an abstract class Actor that specifies the essential methods and variables of objects that move independently on the screen. This class is shown in Listing 10-1. Listing 10-1 Actor class abstract class Actor extends Thread Applet applet protected boolean drawNow abstract void paint Graphics g Each Actor object is a separate thread that runs independently. When it needs to paint itself for example after a change in color or position it will set drawNow to true and notify the mother applet. For example here is the run method of an Actor subclass called CycleColorActor which paints a rectangle that changes colors every few milliseconds public void run while true drawNow true updateColor try sleep delay catch InterruptedException e The repaint method of the applet eventually triggers a call to each Actor s individual paint method. file D Downloads Books Computer Java 20Game 20Programming ch10 1 von 5 13 18 43 Black Art of Java Game Programming Advanced Techniques Here s the paint of the CycleColorActor public void paint Graphics g if drawNow new Color red green blue locx locy 73 73 drawNow false As you see only those Actors that need to paint will draw themselves. The full definition of the CycleColorActor is shown in Listing 10-2. The constructor allows you to specify the location of the .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.