JDK-4066665 : createImage returns always null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.1,1.1.3
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1997-07-23
  • Updated: 1997-12-08
  • Resolved: 1997-12-08
Description

Name: rlT66838			Date: 07/23/97


calling createImage(int,int) of the Component
class always returns null instead of the 
requested image

======================================================================

In the following code, the call to createImage returns NULL and
the next line of code raises a java.lang.NullPointerException.  This happens 
on Solaris and NT 4.0.  Please look at the constructor for ScribbleCanvas.


class ScribbleCanvas extends Canvas
{
  Image		drawImg;
  Graphics	drawGr;
  int		xprev, yprev;
  
  public void paint(Graphics g)
  {
    g.drawImage(drawImg, 0, 0, null);
  }

  public ScribbleCanvas()
  {
     drawImg	= createImage(100,100);
     drawGr	= drawImg.getGraphics();
     setBackground(Color.gray);
     setSize(300,300);
  }

  public boolean mouseDown(Event evt, int x, int y)
  {
    xprev = x;
    yprev = y;

    return true;
  }

  public boolean mouseDrag(Event evt, int x, int y)
  {
     drawGr.drawLine(xprev, yprev, x, y);
     xprev = x;
     yprev = y;
  
     repaint();
     return true;
  }
}



joon.oh@Eng 1997-09-29

Comments
EVALUATION From the Java tutorial on java.sun.com, when double buffering, "You might wonder why the offscreen image and graphics context are created in the update() method, instead of in (for example) the start() method. The reason is that the image and graphics context depend on the size of the applet Panel's drawing area, and the size of any Component's drawing area is not valid until the Component is just about to be drawn to the screen for the first time." I'm not sure that that makes sense. Here is a snippit of createImage from src/java/awt/Component.java: public Image createImage(int width, int height) { ComponentPeer peer = this.peer; if (peer instanceof java.awt.peer.LightweightPeer) { return parent.createImage(width, height); } else { return (peer != null) ? peer.createImage(width, height) : null; } } In the original code (see description) the peer was not created at the time the createImage call was issued, implying that the image could not be created, thus the null return. Re-arranging the sample code to the following produces the desired results: import java.awt.*; class ScribbleCanvas extends Canvas { Image drawImg; Graphics drawGr; int xprev, yprev; public void paint(Graphics g) { update(g); } public void update(Graphics g) { Dimension d = size(); if( drawImg == null ) { drawImg = createImage(100,100); drawGr = drawImg.getGraphics(); setBackground(Color.gray); } g.drawImage(drawImg, 0, 0, null); } public boolean mouseDown(Event evt, int x, int y) { xprev = x; yprev = y; return true; } public boolean mouseDrag(Event evt, int x, int y) { drawGr.drawLine(xprev, yprev, x, y); xprev = x; yprev = y; repaint(); return true; } public static void main(String args[]) { Frame f = new Frame(); f.add(new ScribbleCanvas()); f.setSize(100,100); f.show(); } }
11-06-2004

WORK AROUND Name: rlT66838 Date: 07/23/97 ======================================================================
11-06-2004