30
Sep
// paint all rectangles void paint(Graphics g) {
// paint all rectangles void paint(Graphics g) { for (int i = 0; i < NUM_RECTS; i++) { g.fillRect(locx[i],locy[i],width[i],height[i]); } } Actually, this code would work fine if you filled in all the blanks. The problem is that it s not a good foundation for a more complex animation applet. Let s say you wanted to devise new dance patterns for the rectangles, and add new shapes, such as ovals or triangles. Placing all this new information into the definition of Broadway would quickly lead to a complex program that s both difficult to understand and to extend. Programming languages were devised to help programmers manage complexity. Object-oriented languages like Java encourage you to use objects to design and manage complex programs, such as games. Writing a program in an object-oriented language doesn t mean that it s designed in an objectoriented manner; the Broadway applet is a case in point. Let s use objects to build a version of Broadway that s easily extensible and understandable. We will build Broadway in three steps, using 1. Objects 2. Inheritance 3. Dynamic method binding As it turns out, these are the three keys to object-oriented programming. Let s get to work! Using Objects Sometimes it s necessary to tear down what has already been written in order to construct a better infrastructure that allows you to move ahead. That s what we re doing right now. Our immediate goal is to build an applet that will support multiple rectangles dancing in various ways. The key is defining a class that implements the desired states and behaviors. Defining the Class Let s identify the candidates for objecthood in this new applet. Since there are multiple dancing rectangles, and each has a different state, such as its position and its current dance step, each dancing rectangle should be an object. Its instance variables will track the state. What behaviors will a dancing rectangle have? That depends on the actual dance steps the rectangles will implement. For now, we ll delay making a decision on the particular dances, and provide a default method, danceStep(), which does the simplest dance step standing still. In addition, each dancing rectangle will also be responsible for drawing itself to the screen. Figure 2-8 shows a schematic of what a DancingRect object looks like.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services
