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: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?  Thanks, Mike/Icedust
|
|
|
|
|
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
| BufferStrategy myStrategy;
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);
Graphics2D g = (Graphics2D) myStrategy.getDrawGraphics(); |
|
|
|
|
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 
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
|
ReBirth
|
 |
«
Reply #4 - Posted
2012-01-02 01:06:36 » |
|
Put ra4king's code right below mine and you ready  Sorry ra4king, if there's medal then it's mine 
|
|
|
|
ra4king
|
 |
«
Reply #5 - Posted
2012-01-02 08:58:05 » |
|
Put ra4king's code right below mine and you ready  Sorry ra4king, if there's medal then it's mine  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  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). 
|
|
|
|
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? 
|
|
|
|
BoBear2681
|
 |
«
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  [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]
|
|
|
|
|
ra4king
|
 |
«
Reply #8 - Posted
2012-01-02 16:38:25 » |
|
pack() is such a lousy hack that no one should ever ever use it  @ReBirth ROFLMAO 
|
|
|
|
ReBirth
|
 |
«
Reply #9 - Posted
2012-01-02 16:48:57 » |
|
Well my combi of setVisible, setResizable and pack always work and no problem 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
BoBear2681
|
 |
«
Reply #10 - Posted
2012-01-02 17:22:56 » |
|
pack() is such a lousy hack that no one should ever ever use it  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).
|
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2012-01-02 18:25:13 » |
|
pack() is such a lousy hack that no one should ever ever use it  pack() is the best way to make a window exactly fit the layout.
|
|
|
|
ra4king
|
 |
«
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 
|
|
|
|
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?
|
|
|
|
|
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.
|
|
|
|
|