createImage() null.

I finally make the progressBar using Graphics 2D.

The reason why I create ProgressBar using Graphics 2D is color changes in XP theme. which doesn't work.

What I want is :

show this progressbar added to Jpanel.

But, in JFrame, it shows but in JFrame added to JPanel, it doesn't show. I used createImage(int w, int h).I heard off-Image issue, which I don't understand. In Javadoc,

__

Returns:

an off-screen drawable image, which can be used for double buffering. The return value may be null if the component isnot displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.

_

So, what decides displayable or not. Further Graphics Enviornment.isHeadliess() -> true.

How can I make this isHeadless -> false so that it can be displayable so that the progressBar I draw using createImage can be shown.

Please let me know. This is a bazaar phenomenon.

__

This is code for the test.

package Daemon;

import java.awt.*;

import javax.swing.JPanel;

class HIextends Canvas

{

ImagedrawImg;

GraphicsdrawGr;

intxprev, yprev;

publicvoid paint(Graphics g)

{

update(g);

}

publicvoid 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);

}

publicboolean mouseDown(Event evt,int x,int y)

{

xprev = x;

yprev = y;

returntrue;

}

publicboolean mouseDrag(Event evt,int x,int y)

{

drawGr.drawLine(xprev, yprev, x, y);

xprev = x;

yprev = y;

repaint();

returntrue;

}

publicstaticvoid main(String args[]){

Frame f =new Frame();

// step 1. on top of panel

//JPanel thePanel = new JPanel();

//thePanel.add(new HI());

//f.add(thePanel);

// step 2. not on top of panel

f.add(new HI());

f.setSize(100,100); f.show();

}

}

step 1. doesn't work

step 2. does work.

It is so strange why...

Message was edited by:

PaulSheldonII_Screen

[3811 byte] By [PaulSheldonII_Screena] at [2007-11-27 2:21:36]
# 1

Don't use AWT components in a Swing application. Canvas in an AWT component.

You should be using JPanel and override the paintComponent(..) method for custom painting.

Don't override the mouseDown() and mouseDragged() methods. You should be using MouseListeners for this.

This posting shows how to draw on a BufferImage:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=607073

camickra at 2007-7-12 2:24:28 > top of Java-index,Desktop,Core GUI APIs...