FileDocCategorySizeDatePackage
Blob2.javaAPI DocExample1634Thu Feb 03 08:18:02 GMT 2000None

Blob2.java

// File: Blob2.java
// T Balls : March 1998
// Paint filled ovals on screen
// in response to mouse events
// Modified version of Blob1.java
// Model: a Vector of Blob objects :added

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Blob2 extends Frame
{  public static void main( String[] args )
   {  new Blob2();
   }
   
   public Blob2()
   {  setTitle( "Blob2" ); 
      setSize( 300, 300 );
      picture = new Picture();
      
      addMouseListener( new MouseAdapter() {
         public void mousePressed( MouseEvent e )
         {  picture.addBlob( e.getX(), e.getY() );
            repaint();
         }
      });
      
      addWindowListener( new WindowAdapter() {
         public void windowClosing( WindowEvent e )
         {  System.exit(0);
         }
      });
      
      setVisible( true ); 
   }

   private Picture picture;
   
   public void paint( Graphics g )
   {  picture.paint( g );
   }
   
   public void update( Graphics g )
   {  paint( g );
   }
      
}

class Blob
{  public Blob( int x, int y )
   {  this.x = x;
      this.y = y;
   }
   private int x;
   private int y;
   
   public void paint( Graphics g )
   {  g.fillOval( x, y, 10, 10 );
   }
}

class Picture
{  public Picture()
   {  blobs = new Vector();      
   }

   private Vector blobs;

   public void addBlob( int x, int y )
   {  blobs.addElement( new Blob( x, y ) );
   }

   public void paint( Graphics g )
   {  for( int i = 0; i < blobs.size(); i ++ )
      {  ((Blob)(blobs.elementAt(i))).paint(g);
      }
   }
}