• Advertisement
Sign in to follow this  

[java] BufferStrategy in applets?

This topic is 4592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

From a tutorial, I learned about BufferStrategy in normal windowed applications. Are they accessable in applets, and how so? I'm getting the following errors in my applet... cannot find symbol method createBufferStrategy(int) cannot find symbol method getBufferStrategy() Thanks.

Share this post


Link to post
Share on other sites
Advertisement
From the j2me documentation:

Here is an example of how buffer strategies can be created and used:


// Check the capabilities of the GraphicsConfiguration
...

// Create our component
Window w = new Window(gc);

// Show our window
w.setVisible(true);

// Create a general double-buffering strategy
w.createBufferStrategy(2);
BufferStrategy strategy = w.getBufferStrategy();

// Render loop
while (!done) {
Graphics g = strategy.getDrawGraphics();
// Draw to graphics
...
strategy.show();
}

// Dispose the window
w.setVisible(false);
w.dispose();

Share this post


Link to post
Share on other sites
Use java.awt.Canvas to obtain BufferStrategy and add the Canvas to Applet, some snippet code:

public class YourGame extends Applet implements ... {

BufferStrategy bufferStrategy;

public void start() {
if (running || finish) return;
running = true;

if (bufferStrategy == null) {
setIgnoreRepaint(true);

canvas.setIgnoreRepaint(true);
canvas.setSize(getSize().width, getSize().height);
canvas.addKeyListener(this);

setLayout(null);
add(canvas);
canvas.setLocation(0, 0);

// using buffer strategy as backbuffer
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();


// initialization game resources
.......
}

// start the game thread
new Thread(this).start();

canvas.requestFocus();
}



Hope this helps :)

Share this post


Link to post
Share on other sites
Thanks, however, now I'm getting this error with a blank black screen:
"java uses or overrides a deprecated API." Sample code:


import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.applet.*;
import java.awt.event.*;

public class Applet_Practice extends Applet implements KeyListener, Runnable
{
Image imgWalkDown1, imgWalkDown2, imgStand, imgStandRight, imgPC;
int x, y;
int size = 50;
boolean walkStatus = false;
public static int speed = 3;
public BufferStrategy strategy;
Canvas canvas;
boolean running = true;
Thread animator = null;

public void init()
{
canvas = new Canvas();
setBackground(Color.black);
addKeyListener(this);
imgWalkDown1 = getImage(getDocumentBase(), "C1_WalkDown1.png");
imgWalkDown2 = getImage(getDocumentBase(), "C1_WalkDown2.png");
imgStandRight = getImage(getDocumentBase(), "C1_StandRight.png");
imgStand = getImage(getDocumentBase(), "C1_Stand.png");

imgPC = imgStand;


}

public void start()
{
//if (running || finish) return;
//running = true;

if (strategy == null)
{
setIgnoreRepaint(true);

canvas.setIgnoreRepaint(true);
canvas.setSize(getSize().width, getSize().height);
canvas.addKeyListener(this);

setLayout(null);
add(canvas);
canvas.setLocation(0, 0);

// using buffer strategy as backbuffer
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();

// initialization game resources
}

// start the game thread
//new Thread(this).start();

canvas.requestFocus();

animator = new Thread();
animator.start();
}


public void stop()
{
animator = null;
animator.stop();
}

public void destroy()
{
}

public void run()
{
while (running)
{
paintWorld();
}
}

public void paintWorld()
{
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

g.setBackground(Color.WHITE);
g.drawImage(imgPC, x, y, size, size, this);

strategy.show();
}

public void keyReleased(KeyEvent ke)
{
imgPC = imgStand;
repaint();
}

public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();

switch (key)
{
case KeyEvent.VK_W:

y -= speed;
break;
case KeyEvent.VK_S:

if (walkStatus)
{
imgPC = imgWalkDown1;
walkStatus = false;
}
else
{
imgPC = imgWalkDown2;
walkStatus = true;
}

y += speed;
try
{
Thread.sleep(30);
} catch (InterruptedException e) { }
break;
case KeyEvent.VK_D:
imgPC = imgStandRight;
x += speed;
break;
case KeyEvent.VK_A:
imgPC = imgStand;
x -= speed;
break;
}

repaint();
}

public void keyTyped(KeyEvent ke)
{
}
}

Share this post


Link to post
Share on other sites
There is a compiler option that will give you information about any deprecated functions or classes that you are using I'd use that to find out what the exact function is and then determine if it is interfering with your graphics.

There is another way to implement a Double or Triple Buffering Solution in your code and that is simply to keep a separated Image as your back buffer draw all graphics to that instead of to the Applet or canvas directly and flip it at timed intervals. But I'm sure you probably alreay knew that. Cheers!

Share this post


Link to post
Share on other sites
I could be wrong, but I believe that if you use swing instead of awt (eg, use a JPanel instead of your canvas) it will be automatically double buffered. It works in normal Java, not sure about applets

Share this post


Link to post
Share on other sites
Quote:
Original post by dxFoo
Thanks, however, now I'm getting this error with a blank black screen:
"java uses or overrides a deprecated API."

The "deprecated API" doesn't have anything to do with either AWT or Swing. The method animator.stop() is inherently unsafe - see the Thread docs for more info.. This page describes how to stop a thread in a safe manner.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement