Alright, I don't usually post on forums when I have an issue. I can usually get a fix or an answer to any problem with the holy reference guide (Google). I'm currently trying to get a bit of a game going in a java (later to be an applet) and have done so in the past... the only issue is that I can't get rid of this freaking flickering!!
I've tried double buffering with Images and BufferedImages, manually in the paint method by writing to a seperate offscreen image and overloading the update(Graphics g) function. Still flickered.
I've tried drawing one simple 32x32 tile and moving across the screen with this at a ridiculously slow frame rate (1 fps etc) - still manages to flicker.
My current incarnation of the program uses BufferStrategy (I've tried 2, 3, 4, and 5 buffers with this) but it STILL flickers. It seems as if whenever I call bufferStrat.show() - that call creates the flicker as it shows the image I've drawn to it. HELP!
Note: my world object's show method returns a BufferedImage object, and it works fine. currently in the buffPaint() method, though, I've taken out the part where it shows what world.show returns and just draws a few of the previously loaded images:
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.Scanner;
public class GApplet extends Canvas implements ActionListener
{
//graphics objects
Graphics2D graphics2D;
BufferStrategy bufferStrat;
static GraphicsConfiguration graphicConf;
BufferedImage imageList[];
//game objects
World gWorld;
Timer engine;
//game variables
int width = 640;
int height = 480;
int glob_msdelay = 50;
//temporary variable to get the images moving
float tmpx = 15;
public void buffPaint()
{
Scanner scan = new Scanner(System.in);
//g.drawImage(imageList[0], (int)tmpx, 0, this);
Graphics screen = bufferStrat.getDrawGraphics();
///do all drawing here gWorld.show(tmpx, 0, 13, true)
screen.drawImage(imageList[0], (int)tmpx, 0, this);
screen.drawImage(imageList[0], (int)tmpx, 32, this);
screen.drawImage(imageList[0], (int)tmpx, 64, this);
screen.drawImage(imageList[0], (int)tmpx, 96, this);
screen.drawImage(imageList[0], (int)tmpx, 128, this);
//////////////////////
// screen.dispose();
bufferStrat.show();
}
public void init()
{
engine = new Timer(glob_msdelay, this);
engine.setInitialDelay(0);
engine.setCoalesce(true);
}
public void start()
{
engine.start();
}
public void stop()
{
engine.stop();
}
public GApplet(GraphicsConfiguration theGraphicConf) throws IOException
{
//create frame
JFrame gFrame = new JFrame(graphicConf);
JPanel gPanel = (JPanel)gFrame.getContentPane();
setBounds(0, 0, width, height);
gPanel.setPreferredSize(new Dimension(width, height));
gPanel.setLayout(null);
gPanel.add(this);
gFrame.setBounds(0, 0, width, height);
gFrame.setVisible(true);
gFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
gFrame.setResizable(false);
//create graphics utilities
createBufferStrategy(2);
bufferStrat = getBufferStrategy();
requestFocus();
//timer initializing
engine = null;
//game initializations ================================================
graphicConf = theGraphicConf;
bufferStrat = getBufferStrategy();
System.out.println("Constructed");
//backBufferImage = graphicConf.createCompatibleImage(width, height, BufferedImage.TYPE_INT_ARGB);
//backBuffer = backBufferImage.createGraphics();
imageList = new BufferedImage[10];
//load images
for(int i = 0; i < 10; i++)
imageList[i] = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
//load all images HARDCODED
imageList[0] = ImageIO.read(new File("textures/grass1.png"));
imageList[1] = ImageIO.read(new File("textures/grass2.png"));
imageList[2] = ImageIO.read(new File("textures/grass3.png"));
//construct sprite first for default material
Sprite tmpSprite = new Sprite(3, 1);
tmpSprite.setImage(imageList[0], 0);
tmpSprite.setImage(imageList[1], 1);
tmpSprite.setImage(imageList[2], 2);
Material grass = new Material((Sprite)tmpSprite.clone(), 00, 00);
//create test world
gWorld = new World(100, 10, 100, (Material)grass.clone(), 640, 480);
}
//main loop
public void actionPerformed(ActionEvent e)
{
tmpx += 1;
buffPaint();
}
public static void main(String[] args) throws IOException
{
GApplet theGame = new GApplet(graphicConf);
theGame.init();
theGame.start();
}
}