Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Fullscreen buffer problem  (Read 645 times)
0 Members and 1 Guest are viewing this topic.
Offline penkey

Junior Newbie





« Posted 2010-01-10 16:45:34 »

Hello

I am having having hard time figuring out why my whole screen flickers when I use bufferstrategy. The screen flickers whenever I move my character, and if i move him slowly I can see the page switching between default and black background. The thing is I haven't even set the background color.... isPageFlipping() returns true when i check it. Using 2 buffers when creating bufferstrategy.

The link required for the image to work:
http://i.imgur.com/WIpwY.png

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  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class GamePractice extends JFrame{

   final float SPEED;
   final float JUMP_SPEED;
   final float X_SPEED;
   final long PERIOD;
   final int NUM_BUFFERS;

   GamePanel mainPane;
   private GraphicsDevice gd;
   private BufferStrategy bufferStrategy;
   
   int jumpUpCount;
   int jumpDownCount;

   Action actionJumpKey;

   float xV;
   float yV;

   private volatile boolean running;
   boolean didPaint;
   boolean bufferStratReady;
   boolean moveRight;
   boolean moveLeft;
   boolean jumpDown;
   boolean isJumping;

   float totalFPS = 1;
   long fpscount1 = 0;
   long fpscount2 = 0;

   public GamePractice(){
      PERIOD = 16;
      NUM_BUFFERS = 2;
      SPEED = 15;
      JUMP_SPEED = 10;
      //PERIOD = 17000000L; //17ms
     X_SPEED = 6;
      jumpUpCount = 0;
      jumpDownCount = 0;
   }

   public static void main(String[] args) {
      GamePractice gp = new GamePractice();
      gp.go();

   }

   public void go(){

     
      getRootPane().setDoubleBuffered(false);
      setFocusTraversalKeysEnabled(false);
      initFullScreen();  
      mainPane = new GamePanel();

     
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     actionJumpKey = new ActionKeys("jump");
      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed SPACE"),"jump");
      ((JComponent)getContentPane()).getActionMap().put("jump",actionJumpKey);

      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed LEFT"),"moveLeft");
      ((JComponent)getContentPane()).getActionMap().put("moveLeft",new ActionKeys("moveLeft"));

      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("pressed RIGHT"),"moveRight");
      ((JComponent)getContentPane()).getActionMap().put("moveRight",new ActionKeys("moveRight"));

      //*************************************************************************************************************
     //*************************************************************************************************************

      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released LEFT"),"stopLeft");
      ((JComponent)getContentPane()).getActionMap().put("stopLeft",new ActionKeys("stopLeft"));

      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released RIGHT"),"stopRight");
      ((JComponent)getContentPane()).getActionMap().put("stopRight",new ActionKeys("stopRight"));
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     

      ((JComponent)getContentPane()).getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"),"quit");
      ((JComponent)getContentPane()).getActionMap().put("quit",new QuitAction());

      mainPane.startGame();

   }

   
   
   private void initFullScreen() {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      gd = ge.getDefaultScreenDevice();
      setUndecorated(true);
      setIgnoreRepaint(true);
      setResizable(false);

      if (!gd.isFullScreenSupported( )) {
         System.out.println("Full-screen exclusive mode not supported");
         System.exit(0);
      }
      gd.setFullScreenWindow(this);
      setDisplayMode(1920, 1080, 32);
     
      setBufferStrategy();

   }

   private void setDisplayMode(int w, int h, int d){
      DisplayMode dMode = new DisplayMode(w, h, d,
            DisplayMode.REFRESH_RATE_UNKNOWN);
      if(gd.isDisplayChangeSupported()){
         gd.setDisplayMode(dMode);
      }else{
         System.out.println("HAHA!");
         System.exit(0);
      }

   }

   private void setBufferStrategy(){
      { try {
         EventQueue.invokeAndWait( new Runnable( ) {
            public void run( )
            { createBufferStrategy(NUM_BUFFERS);  }
         });
      }
      catch (Exception e) {
         System.out.println("Error while creating buffer strategy");
         System.exit(0);
      }

      try {  
         Thread.sleep(500);

      }catch(InterruptedException ex){}

      bufferStrategy = getBufferStrategy( );
      bufferStratReady = true;
      //System.out.println(bufferStrategy.getCapabilities().isPageFlipping());
     
      }
   }
   /*class TimerLoop implements ActionListener{  
      public void actionPerformed(ActionEvent arg0) {
         mainPane.reDraw();
      }

   }*/


   class GamePanel {

      Player mainPlayer;
      BufferedImage image = null;
      int paintOnce;
      public GamePanel(){

         try {
            image = ImageIO.read(new File("Super Paper Mario_128.png"));

         } catch (IOException e) {
            e.printStackTrace();
         }
         mainPlayer = new Player(192, 822, image);
         paintOnce = 0;
      }

     
      public void startGame() {
         // TODO Auto-generated method stub
       
         //Thread loop = new Thread( new MainLoop());
        //loop.start();

         if (bufferStratReady){
            Timer t = new Timer( );
            t.scheduleAtFixedRate(new MainLoop() , 0, PERIOD);
            running = true;
         }

      }

      private void gameUpdate(){
         xV = 0;
         //yV = 0;
        if (isJumping){
            yV = JUMP_SPEED;  
            if(moveLeft) xV = -X_SPEED;//-SPEED + 4;
           if(moveRight) xV = X_SPEED;//SPEED - 4;
        } else {
            if(moveLeft) xV = -SPEED;
            if(moveRight) xV = SPEED;
         }
      }

      private void reDraw(){

         final float POSITION_X = mainPlayer.getX();
         final float POSITION_Y = mainPlayer.getY();
         final int IMAGE_W = mainPlayer.getWidth();
         final int IMAGE_H = mainPlayer.getHeight();
         didPaint = false;

         if (POSITION_X >= 1792){

            mainPlayer.setX(1791);      
         } else if (POSITION_X <= 0){

            mainPlayer.setX(1);
         } else {

            Graphics2D gScr;
            try {
               gScr = (Graphics2D) bufferStrategy.getDrawGraphics();
               
               if(paintOnce < 1){
                mainPlayer.paintImage(gScr);
                didPaint = true;
               }
               paintOnce = 1;
               
               if(xV != 0){
                  gScr.clearRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
                  //gScr.fillRect((int)POSITION_X, (int)POSITION_Y, IMAGE_W, IMAGE_H);
                 mainPlayer.setX(POSITION_X + xV);  
                  mainPlayer.paintImage(gScr);
                  didPaint = true;
               }


               if(jumpDown && jumpDownCount < 15){
                  didPaint = true;
                  gScr.clearRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W, IMAGE_H);
                  //gScr.fillRect((int)POSITION_X, (int)POSITION_Y - 7, IMAGE_W , IMAGE_H );
                 mainPlayer.setY(POSITION_Y + yV);
                  mainPlayer.paintImage(gScr);
                  if (jumpDownCount == 14){
                     jumpDown = false;
                     isJumping = false;
                     actionJumpKey.setEnabled(true);
                  }
                  jumpDownCount++;
               }

               if(isJumping && jumpUpCount < 15){
                  didPaint = true;
                  gScr.fillRect((int)POSITION_X, (int)POSITION_Y + 6, IMAGE_W, IMAGE_H);
                  mainPlayer.setY(POSITION_Y - yV);
                  mainPlayer.paintImage(gScr);
                  if (jumpUpCount == 14){
                     jumpDown = true;
                  }
                  jumpUpCount++;
               }
               
               gScr.dispose();

               if (!bufferStrategy.contentsLost() && didPaint)
                  bufferStrategy.show( );
               else
                  System.out.println("Contents Lost");

               Toolkit.getDefaultToolkit( ).sync( );
               
            }catch (Exception e){
               //System.out.println("Graphics context error: " + e);
              e.printStackTrace();
               running = false;
            }

           
         }

      }

   }

   
Offline penkey

Junior Newbie





« Reply #1 - Posted 2010-01-10 16:46:36 »

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  
class ActionKeys extends AbstractAction{

      private String description;

      public ActionKeys(String desC){
         description = desC;
      }

      public void changeDescription(String chngedDes){
         description = chngedDes;
      }

      public void actionPerformed(ActionEvent e) {

         if (description.equals("jump")){
   
            jumpDownCount = 0;
            jumpUpCount = 0;
            isJumping = true;
            actionJumpKey.setEnabled(false);
         } else if (description.equals("moveDown")){

         } else if (description.equals("moveLeft")){

            moveLeft = true;
         } else if (description.equals("moveRight")){

            moveRight = true;
         } else if (description.equals("stopLeft")){

            moveLeft = false;
         } else if (description.equals("stopRight")){

            moveRight = false;
         }

      }

   }

   class MainLoop extends TimerTask{


      @Override
      public void run() {
         // TODO Auto-generated method stub
        if(running){
            mainPane.gameUpdate();
            mainPane.reDraw();
         }
         /*if (totalFPS == 1){
            fpscount1 = System.currentTimeMillis();
            //System.out.println(System.currentTimeMillis());
         }
         if(totalFPS == 60){
            //System.out.println(totalFPS);
            fpscount2 = System.currentTimeMillis() - fpscount1;
            System.out.println(fpscount2);
            //System.out.println(System.currentTimeMillis());
            totalFPS = -1;
         }
         totalFPS++;*/

      }

   }

   class QuitAction extends AbstractAction{

      @Override
      public void actionPerformed(ActionEvent arg0) {
         // TODO Auto-generated method stub
        System.exit(0);
      }

   }
}

I am using Windows 7 jdk 6 update 18 beta
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!