The Clips Loader


The Sounds Panel

SoundsPanel implements a JPanel that draws a white background and four images. The interesting part of the code is the setting up and use of the images' hot spots (rectangular areas the same size as each image). If a mouse press is inside one of the hot spots, then LoadersTests' playClip( ) plays the associated clip.

The SoundsPanel constructor stores a reference to LoadersTests, calls initImages( ), and sets up the MouseListener to call selectImage( ):

     // globals     private static final int PWIDTH = 350;     // size of this panel     private static final int PHEIGHT = 350;     private LoadersTests topLevel;     public SoundsPanel(LoadersTests sts)     { topLevel = sts;       setPreferredSize( new Dimension(PWIDTH, PHEIGHT) );       initImages( );       addMouseListener( new MouseAdapter( ) {         public void mousePressed( MouseEvent e)         { selectImage( e.getX( ), e.getY( ));  }       } );     }

initImages( ) uses ImagesLoader to load the four GIFs, whose names are hard-wired into the code in the names[] array. The width and height of each image is used to build the array of Rectangle objects that represent the hot spots:

     // globals     // clip image names     private final static String[] names = {"dog", "cat", "sheep", "chicken"};     // on-screen top-left coords for the images     private final static int[] xCoords = {20, 210, 20, 210};     private final static int[] yCoords = {25, 25, 170, 170};     // location of image and sound info     private final static String IMS_FILE = "imagesInfo.txt";     private int numImages;     private BufferedImage[] images;     private Rectangle[] hotSpots;     // a click inside these triggers the playing of a clip     private void initImages( )     // load and initialise the images, and build their "hot spots"     {       numImages = names.length;       hotSpots = new Rectangle[numImages];       images = new BufferedImage[numImages];       ImagesLoader imsLoader = new ImagesLoader(IMS_FILE);       for (int i=0; i < numImages; i++) {         images[i] = imsLoader.getImage(names[i]);         hotSpots[i] = new Rectangle( xCoords[i], yCoords[i],                          mages[i].getWidth( ), images[i].getHeight( ));           // use images' dimensions for the size of the rectangles       }     }

Each hot-spot rectangle is defined by a top-left coordinate, taken from the xCoords[] and yCoords[] arrays and from a width and height obtained from the loaded image. paintComponent( ) draws the images in the panel using the same xCoords[] and yCoords[] data as the hot-spot rectangles, thereby ensuring that they occupy the same spaces.

selectImage( ) tries to find the hot spot containing the mouse press coordinates. A matching hot spot's index position in hotSpots[] is used to retrieve a clip name from names[]. playClip( ) is passed the name and the index:

     private void selectImage(int x, int y)     /* Work out which image was clicked on (perhaps none),        and request that its corresponding clip be played. */     {       for (int i=0; i < numImages; i++)         if (hotSpots[i].contains(x,y)) {    // (x,y) inside hot spot?           topLevel.playClip(names[i], i);   // play that name's clip           break;         }     }

Back in LoadersTests, playClip( ) is defined as:

     public void playClip(String name, int i)     // called from SoundsPanel to play a given clip (looping or not)     {  clipsLoader.play(name, clipLoops[i]);  }

The index parameter is employed to look inside clipLoops[] to get the playing mode. This coding approach works because I've ensured that the clipLoops[] array refers to the clips in the same order as the arrays in SoundsPanel.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

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