Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Properly (?) Double Buffered Applet is Really Choppy  (Read 661 times)
0 Members and 1 Guest are viewing this topic.
Offline CrumbBread

Junior Newbie





« Posted 2006-06-30 22:23:02 »

Hi all. I looked around here for the proper way to double buffer an applet, and I think I've got it, but it's realllly choppy when it runs. (It's a little 2d jump 'n run game with not much in it yet). If you have any idea why, please tell =) Thanks!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
public class Game extends JApplet implements KeyListener, Runnable
{  
   //doublebuffer stuff
   Graphics bufferGraphics;
   Image offscreenImage;
   Dimension dim;
   
   //constants
   int SCREEN_WIDTH = 800;
   int SCREEN_HEIGHT = 600;
   int SPRITE_SIZE = 64;
   Color BGColor = Color.white;
   int PLAYER_SPEED_X = 10;
   int PLAYER_JUMP_SPEED = 20;
   int GRAVITY_ACCEL = 1;
   
   //directional constants
   int LEFT = -1;
   int RIGHT = 1;
   int STOPPED = 0;
   int UP = -1;
   int DOWN = 1;
   
   //images
   Image playerImage;
   
   //movement
   boolean freeFall = true;
   int playerDirX = STOPPED;
   int playerVelY = 0;
   int playerPosX = 0;
   int playerPosY = 0;
   
   //game control variables
   boolean gameIsRunning = false;
   Thread t;
   
   int i = 0;
   
   public void init()
   {
      addKeyListener(this);
      gameIsRunning = true;
     
      //prepare offscreen image for double buffering
      setSize(800,600);
      dim = getSize();
      offscreenImage = createImage(dim.width,dim.height);
      bufferGraphics = offscreenImage.getGraphics();
     
      playerImage = getImage(getDocumentBase(),"images/playerRight.gif");
     
      t = new Thread(this);
      t.start();
   }
   
   public void run()
   {
      while(gameIsRunning)
      {
         try
         {
            t.sleep(33);
         }
         catch (InterruptedException e) {}
         
         playerPosX += playerDirX*PLAYER_SPEED_X;
         playerPosY -= playerVelY;
         
         if(freeFall)
         {
            playerVelY -= GRAVITY_ACCEL;
         }        
         
         //wraparound
         if(playerPosX < 0)
         {
            playerPosX = 0;
         }
         if(playerPosX > SCREEN_WIDTH - SPRITE_SIZE)
         {
            playerPosX = SCREEN_WIDTH - SPRITE_SIZE;
         }
         
         //stop at the floor
         if(playerPosY > SCREEN_HEIGHT - SPRITE_SIZE)
         {
            freeFall = false;
            playerVelY = 0;
            playerPosY = SCREEN_HEIGHT - SPRITE_SIZE;
         }
         
         repaint();
      }
   }
   
   public void keyPressed(KeyEvent e)
   {      
      //move right
      if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
            e.getKeyCode() == KeyEvent.VK_D)
      {
         playerImage = getImage(getDocumentBase(),"images/playerRight.gif");
         playerDirX = RIGHT;
      }
     
      //move left
      if(e.getKeyCode() == KeyEvent.VK_LEFT ||
            e.getKeyCode() == KeyEvent.VK_A)
      {
         playerImage = getImage(getDocumentBase(),"images/playerLeft.gif");
         playerDirX = LEFT;
      }
     
      //jump
      if(e.getKeyCode() == KeyEvent.VK_SPACE ||
            e.getKeyCode() == KeyEvent.VK_W ||
            e.getKeyCode() == KeyEvent.VK_UP)
      {
         //allow jumping if we're standing on a surface
         if(playerPosY == SCREEN_HEIGHT - SPRITE_SIZE ||
               playerPosX == 0 ||
               playerPosX == SCREEN_WIDTH - SPRITE_SIZE)
         {
            if(playerPosX == 0)
            {
               playerDirX = RIGHT;
               playerImage =
                  getImage(getDocumentBase(),"images/playerRight.gif");
            }
            if(playerPosX == SCREEN_WIDTH - SPRITE_SIZE)
            {
               playerDirX = LEFT;
               playerImage =
                  getImage(getDocumentBase(),"images/playerLeft.gif");
            }
            freeFall = true;
            playerVelY = PLAYER_JUMP_SPEED;
         }
      }
   }
   
   public void keyReleased(KeyEvent e)
   {
      //stop moving right
      if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
            e.getKeyCode() == KeyEvent.VK_D)
      {
         if(playerDirX == RIGHT)
         {
            playerDirX = STOPPED;
         }
      }
     
      //stop moving left
      if(e.getKeyCode() == KeyEvent.VK_LEFT ||
            e.getKeyCode() == KeyEvent.VK_A)
      {
         if(playerDirX == LEFT)
         {
            playerDirX = STOPPED;
         }
      }
   }
   
   public void keyTyped(KeyEvent e)
   {
   }
   
   public void paint(Graphics g)
   {
      //clear screen
      bufferGraphics.setColor(Color.white);
      bufferGraphics.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
     
      //draw the player
      bufferGraphics.drawImage(playerImage,playerPosX,playerPosY,this);
     
      g.drawImage(offscreenImage,0,0,this);
   }
   
   public void stop()
   {
      t.stop();
   }
   
    public void update(Graphics g)
    {
         paint(g);
    }
}
Offline Andrew

Senior Newbie




Java games rock!


« Reply #1 - Posted 2006-07-01 07:29:17 »

That's the java 1.1 of doing it. (which is good if you want maximum compatibility). If you don't mind your users needing a more recent version of java then you might well get better performance by using a BufferStrategy which lets you do the double buffering with hardware acceleration.

do a google search for:
java double buffering buffer strategy

the first result you get seems quite a good explanation of how to do double buffering using a buffer strategy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!