Implementing NetRatServer - Second Iteration


Implementing NetRatServer — First Iteration

Table 20-4 lists the design considerations and design decisions for the first iteration.

Table 20-4: First Iteration Design Considerations and Decisions

Check-Off

Design Considerations

Design Decisions

 

Objectives for the first iteration

Implement the Rat, Floor, RobotRat, and NetRatServer classes concentrating on drawing a collection of Rat images on the Floor and testing their move capability. Network issues will be ignored for now.

 

Rat class

The Rat class will keep track of its x and y coordinates upon the floor. The Rat class will also have an associated image that will get painted to the floor.

 

Floor class

The Floor class will reference a Vector of Rat objects and paint each Rat’s image on the Floor using its x and y coordinates.

 

java.util.Vector class

The Vector class will be used to maintain a collection of Rat objects.

 

javax.JFrame

The Floor will be drawn into a static instance of JFrame.

 

RobotRat class

The RobotRat class will contain one static instance of Floor, Vector, and JFrame. These static instances will be used by all instances of RobotRat. An instance of RobotRat will maintain and move its own private instance of a Rat.

 

RMI Functionality

Ignored in this iteration.

 

NetRatServer

The NetRatServer class will be used to test the functionality of the classes developed during this iteration.

First Iteration Code

The following code is developed for this first iteration:

Referring to example 20.3 through 20.6 — The Rat class implementation is fairly straight forward. It has three private fields representing its x and y coordinates and its image. It uses the Toolkit class to load the image from book rat.gif from the default working directory. It implements various getter and setter methods to manipulate its coordinates and retrieve its image.

Example 20.3: Rat.java

image from book
 1     import java.awt.*; 2     import javax.swing.*; 3 4     public class Rat { 5       private Image _its_image = null; 6       private int _x = 0; 7       private int _y = 0; 8 9       public Rat(String image_name){ 10       Toolkit tool_kit = Toolkit.getDefaultToolkit(); 11       try{ 12          _its_image = tool_kit.getImage(image_name); 13         }catch(Exception e){ 14           System.out.println("Rat constructor: Problem loading or creating Rat image!"); 15           e.printStackTrace(); 16         } 17        _x = 0; 18        _y = 0; 19       } 20 21       public Rat(){ 22         this("rat.gif"); 23       } 24 25       public Image getImage(){ 26        return _its_image; 27       } 28 29       public int getX(){ 30         return _x; 31        } 32 33       public int getY(){ 34         return _y; 35       } 36 37       public void setX(int x){ 38         _x = x; 39       } 40 41       public void setY(int y){ 42         _y = y; 43       } 44     } // end Rat class definition
image from book

Example 20.4: Floor.java

image from book
 1     import java.awt.*; 2     import java.util.*; 3 4     public class Floor extends Canvas { 5       private Vector _rats = null; 6 7        public Floor(Vector rats){ 8          _rats = rats; 9          this.setBackground(Color.BLACK); 10       } 11 12       public void paint(Graphics g){ 13         for(int i = 0; i<_rats.size(); i++){ 14           if(_rats.elementAt(i) != null){ 15              g.drawImage(((Rat)_rats.elementAt(i)).getImage(),((Rat) _rats.elementAt(i)).getX(), 16                          ((Rat) _rats.elementAt(i)).getY(), this); 17            } 18         } 19       } 20     } // end Floor class definition
image from book

Example 20.5: RobotRat.java

image from book
 1     import javax.swing.*; 2     import java.util.*; 3 4     public class RobotRat { 5 6       private static JFrame _frame = null; 7       private static Floor _floor = null; 8       private static Vector _rats = null; 9       private Rat _its_rat = null; 10 11      static { 12        _rats  = new Vector(); 13        _floor = new Floor(_rats); 14        _frame = new JFrame("Rat Movement Floor"); 15        _frame.getContentPane().add(_floor); 16        _frame.setSize(300, 300); 17        _frame.setLocation(200, 200); 18        _frame.show(); 19      } 20 21 22      public RobotRat() { 23        _its_rat = new Rat(); 24        _rats.addElement(_its_rat); 25        _floor.repaint(); 26       } 27 28      public void moveEast(){ 29        _its_rat.setX(_its_rat.getX() + 3); 30        _floor.repaint(); 31      } 32 33      public void moveSouth(){ 34        _its_rat.setY(_its_rat.getY() + 3); 35        _floor.repaint(); 36      } 37 38      public void moveWest(){ 39        _its_rat.setX(_its_rat.getX() - 3); 40        _floor.repaint(); 41      } 42 43      public void moveNorth(){ 44        _its_rat.setY(_its_rat.getY() - 3); 45        _floor.repaint(); 46      } 47 48      public void moveNorthWest(){ 49        _its_rat.setY(_its_rat.getY() - 3); 50        _its_rat.setX(_its_rat.getX() - 3); 51        _floor.repaint(); 52      } 53 54      public void moveSouthWest(){ 55        _its_rat.setY(_its_rat.getY() + 3); 56        _its_rat.setX(_its_rat.getX() - 3); 57        _floor.repaint(); 58      } 59 60      public void moveNorthEast(){ 61        _its_rat.setY(_its_rat.getY() - 3); 62        _its_rat.setX(_its_rat.getX() + 3); 63        _floor.repaint(); 64      } 65 66       public void moveSouthEast(){ 67         _its_rat.setY(_its_rat.getY() + 3); 68         _its_rat.setX(_its_rat.getX() + 3); 69         _floor.repaint(); 70       } 71     } // end RobotRat class definition
image from book

Example 20.6: NetRatServer.java

image from book
 1     import javax.swing.*; 2     import java.awt.*; 3     import java.awt.event.*; 4 5     public class NetRatServer extends JFrame implements ActionListener { 6 7       private JButton button1 = null; 8       private RobotRat _r1 = null; 9       private RobotRat _r2 = null; 10 11      public NetRatServer(){ 12 13        _r1 = new RobotRat(); 14        _r2 = new RobotRat(); 15        button1 =  new JButton("move"); 16        button1.addActionListener(this); 17        this.getContentPane().add(button1); 18        this.setSize(100, 100); 19        this.setLocation(300, 300); 20        this.show(); 21      } 22 23      public void actionPerformed(ActionEvent ae){ 24        if(ae.getActionCommand().equals("move")){ 25          _r1.moveEast(); 26          _r2.moveSouthEast(); 27       } 28      } 29 30      public static void main(String[] args){ 31        System.out.println("NetRatServer Lives!!"); 32        new NetRatServer(); 33      } // end main() 34    }// end NetRatServer class definition
image from book

The Floor class extends the Canvas class as called for in the design. It contains a Vector reference field that is initialized to the Vector reference passed in via the constructor. The Floor class overrides the paint() method to draw the rat images contained in the Vector object upon itself.

The RobotRat class has a lot going on. It uses a static initializer to initialize the JFrame, Vector, and Floor objects. The JFrame size and location is set in the static initializer as well. Because these statements are located in a static initializer block they are executed when the class is loaded. The RobotRat constructor creates an instance of a Rat and adds it to the vector of rats via the _rats reference. The repaint() method is then called via the _floor reference. This causes the paint() method to be called which draws the collection of Rat images on the floor.

The NetRatServer class is used to test the first iteration code. For testing purposes a simple GUI that consists of one button is created that allows users to move a couple of rats about the floor in limited directions.

Testing The Fist Iteration Code

Figures 20-11 and 20-12 show the NetRatServer application on start-up and after the move button has been clicked about ten times.

image from book
Figure 20-11: NetRatServer Application Upon Start-up

image from book
Figure 20-12: NetRatServer Application After Approximately 10 move Button Clicks

Quick Review

When ready to proceed with the implementation phase select the smallest subset of the design that results in a set of application functionality that can be tested. Some code written during the implementation may be ultimately discarded before the final application is released.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net