Double Buffering with 2 or more objects in JAVA
Hi, i'm working on to make a simple River Raid type game. I'm using double buffering for preventing flickin in one image however when i want to use another moving object, it goes crazy and both images start to flick. Because of making nearly more than 100 objects in a screen it seems a huge problem for my project.
Simply i have 2 questions and my code is below;
1) I need to prevent flicking in another images that i will add(as enemies)
2) Need to prevent flicking in my space ships bullets.I'm trying to do this by now. I need to draw an oval object with double buffering but it refuses as i said.
The code with one object that works perfectly(only a moving image with double buffering) is below, and the other one i tried to add a simple rectangle is under it.
Thanks for any help!
The one that works:
The one that i tried drawing moving rectangle
Simply i have 2 questions and my code is below;
1) I need to prevent flicking in another images that i will add(as enemies)
2) Need to prevent flicking in my space ships bullets.I'm trying to do this by now. I need to draw an oval object with double buffering but it refuses as i said.
The code with one object that works perfectly(only a moving image with double buffering) is below, and the other one i tried to add a simple rectangle is under it.
Thanks for any help!
The one that works:
Code:
import java.applet.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SpaceCrush extends JApplet implements Runnable,KeyListener
{
Image img;
int borderw=640;
int borderh=480;
int x;
int y=250;
Thread timer;
MediaTracker mt = new MediaTracker(this);
Image offscreenImage;
Graphics offscreenGraphics;
boolean left=false, right=false,up=false,down=false;
public void init() {
setBackground(Color.white);
setVisible(true);
addKeyListener(this);
img = getImage(getDocumentBase(),"batu.gif");
mt.addImage(img, 0);
offscreenImage = createImage(borderw,borderh);
offscreenGraphics = offscreenImage.getGraphics();
offscreenGraphics.drawImage(img, 10, 10, this);
}
public void paint(Graphics g) {
g.drawImage(offscreenImage,x,y,this);
}
public void shoot(int CordX,int CordY){
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left=true;
}
if (key == KeyEvent.VK_RIGHT)
{
right=true;
}
if (key == KeyEvent.VK_DOWN)
{
down=true;
}
if (key == KeyEvent.VK_UP)
{
up=true;
}
if(key== KeyEvent.VK_SPACE)
{
shoot(x,y);
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left=false;
}
if (key == KeyEvent.VK_RIGHT)
{
right=false;
}
if(key== KeyEvent.VK_SPACE)
{
shoot(x,y);
}
if (key == KeyEvent.VK_DOWN)
{
down=false;
}
if (key == KeyEvent.VK_UP)
{
up=false;
}
}
public void keyTyped(KeyEvent e)
{}
public void start()
{
if (timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void run() {
setSize(600,400);
while (true) {
try {Thread.currentThread().sleep(10);} catch (InterruptedException e){}
//saga sola gitme alengirleri
if(right==true)
x=x+10;
if(left==true)
x=x-10;
if(down==true)
y=y+10;
if(up==true)
y=y-10;
repaint();
}
}
}The one that i tried drawing moving rectangle
Code:
import java.applet.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SpaceCrush extends JApplet implements Runnable,KeyListener
{
Image img;
int borderw=640;
int borderh=480;
int x;
int y=250;
Thread timer;
Image offscreenImage;
Graphics offscreenGraphics;
boolean left=false, right=false,up=false,down=false;
Graphics bufferGraphics;
Image offscreen;
public void init() {
setBackground(Color.white);
setVisible(true);
addKeyListener(this);
img = getImage(getDocumentBase(),"batu.gif");
offscreenImage = createImage(borderw,borderh);
offscreenGraphics = offscreenImage.getGraphics();
offscreenGraphics.drawImage(img, 10, 10, this);
offscreen = createImage(borderw,borderh);
bufferGraphics = offscreen.getGraphics();
}
public void paint(Graphics g) {
g.drawImage(offscreenImage,x,y,this);
bufferGraphics.drawRect(x+10,x+10,20,20);
g.drawImage(offscreen,x+10,y+10,this);
}
public void shoot(int CordX,int CordY){
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left=true;
}
if (key == KeyEvent.VK_RIGHT)
{
right=true;
}
if (key == KeyEvent.VK_DOWN)
{
down=true;
}
if (key == KeyEvent.VK_UP)
{
up=true;
}
if(key== KeyEvent.VK_SPACE)
{
shoot(x,y);
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left=false;
}
if (key == KeyEvent.VK_RIGHT)
{
right=false;
}
if(key== KeyEvent.VK_SPACE)
{
shoot(x,y);
}
if (key == KeyEvent.VK_DOWN)
{
down=false;
}
if (key == KeyEvent.VK_UP)
{
up=false;
}
}
public void keyTyped(KeyEvent e)
{}
public void start()
{
if (timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void run() {
setSize(600,400);
while (true) {
try {Thread.currentThread().sleep(10);} catch (InterruptedException e){}
//saga sola gitme alengirleri
if(right==true)
x=x+10;
if(left==true)
x=x-10;
if(down==true)
y=y+10;
if(up==true)
y=y-10;
repaint();
}
}
}
To begin with your not using double-buffering in the applet with a moving rectangle.
When you override the paint function you should avoid any unnecessary drawing there.
For a proper double-buffering to work everything in the paint function must be drawn somewhere else(in the game loop).
Just send me a message if you need some more explanation.
When you override the paint function you should avoid any unnecessary drawing there.
For a proper double-buffering to work everything in the paint function must be drawn somewhere else(in the game loop).
Just send me a message if you need some more explanation.
I double buffer like this.
Code:
Image im = new Image(getSize().width, getSize().height);
Graphics g;
public void paint(Graphics g2) {
g = im.getGraphics();
g.clearRect(0, 0, width, height);
my_paint(g);
g2.drawImage(im, 0, 0, this);
}
public void myPaint(Graphics g) {
// draw a bunch of objects
}Sir, e^iπ + 1 = 0, hence God exists; reply!
I actually prefer using BufferStrategy(java.awt.image.BufferStrategy) for double buffering, something in the style of:
Normally I put the graphics part in it's own thread(only necessary in graphics intense applications(not pong)) running while the physics thread pauses or checks for collisions.
On the other hand I'm convinced that for game programming JOGL, SDL(tough they didn't have a mac port last time I checked) or any other OpenGL bind is to be preferred.
Code:
f1.createBufferStrategy(2);
BufferStrategy BF = f1.getBufferStrategy();
Graphics G = BF.getDrawGraphics();
G.drawALotOfStuff(:p);
BF.show();On the other hand I'm convinced that for game programming JOGL, SDL(tough they didn't have a mac port last time I checked) or any other OpenGL bind is to be preferred.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| iPhone/iPad Audio Glitch - Double Hiss/Static | reapz | 2 | 4,267 |
Dec 8, 2010 06:22 PM Last Post: reapz |
|
| Double-Buffering with NSOpenGLView | DesertPenguin | 3 | 4,798 |
Aug 1, 2006 07:17 AM Last Post: DesertPenguin |
|
| float/double vs GLfloat/double | maddanio | 4 | 5,718 |
Feb 5, 2003 05:40 PM Last Post: antadam |
|
| Double Buffer Speeds in Cocoa | psyba | 1 | 3,096 |
Jan 31, 2003 03:03 PM Last Post: OneSadCookie |
|

