Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  BufferStrategy problems  (Read 1313 times)
0 Members and 1 Guest are viewing this topic.
Offline icedust

Junior Newbie





« Posted 2012-01-01 04:59:07 »

Hi, i'm new to these forums & game programming. 

I decided to try using BufferStrategy, but ran across some problems.

GameBoard.java:

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  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JPanel;

public class GameBoard extends JPanel implements Runnable
{
   private BufferStrategy myBufferStrategy;
   private Graphics g;
   private Thread t;
   
   private final int PERIOD = 10;
   
   private Ball ball;
   
   public GameBoard()
   {
      ball = new Ball();
      addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e)
         {
            int keyCode = e.getKeyCode();
            if (keyCode == e.VK_LEFT)
               ball.keyPressed(e);
            if (keyCode == e.VK_RIGHT)
               ball.keyPressed(e);
            if (keyCode == e.VK_UP)
               ball.keyPressed(e);
            if (keyCode == e.VK_DOWN)
               ball.keyPressed(e);
         }
         @Override
         public void keyReleased(KeyEvent e)
         {
            int keyCode = e.getKeyCode();
            if (keyCode == e.VK_LEFT)
               ball.keyReleased(e);
            if (keyCode == e.VK_RIGHT)
               ball.keyReleased(e);
            if (keyCode == e.VK_UP)
               ball.keyReleased(e);
            if (keyCode == e.VK_DOWN)
               ball.keyReleased(e);
         }
      });
   }
   
   public void addNotify()
   {
      super.addNotify();
      startGame();
   }
   
   public void startGame()
   {
      t = new Thread(this);
      t.start();
   }
   
   public void updateGameState()
   {
      ball.move();
   }

   private void render()
   {
      g = myBufferStrategy.getDrawGraphics();
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, 500, 400);
      g.dispose();
      myBufferStrategy.show();
   }


   public void draw()
   {
      g.setColor(Color.red);
      g.fillOval(ball.x, ball.y, 50, 50);
   }
   
   @Override
   public void run()
   {
      while (true)
      {
         long beforeTime = System.currentTimeMillis();
         
         updateGameState();
         render();
         draw();
         
         long passedTime = System.currentTimeMillis() - beforeTime;
         long sleepTime = PERIOD - passedTime;
         if (sleepTime <= 0) sleepTime = 5;
         try
         {
            Thread.sleep(sleepTime);
         } catch (InterruptedException e) { }
      }
   }
}


The error:
Quote
Exception in thread "Thread-3" java.lang.NullPointerException
   at se.keisu.gamedev.GameBoard.render(GameBoard.java:72)
   at se.keisu.gamedev.GameBoard.run(GameBoard.java:94)
   at java.lang.Thread.run(Thread.java:680)

Any idea's what is wrong? Clueless

Thanks,
Mike/Icedust
Offline ReBirth
« Reply #1 - Posted 2012-01-01 05:52:40 »

Your "myBufferStrategy.getDrawGraphics()" return null object so the "g" was not initted. Why? because your myBufferStrategy itself was also null. You should get the BufferStrategy first from a container, like Canvas, then obtain Graphics object from it.

For example
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//global variable
BufferStrategy myStrategy;

//init/constructor
JFrame container = new JFrame("my game");
JPanel myJPanel = (JPanel) container.getContentPane();
Canvas myCanvas = new Canvas();
myCanvas.setIgnoreRepaint(true);
myCanvas.requestFocus();
myCanvas.createBufferStrategy(2);
myStrategy = getBufferStrategy();
myJPanel.add(myCanvas);

//render/draw method
Graphics2D g = (Graphics2D) myStrategy.getDrawGraphics();

Offline icedust

Junior Newbie





« Reply #2 - Posted 2012-01-01 17:17:53 »

Thanks.

Seems I got to read some more about BufferStrategy. I noticed another error, I disposed of graphics before I was done using them.. Embarassing  Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-01-01 17:19:25 »

Oh no that's what you are supposed to do with the Graphics object Tongue

1  
2  
3  
4  
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
//drawing code
g.dispose();
strategy.show();


Also, it is best to follow BufferStrategy's JavaDoc on how to properly set up the rendering code.

Offline ReBirth
« Reply #4 - Posted 2012-01-02 01:06:36 »

Put ra4king's code right below mine and you ready Pointing
Sorry ra4king, if there's medal then it's mine Tongue

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #5 - Posted 2012-01-02 08:58:05 »

Put ra4king's code right below mine and you ready Pointing
Sorry ra4king, if there's medal then it's mine Tongue
Unfortunately, your code doesn't work. You have to add the canvas and make the frame visible before you can call canvas.createBufferStrategy(2) or else you will get an IllegalStateException Wink

Also, it is pointless to put the Canvas in a JPanel. Just put it directly on the JFrame's contentPane (which in itself is a JPanel). Tongue

Offline ReBirth
« Reply #6 - Posted 2012-01-02 13:44:22 »

1  
Unfortunately, your code doesn't work.

both of us already know that's pseudocode. Why make it hard? I named it myStrategy and yours is strategy, it won't work from to begin. It's my first time to answer faster than you so may I please? Cool

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #7 - Posted 2012-01-02 14:51:27 »

You have to add the canvas and make the frame visible before you can call canvas.createBufferStrategy(2) or else you will get an IllegalStateException Wink

[nitpick]
This isn't quite true.  All you have to do is the Canvas displayable displayable (call pack() on the parent window, for example) to call Canvas.createBufferStrategy().  It doesn't actually have to be visible on the screen.
[/nitpick]
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #8 - Posted 2012-01-02 16:38:25 »

pack() is such a lousy hack that no one should ever ever use it Smiley

@ReBirth ROFLMAO Pointing

Offline ReBirth
« Reply #9 - Posted 2012-01-02 16:48:57 »

Well my combi of setVisible, setResizable and pack always work and no problem Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #10 - Posted 2012-01-02 17:22:56 »

pack() is such a lousy hack that no one should ever ever use it Smiley

How so?  It allows your window to be sized properly for any localization, desktop settings, etc.  I would argue that nobody should ever use setSize() (talking GUI's here, not necessarily games).
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2012-01-02 18:25:13 »

pack() is such a lousy hack that no one should ever ever use it Smiley
pack() is the best way to make a window exactly fit the layout.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #12 - Posted 2012-01-02 18:29:42 »

I'm talking about using it to bypass the setVisible(true) thing!

Of course pack() is excellent for GUIs Tongue

Offline icedust

Junior Newbie





« Reply #13 - Posted 2012-01-02 20:25:00 »

thank you so much for all the replies, I have gotten it working.

I have another problem, though.. Sometimes KeyEvents are slow to respond, or dont respond at all, this happens most often with multiple key presses, it sometimes acts as if I released key even though i didn't. Don't know how to explain it exactly. Anyway, any ideas what could be wrong?
Offline ReBirth
« Reply #14 - Posted 2012-01-03 10:59:55 »

@OP
well I have similiar keyListener as yours, but there're differences. Mine uses boolean to store a key's state and "else" to break the checking sooner. I think you should use boolean.

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.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (25 views)
2014-04-14 17:35:47

Porlus (42 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (223 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!