HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Meat Fighter"
Vol. 8, Issue 3, p. 8

	


Listing 1

1  public void animationTest() {
2
3    // Enter full-screen mode
4    // and start animation
5    GraphicsSource.enterDisplayMode(
        640, 480);
6    GraphicsSource.setRenderListener(
        this);
7    GraphicsSource.startAnimation(60);
8
9    // wait for it to complete ...
10
11   // Stop animation
12   // and exit full-screen mode
13   GraphicsSource.stopAnimation();
14   GraphicsSource.exitDisplayMode();
15 }
16
17 public void init(
      boolean isPageFlipping,
      int bufferWidth,
      int bufferHeight) {
18
19   // Load the background image
20   // and foreground sprite
21   background = ImageSource.getImage(
        "background.gif");
22   fighter = ImageSource.getImage(
        "fighter.gif");
23 }
24
25 public void updateModel() {
26   // update position of fighter
27   // and background ...
28 }
39
30 public void render(Graphics g,
      boolean isBufferCleared,
      int bufferIndex) {
31
32   // Draw the animation frame
33   g.drawImage(
        background, xb, yb, null);
34   g.drawImage(fighter, xf, yf, null);
35 }

Listing 2

1  long fractions = 0L;
2  long overSleepTime = 0L;
3  int noDelays = 0;
4  animating = true;
5  StopWatch stopWatch 
       = StopWatchSource.getStopWatch();
6  stopWatch.start();
7
8  while(animating) {
9
10   // Update the game state
11   listener.updateModel();
12
13   // Obtain a graphics context to the 
14   // back buffer and render the frame
15   Graphics g = bufferStrategy
         .getDrawGraphics();
16
17   if (bufferStrategy.contentsLost())
18     clearCount = (isPageFlipping) 
           ? 2 : 1;
19   else {
20     boolean isBufferCleared = false;
21     if (clearCount > 0) {
22       clearCount--;
23       isBufferCleared = true;
24     }
25     listener.render(g, 
          isBufferCleared, bufferIndex);
26     bufferStrategy.show();
27     if (isPageFlipping)
28       bufferIndex = 
             (bufferIndex == 0) ? 1 : 0;
29   }
30   g.dispose();
31
32   // Compute the time remaining in 
33   // the frame period
33   long sleepTime = PERIOD 
       - stopWatch.stop()-overSleepTime;
34
35   // If there is time remaining, 
36   // sleep it away
37   stopWatch.start();
38   if (sleepTime >= 0L) {
39     Thread.sleep(
           sleepTime / 1000000L);
40     overSleepTime = stopWatch.stop() 
           - sleepTime;
41     stopWatch.start();
42   } else {
43
44     // Otherwise aggregate the excess
45     fractions -= sleepTime;
46     overSleepTime = 0L;
47
48     if (++noDelays 
           >= NO_DELAYS_PER_YIELD) {
49       Thread.yield();
50       noDelays = 0;
51     }
52   }
53
54   // While excess aggregate exceeds 
55   // frame period, update the model
56   int skips = 0;
57   while(fractions > PERIOD 
         && ++skips < maxFrameSkips) {
58     fractions -= PERIOD;
59     listener.updateModel();
60   }
61   while(fractions > PERIOD)
62     fractions -= PERIOD;
63   }
64 }

Listing 3

1  public class AnimationTest {
2
3    public static void main(
         String[] args) {
4    
5      // Enter full-screen mode
6      // and start animation
7      GraphicsSource.enterDisplayMode(
           640, 480);
8      GraphicsSource.setRenderListener(
           new FrameBuilderTest());
9      GraphicsSource.startAnimation(
           60);
10    
11     // wait for it to complete ...
12    
13     // Stop animation 
14     // and exit full-screen mode
15     GraphicsSource.stopAnimation();
16     GraphicsSource.exitDisplayMode();
17   }
18 }
19
20 class FrameBuilderTest 
       extends FrameBuilder {
21  
22   Image fighter, background;
23   int xf, yf, xb, yb;
24  
25   public void init() {
26     // Load the background image 
27     // and foreground sprite
28     background = ImageSource
           .getImage("background.gif");
29     fighter = ImageSource
           .getImage("fighter.gif");
30   }
31  
32   public void updateState() {
33     // update position of fighter 
34     // and background ... 
35   }
36  
37   public boolean isBackgroundSame() {
38     if (/* ... position of background 
           changed ... */)
39       return false;
40     else
41       return true;
42   }
43  
44   public void renderBackground(
         Graphics g) {
45     g.drawImage(
           background, xb, yb, null);
46   }
47  
48   public void renderBackground(
         Graphics g, int x, int y, 
             int width, int height) {
49     renderBackground(g);
50   }
51  
52   public void renderForeground(
         Graphics g) {
53     drawSprite(g, fighter, xf, yf);
54   }
55 }

Listing 4

1  public void audioTest() {
2    Audio fightSFX = AudioSource
         .getAudio("fight.wav");
3    fightSFX.setAudioListener(this);
4    fightSFX.play();
5   
6    // wait for it to finish ...
7    
8    fightSFX.dispose();
9  }
10  
11 public void audioStopped() {
12   // notify that sound is done ...
13 }

Additional Code For This Article (~ 132 KB ~zip format )
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.