01
Oct
Black Art of Java Game Programming by Joel
Being able to invoke another constructor can save you a bit of typing, if you ve written one constructor that does all the work and you want another way of calling it. Using the DancingRect Class Now let s go back to the DancingRect class. We can create an instance of DancingRect by calling the constructor in the following manner: // create an instance of DancingRect at (80,80) // with width 40 and height 40 DancingRect r = new DancingRect(80,80,40,40); To tell r to perform a dance step and paint itself, use the following syntax: r.danceStep(); // make a dance step r.paint(); // paint r to screen Let s use the DancingRect class to implement a new version of the Mondrian class from Chapter 1. In Listing 2-4 you can immediately see how information is now distributed, handled, and encapsulated by the objects that need it, instead of being accessible to all. Listing 2-4 Rebuilt Mondrian.java import java.applet.*; import java.awt.*; // rebuilt Mondrian with objects public class Mondrian2 extends Applet { static final int NUM_RECTS = 9; DancingRect r[]; // array of dancing rectangles public void init() { System.out.println(”>> init <<"); setBackground(Color.black); initRectangles(); } public void initRectangles() { // allocate dancing rectangles // now the data is encapsulated by the objects! r = new DancingRect[NUM_RECTS];
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services
