iDevGames Forums
Double Buffering with 2 or more objects in JAVA - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Double Buffering with 2 or more objects in JAVA (/thread-4854.html)



Double Buffering with 2 or more objects in JAVA - batuhanu - Oct 30, 2005 05:12 PM

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:
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();
        }
        
    }
}



Double Buffering with 2 or more objects in JAVA - Tools10 - Oct 31, 2005 05:12 AM

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.


Double Buffering with 2 or more objects in JAVA - batuhanu - Nov 25, 2005 01:19 PM

i'm aware of that i'm late, but thanks...


Double Buffering with 2 or more objects in JAVA - unknown - Nov 25, 2005 03:27 PM

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
}



Double Buffering with 2 or more objects in JAVA - Tools10 - Nov 26, 2005 06:24 AM

I actually prefer using BufferStrategy(java.awt.image.BufferStrategy) for double buffering, something in the style of:
Code:
f1.createBufferStrategy(2);
BufferStrategy BF = f1.getBufferStrategy();
Graphics G = BF.getDrawGraphics();
G.drawALotOfStuff(:p);
BF.show();
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.