• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

createImage() is null

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please does anyone have an explaination of why I can create an image to be used in double buffering in the render() method. thankyou

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;

public class GamePanel extends JPanel implements Runnable

public static final int GAME_WIDTH = 600;
public static final int GAME_HEIGHT = 600;
public static final int LEFT_PADDLE_FROM_LEFT = 10;
public static final int RIGHT_PADDLE_FROM_RIGHT = 10;

public static final int MOVE_PADDLE_UP = 1;
public static final int MOVE_PADDLE_DOWN = 2;
public static final int NO_MOVE = 0;

private int leftPaddleMovementStatus;
private int rightPaddleMovementStatus;

private Thread thread;
private boolean running = false;

private Ball ball;
private Paddle leftPaddle;
private Paddle rightPaddle;

//used in double duffering
private Image dbImage;
private Graphics dbg;
//used in game update to move paddles

public GamePanel()
this.setSize(WIDTH, HEIGHT);

//start the ball right in the center of the game
ball = new Ball(WIDTH/2 - Ball.DIAMETER/2, HEIGHT/2 - Ball.DIAMETER/2);

leftPaddle = new Paddle(LEFT_PADDLE_FROM_LEFT, GAME_HEIGHT/2 - Paddle.HEIGHT/2 );
rightPaddle = new Paddle(RIGHT_PADDLE_FROM_RIGHT, GAME_HEIGHT/2 - Paddle.HEIGHT/2);

thread = new Thread(this);

private void setUpListeners() {
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP ){
rightPaddleMovementStatus = MOVE_PADDLE_UP;
if(e.getKeyCode() == KeyEvent.VK_DOWN ){
rightPaddleMovementStatus = MOVE_PADDLE_DOWN;
if(e.getKeyCode() == KeyEvent.VK_W ){
leftPaddleMovementStatus = MOVE_PADDLE_UP;
if(e.getKeyCode() == KeyEvent.VK_S ){
leftPaddleMovementStatus = MOVE_PADDLE_DOWN;

public void run(){
running = true;
catch(InterruptedException e){};

public void gameUpdate(){
//check that paddle does not run of screen, if safe move the paddle
if(leftPaddle.getPoint().y > 0 && leftPaddle.getPoint().y < GAME_HEIGHT - Paddle.HEIGHT){

if(leftPaddleMovementStatus == MOVE_PADDLE_UP){
leftPaddle.getPoint().y += 1;
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
leftPaddle.getPoint().y -= 1;
if(rightPaddleMovementStatus == MOVE_PADDLE_UP){
rightPaddle.getPoint().y += 1;
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
rightPaddle.getPoint().y -= 1;
//reset status for next keylisten event
leftPaddleMovementStatus = NO_MOVE;
rightPaddleMovementStatus = NO_MOVE;

public void render(){

// this returns true

// this returns false
if(dbImage == null){
dbImage = createImage(GAME_WIDTH,GAME_HEIGHT);
if(dbImage == null){
System.out.println("error in making image");

dbg = dbImage.getGraphics();

dbg.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

int x = leftPaddle.getPoint().x;
int y = leftPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );

x = rightPaddle.getPoint().x;
y = rightPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );


public void paintToScreen(){
Graphics g = getGraphics();
g = dbg;
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that createImage() is going to return null until the component is actually on-screen. Doing lazy initialization of the image at the first call to paint() on the event thread would work, but because you're calling paint() yourself (which is rarely the great idea that people always think it is!) you don't actually have a good way to know when that will be, other than trying, sleeping, and trying again.

Moving to the Swing/AWT/SWT/JFace forum for any further discussion.
Nikos kat
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for you advice, i'll have a think about it
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't going to do what you think it will:

Notice, for example that the object returned from getGraphics is ignored.

I would suggest that you simplify the code -- there's no need to do this sort of double buffering, because Swing already does double buffering for you! Also, javax.swing.Timer is a better way to drive simple animation.
Tell me how it all turns out. Here is a tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic