...
However, a few issues will keep your image from being accelerated:
- If you constantly change the contents of the image (for example, drawing graphics primitves into the image), the image won`t be accelerated.
- As of Java SDK 1.4.1 translucent image aren`t accelerated. Only opaque and transparent images are accelerated. Because of this, translucent images are used sparingly in this book.
- Not every System has accelerated image capability
// image creation
VolatileImage vImg = createVolatileImage(w, h);
// rendering to the image
void renderOffscreen() {
do {
if (vImg.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
{
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
}
Graphics2D g = vImg.createGraphics();
//
// miscellaneous rendering commands...
//
g.dispose();
} while (vImg.contentsLost());
}
// copying from the image (here, gScreen is the Graphics
// object for the onscreen window)
do {
int returnCode = vImg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// Contents need to be restored
renderOffscreen(); // restore contents
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
renderOffscreen();
}
gScreen.drawImage(vImg, 0, 0, this);
} while (vImg.contentsLost());
public void createBackBuffer() {
GraphicsConfiguration.gc = getGraphicsConfiguration();
cImg = gc.createCompatibleVolatileImage(getWidth(),
getHeight());
}
public void renderFrame() {
}
public void paint (Graphics g)
{
createBuffer();
do {
GraphicsConfiguration gc =
this.getGraphicsConfiguration();
int valCode = vImg.validate(gc);
if(valCode==VolatileImage.IMAGE_INCOMPATIBLE){
createBackBuffer();
}
renderFrame();
g.drawImage(vImg, 0, 0, this);
} while(vImg.contentsLost())
g.setColor(Color.red);
g.fillOval(Ballx,Bally,10,10);
g.setColor(Color.gray);
g.fillRect(50,ypl-30,10,100);
g.fillRect(910,Cypl-30,10,100);
}
do{
}while( ... );
int w=970;
int h=580;
VolatileImage vImg = createVolatileImage(w, h);
public void renderOffscreen() {
do {
if (vImg.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
{
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
}
Graphics2D g = vImg.createGraphics();
g.dispose();
} while (vImg.contentsLost());
}
public void paint (Graphics g)
{
do {
int returnCode = vImg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// Contents need to be restored
renderOffscreen(); // restore contents
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
renderOffscreen();
}
g.drawImage(vImg, 0, 0, this);
} while (vImg.contentsLost());
g.drawImage(Ball,400,400,this);
g.setColor(Color.red);
g.fillOval(Ballx,Bally,10,10);
g.setColor(Color.gray);
g.fillRect(50,ypl-30,10,100);
g.fillRect(910,Cypl-30,10,100);
System.out.println("Paint aufgerufen");
}