Black Art of Java Game Programming PHẦN 4 ppsx

98 343 0
Black Art of Java Game Programming PHẦN 4 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Black Art of Java Game Programming:Creating Customizable Games with the AWT Selected methods in the abstract class Container are listed in Table 7-12. Table 7-12Container methods Container Methods Purpose public Component add(Component c); Adds the specified component public Component add(String s,Component c); Adds component with String argument public Insets insets(); Creates insets public void setLayout(LayoutManager m); Uses the specified layout manager public synchronized void remove(Component c); Removes the specified component public synchronized void removeAll(); Removes all components from container Components Table 7-13 lists the components we’ve discussed in this chapter and selected methods that are available. Remember that these classes also inherit the Component methods listed above. Table 7-13Components Class Methods Button public Button(); public Button(String label); public String getLabel(); public void setLabel(String label); Checkbox public Checkbox(); public Checkbox(String label); public Checkbox(String label, CheckboxGroup group,boolean state); public String getLabel(); public boolean getState(); public void setLabel(String label); public void setState(booleanstate); file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/264-267.html (2 von 4) [13.03.2002 13:18:24] Black Art of Java Game Programming:Creating Customizable Games with the AWT CheckboxGroup public CheckboxGroup(); public Checkbox getCurrent(); public synchronized void setCurrent(Checkbox box); Label public Label(); public Label(String label); public Label(String label,int alignment); public int getAlignment(); public String getText(); public void setAlignment(intalignment); public void setText(String label); TextField public Textfield(); public Textfield(int size); public Textfield(String text,intsize); public String getText(); // inherited from TextComponent public setText(String text); // inherited from TextComponent Containers Table 7-14 lists the containers discussed in this chapter and selected methods that are available. Table 7-14Container classes Container Methods Dialog public Dialog(Frame parent,boolean modal); public Dialog(Frame parent,String title,boolean modal); public synchronized void dispose(); // inherited from Window public boolean isModal(); public boolean isResizable(); public synchronized void pack(); // inherited from Window public void setResizable(boolean b); Frame public Frame(); file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/264-267.html (3 von 4) [13.03.2002 13:18:24] Black Art of Java Game Programming:Creating Customizable Games with the AWT public Frame(String title); public synchronized void dispose(); // overrides dispose() from Window public MenuBar getMenuBar(); public boolean isResizable(); public synchronized void pack(); // inherited from Window public void setCursor(int cursorType); public synchronized void setMenuBar(MenuBar mb); public void setResizable(boolean b); Panel public Panel(); Cursors Cursor types are static constants defined within the Frame class. To set the cursor, use the Frame method public void setCursor(int cursorType); Previous Table of Contents Next file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/264-267.html (4 von 4) [13.03.2002 13:18:24] Black Art of Java Game Programming:Creating Customizable Games with the AWT 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 Table 7-15 lists the cursors that are available. Table 7-15Cursor types Cursor Appearance Frame.CROSSHAIR_CURSOR Frame.DEFAULT_CURSOR Frame.E_RESIZE_CURSOR Frame.HAND_CURSOR Frame.MOVE_CURSOR Frame.NE_RESIZE_CURSOR Frame.NW_RESIZE_CURSOR Frame.N_RESIZE_CURSOR Frame.SE_RESIZE_CURSOR Frame.SW_RESIZE_CURSOR Frame.S_RESIZE_CURSOR Frame.TEXT_CURSOR file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/267-272.html (1 von 4) [13.03.2002 13:18:25] Black Art of Java Game Programming:Creating Customizable Games with the AWT Frame.WAIT_CURSOR Frame.W_RESIZE_CURSOR Menu, MenuBar, and MenuItem Table 7-16 lists selected methods for Menu, MenuBar, and MenuItem. Table 7-16Menu, MenuBar, and MenuComponent Class Methods Menu public Menu(String label); public synchronized MenuItemadd(MenuItem mi); public synchronized void remove(MenuComponent item); MenuBar public MenuBar(); public synchronized Menu add(Menu m); public synchronized void remove(MenuComponent item); MenuItem public MenuItem(String label); public void disable(); public void enable(); public void enable(boolean cond); The Event Class Table 7-17 lists instance variables of the Event class. Table 7-17Instance variables of the Event class Event Instance Variables Purpose public Object arg; Argument that depends on event type file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/267-272.html (2 von 4) [13.03.2002 13:18:25] Black Art of Java Game Programming:Creating Customizable Games with the AWT public int clickCount; The number of consecutive mouse clicks (e.g., clickCount = 2 for a double-click) public Event evt; The next event (used to create a linked list of events) public int id; The type of event public int key; The key (used for key events) public int modifiers; Modifier keys used during event public Object target; The object that triggered the event public long when; Time that event occurred public int x,y; x and y coordinates of event The id variable tells you the type of the event. Table 7-18 shows the values that id can take on. These values are defined as static constants in the Event class. Table 7-18Event types Event.id Value Interpretation ACTION_EVENT Action event GOT_FOCUS, LOST_FOCUS Component got/lost input focus KEY_ACTION, KEY_ACTION_RELEASE, KEY_PRESS, KEY_RELEASE Key event LIST_SELECT, LIST_DESELECT List event LOAD_FILE, SAVE_FILE File event MOUSE_DOWN, MOUSE_UP, MOUSE_DRAG MOUSE_MOVE, MOUSE_ENTER, MOUSE_EXIT Mouse event SCROLL_ABSOLUTE, SCROLL_LINE_UP, SCROLL_LINE_DOWN SCROLL_PAGE_UP, SCROLL_PAGE_DOWN Scrollbar event WINDOW_DESTROY, WINDOW_EXPOSE, WINDOW_DEICONIFY, WINDOW_ICONIFY WINDOW_MOVED Window event The interpretation of the Object argument to the action() method depends on the type of component that triggered the event. Table 7-19 associates the component (stored in the target variable) with the Object argument. file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/267-272.html (3 von 4) [13.03.2002 13:18:25] Black Art of Java Game Programming:Creating Customizable Games with the AWT Table 7-19Interpreting arguments to action(Event evt, Object obj) If evt.target Is an Instance of Then obj Is an Instance of Button String (the button label) Checkbox boolean (the checkbox state) Choice String (the chosen item) MenuItem String (the chosen item) TextField String (the input text) Suggestion Box • Allow the player to customize other features of the game, such as the bitmaps for the missile launcher, the aliens, or the explosions. Other possibilities for customization are the speed of the missiles, the scoring, and the color (or bitmap) for the background. • Explore the other widgets in the AWT. Here are the ones we didn’t cover in this chapter: Choice, List, Scrollbar, and TextArea. These aren’t hard to learn, and the pattern of creating interfaces remains the same. • Learn how to use GridBagLayout. This LayoutManager is the most powerful that the AWT provides, and you will use it in Java projects to come! Summary In this chapter, you’ve seen how Java’s AWT allows players to interact with and customize your games in an easy, intuitive fashion. By allowing customization, your games can appeal to the broadest audience possible. And by using the AWT, your applications and games can have graphical front ends that are portable to any platform that runs Java. In the next chapter, you’ll see how to use networking in your games! Previous Table of Contents Next file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch07/267-272.html (4 von 4) [13.03.2002 13:18:25] Black Art of Java Game Programming:Implementing a High Score Server on a Network 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 Part II Advanced Game and Graphics Techniques Chapter 8 Implementing a High Score Server on a Network Eric Ries Goals: Understand client-server networking fundamentals Implement high scores in Java Use Threads, Sockets, and Files Build a server Allowing competition among players enhances the enjoyment of any game. In traditional programming environments, a “high score list” is used to allow players to keep track of their best scores, thus providing an incentive for further play. Java extends this paradigm to a new level. By allowing communications over the Internet, Java allows players to compete against other players worldwide. Implementing a high score server in Java is relatively simple when compared with older-generation languages. To do this, you need two separate components: the client and the server. The client is the program (your game, in this case) that runs on the user’s computer. The server is the program that runs on the machine where your programs were initially located. By obtaining information from, and reporting back to, the server, your Java game can display and continually update a list of the best players of your game. This can be a decisive advantage for your game over other games that compete for users’ attention. file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch08/273-281.html (1 von 4) [13.03.2002 13:18:26] Black Art of Java Game Programming:Implementing a High Score Server on a Network In this chapter, there are two things we need to discuss. The first is using Java to handle high scores using concepts this book has already discussed. The second part of the chapter discusses using Java to implement these concepts over a network. Why Use Java for Network Programming? Client-server communication over the Internet has obviously been around much longer than Java. Java, however, brings with it an unprecedented level of ease-of-use in network programming. Being game programmers, we have absolutely no need to waste our time with all of the details of Internet communications (and there are many details). Java allows us to focus on the more important aspects of the program while it transparently takes care of the messy stuff in the background. What Is Client-Server Networking? Most individuals with a reasonable amount of computer experience understand the basics of client- server networking. However, generations of computer science majors have managed to come up with an entire lexicon designed to confuse you. Things like sockets, ports, packets, and streams may sound like they have more to do with fishing than with computers, so let’s start with a metaphor to help us along. Basic Client-Server Terminology Pretend you are trying to reach customer support at a huge corporation (a painful experience all of us have had). You call the company and reach the receptionist, who asks you for an extension. Luckily, you have the number handy, and you are transferred to the customer representative. The two of you have a delightful conversation, and then you both hang up. The whole process is simple and straightforward; any child could tell you how it’s done. Unfortunately, to do something simple like this on a network requires a whole new vocabulary. Let’s start with the most common terms we need to know: • Client. The entity making the call (in our example, you). • Server. The entity processing your requests (the company, in our example). • Socket. Computers on the Internet communicate just like you did with your customer service representative. However, instead of telephones, computers use sockets. Java provides you with a very handy Socket class, which handles all of the low-level code for network communications. All you have to do is dial. • IP address. For one computer to call another computer, it needs a “phone number.” In Internet language this is called an IP (for Internet protocol) address. This is a series of numbers and periods that looks something like this: 131.247.1.58. While this may not be too meaningful to a human being, an Internet computer can use it just like a phone number. • Domain name server (DNS). What if you didn’t know the number of a company? For a computer, this is never a problem, because computer memory is flawless. Humans are not so well equipped, so we sometimes rely on a phone book to find the number we’re looking for. On the Internet, this is called a domain name server (DNS), and it is what allows you to type in file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch08/273-281.html (2 von 4) [13.03.2002 13:18:26] Black Art of Java Game Programming:Implementing a High Score Server on a Network an address like “www.waite.com” instead of all those pesky numbers. Using an IP address or its DNS equivalent, a client program can open a socket connection to a server. Bear in mind that every computer connected to the Internet must have a unique IP address assigned to it. • Port. What does a client do once it has connected to a server? Just as in our example, it gets the receptionist, who asks it for an extension. In Internet jargon, the extension is called the port. On any one machine, any program can access any port, which is usually given a number between 1 and 9999. • Service. No two programs can share a port, so each port represents a different service offered by the server. In order for a client and a server to communicate, the server must be listening to the same port that the client is calling on. Otherwise, your client might get sales instead of customer support. • Protocol. Now, when you finally get through to someone on their extension, it doesn’t do anybody any good if they speak Korean and you speak Portuguese. In order to do any kind of useful communicating, the client and the server must use the same protocol. A protocol is like a language that computers use to speak to each other. A protocol defines the order and type of interactions that can take place in a socket connection. Even though you may not know it, you are probably familiar with many protocols already. • HyperText Transfer Protocol (HTTP). This is the most popular protocol on the World Wide Web. It is used to send a wide variety of textual and multimedia data. Other common ones include Gopher, Telnet, FTP , WAIS, and SMNP. The protocols that we will be using are far less complex, but the concepts are the same. A typical phone conversation is shown in Figure 8-1, and its networking equivalent is shown in Figure 8-2. Figure 8-1 Diagram of telephone conversation Figure 8-2 Networking equivalents of telephone metaphor file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch08/273-281.html (3 von 4) [13.03.2002 13:18:26] [...].. .Black Art of Java Game Programming: Implementing a High Score Server on a Network Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/273-281.html (4 von 4) [13.03.2002 13:18:26] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams,... Color")); Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/296-299.html (4 von 4) [13.03.2002 13:18:30] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 157169 043 3 Pub Date: 11/01/96 Previous Table of Contents Next This adds... applet! Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/293-296.html (3 von 3) [13.03.2002 13:18:29] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 157169 043 3 Pub Date: 11/01/96 Previous Table of Contents Next Creating... background color Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/291-293.html (3 von 3) [13.03.2002 13:18:29] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 157169 043 3 Pub Date: 11/01/96 Previous Table of Contents Next Methods... 2 0Java% 2 0Game% 2 0Programming/ ch08/2 84- 287.html (4 von 4) [13.03.2002 13:18:28] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 157169 043 3 Pub Date: 11/01/96 Previous Table of Contents Next Converting Data to Objects To successfully parse this string of data, we must take each name/score... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/2 84- 287.html (3 von 4) [13.03.2002 13:18:28] Black Art of Java Game Programming: Implementing a High Score Server on a Network nextToken() /* Returns the next substring token */ countTokens() /* Returns the total number of tokens in the tokenizer */ Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/2 84- 287.html... Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/299-301.html (3 von 3) [13.03.2002 13:18:30] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 157169 043 3 Pub Date: 11/01/96 Previous Table of Contents Next Converting HighScoreManager to a... all of this work for nothing, but you will soon see that putting a lot of work into the foundation classes will make writing the exciting stuff a lot easier This is one of the lessons any OOP programmer has to learn very well Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/287-290.html (4 von 4) [13.03.2002 13:18:28] Black Art of Java Game. .. create a series of object classes that can be used to effectively store, retrieve, and transmit high scores For this, we will create a special class for a single high score, and a class for a list of many high scores The HighScoreList Class file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/2 84- 287.html (1 von 4) [13.03.2002 13:18:27] Black Art of Java Game Programming: Implementing... aspect of our design is that it requires almost no calculation to be done by the server The server merely needs to send us a string of raw data that we can parse, and process our occasional updates (more on this later) Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch08/282-2 84. html (3 von 3) [13.03.2002 13:18:27] Black Art of Java Game Programming: Implementing . Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch07/267-272.html (4 von 4) [13.03.2002 13:18:25] Black Art of Java Game Programming: Implementing a High Score Server on a Network Black Art of Java Game. cursorType); Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch07/2 64- 267.html (4 von 4) [13.03.2002 13:18: 24] Black Art of Java Game Programming: Creating. file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch07/2 64- 267.html (2 von 4) [13.03.2002 13:18: 24] Black Art of Java Game Programming: Creating Customizable Games with the AWT CheckboxGroup

Ngày đăng: 12/08/2014, 09:21

Mục lục

  • Black Art Of Java Game Programming

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Implementing a High Score Server on a Network

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

    • Black Art of Java Game Programming:Advanced Networking and Multiplayer Gaming Concepts

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan