File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes createImage() is null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "createImage() is null" Watch "createImage() is null" New topic

createImage() is null

Nikos kat

Joined: Jan 20, 2006
Posts: 20
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;
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

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.

[Jess in Action][AskingGoodQuestions]
Nikos kat

Joined: Jan 20, 2006
Posts: 20
thanks for you advice, i'll have a think about it
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.

There is no emoticon for what I am feeling!
It is sorta covered in the JavaRanch Style Guide.
subject: createImage() is null