Double-Buffering the Drawing


Besides the swim method, the Fish class also has a drawFishImage method, which will draw the fish. Note that the last line in the run method, after each fish has been moved to a new position by the swim method, calls the window's repaint method to redraw the aquarium:

     while (runOK) {         for (int loopIndex = 0; loopIndex < numberFish; loopIndex++){             fish = (Fish)fishes.elementAt(loopIndex);             fish.swim();         }         try {             Thread.sleep(sleepTime);         }         catch (Exception exp) {             System.out.println(exp.getMessage());         }         repaint(); } 

The repaint method calls the update method, which is where flickering usually happens, because by default the update method first redraws the entire window using the background window color. To avoid that flickering, this application overloads the update method itself, drawing everything in the offscreen memory-based image memoryImage and then drawing the completed image onscreen.

The overridden update method is passed the Graphics object it should use to draw in the main window. In the Aquarium application, the code starts by drawing the background image using the Graphics object corresponding to the memory image, not to the main window. You can draw the background image in the memory image with the drawImage method by passing it the image to draw, the X and Y location at which to draw the new image, and an object that acts as an image observer in case you want to be notified of the events that happen as the image is loaded (Aquarium doesn't make use of these events, so the code simply passes the current object as the image observer):

 public void update(Graphics g) {     memoryGraphics.drawImage(aquariumImage, 0, 0, this);         .         .         . } 

And you can draw each fish in the memory image simply by calling each fish's drawFishImage method:

 public void update(Graphics g) {     memoryGraphics.drawImage(aquariumImage, 0, 0, this);     for (int loopIndex = 0; loopIndex < numberFish; loopIndex++){         ((Fish)fishes.elementAt(loopIndex)).drawFishImage             (memoryGraphics);     }         .         .         . } 

Now that the offscreen image is complete, you can flash it onto the screen with minimum flicker, like so:

 public void update(Graphics g) {     memoryGraphics.drawImage(aquariumImage, 0, 0, this);     for (int loopIndex = 0; loopIndex < numberFish; loopIndex++){         ((Fish)fishes.elementAt(loopIndex)).drawFishImage             (memoryGraphics);     }     g.drawImage(memoryImage, 0, 0, this); } 

That completes everything except creating the actual Fish objects with their constructors, the swim and drawImage methods. That's coming up next.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

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