Drawing Buffered Images

C

Chase Preuninger

BufferedImage img = new BufferedImage(50 * text.length(), 100,

BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = img.createGraphics();
g.drawRect(0, 0, img.getWidth()- 10, img.getHeight() -10);
g.setBackground(Color.WHITE);


Why is the background still black?
 
M

Mark Space

Chase said:
BufferedImage img = new BufferedImage(50 * text.length(), 100,

BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = img.createGraphics();
g.drawRect(0, 0, img.getWidth()- 10, img.getHeight() -10);
g.setBackground(Color.WHITE);


Why is the background still black?


Did you read the manual?

"public abstract void setBackground(Color color)

Sets the background color for the Graphics2D context. The
background color is used for clearing a region. When a Graphics2D is
constructed for a Component, the background color is inherited from the
Component. Setting the background color in the Graphics2D context only
affects the subsequent clearRect calls and not the background color of
the Component. To change the background of the Component, use
appropriate methods of the Component. "
 
K

Knute Johnson

Mark said:
Did you read the manual?

"public abstract void setBackground(Color color)

Sets the background color for the Graphics2D context. The background
color is used for clearing a region. When a Graphics2D is constructed
for a Component, the background color is inherited from the Component.
Setting the background color in the Graphics2D context only affects the
subsequent clearRect calls and not the background color of the
Component. To change the background of the Component, use appropriate
methods of the Component. "

But you probably shouldn't use Graphics.clearRect() any more because
"Beginning with Java 1.1, the background color of offscreen images may
be system dependent. Applications should use setColor followed by
fillRect to ensure that an offscreen image is cleared to a specific color."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top