previous | start | next

The model: 1

The RectangleComponent object contains the data - the model.

    1   
    2   public class RectangleComponent extends JComponent
    3   {  
    4      private static final int BOX_X = 100;
    5      private static final int BOX_Y = 100;
    6      private static final int BOX_WIDTH = 20;
    7      private static final int BOX_HEIGHT = 30;
    8   
    9      private Rectangle box;
   10   
   11      public RectangleComponent()
   12      {  
   13         // The rectangle that the paintComponent method draws
   14         box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT);         
   15      }
   16   
   17      public void paintComponent(Graphics g)
   18      {  
   19         Graphics2D g2 = (Graphics2D) g;
   20   
   21         g2.draw(box);
   22      }
   23   
   24      /**
   25         Moves the rectangle to the given location.
   26         @param x the x-position of the new location
   27         @param y the y-position of the new location
   28      */
   29      public void moveTo(int x, int y)
   30      {
   31         box.setLocation(x, y);
   32         repaint();      
   33      }
   34   } 


previous | start | next