When we use Graphics2D and BufferedImage like the code bellow,
a black-colored squar is created. How can I set its background color?
I tried to use Graphics2D.setBackground(this.getBackground()) as
bellow, but it doesn't work. I just want to set the black color of the
Graphics2D obj to the default background color of JPanel. What did I
miss?
Thank you.
Lee
class imagePanel extends JPanel
{
...
public void paintComponent(Graphics g)
{
BufferedImage biKR = new BufferedImage(80, 120,
BufferedImage.TYPE_INT_RGB);
Graphics2D bigKR = biKR.createGraphics();
super.paintComponent(g);
bigKR.setBackground(this.getBackground()); /*#### this doesn't
work. The background color is still black ####*/
Graphics2D g2d = (Graphics2D)g;
bigKR.drawImage(image,0,0,this);
g2d.drawImage(biKR, null, x, y);
}
}
lee <lee_...@dell.com> a écrit dans le message :
99el9h$g7u$2...@galaxy.us.dell.com...
lee wrote:
> Hi,
> When we use Graphics2D and BufferedImage like the code bellow,
> a black-colored squar is created. How can I set its background color?
> I tried to use Graphics2D.setBackground(this.getBackground()) as
> bellow, but it doesn't work.
> I just want to set the black color of the
> Graphics2D obj to the default background color of JPanel. What did I
> miss?
> lass imagePanel extends JPanel
> {
[...]
...
Instead of creating the BufferedImage in the paintComponent()
it would be more efficient to do this before you call the method
itself.
> public void paintComponent(Graphics g)
> {
> BufferedImage biKR = new BufferedImage(80, 120,
> BufferedImage.TYPE_INT_RGB);
> Graphics2D bigKR = biKR.createGraphics();
What is that for an image? If you like to show an image as a
BufferedImage you should first have loaded one or draw
something yourself.
> bigKR.drawImage(image,0,0,this);
>}
The problem is that you didn't draw anything, thus you have
a black square which means that _nothing_ has been painted.
If you like to have a colored 2DRect you can draw one, eg:
Color myColor;
myColor = new Color(r,g,b);
myBuf = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
/* draw into memory buffer */
Graphics2D bi = (Graphics2D) myBuf.createGraphics();
/*Color-Rect */
bi.setPaint(myColor);
bi.drawRect(int, int, int,int);
g2.drawImage(myBuf, 0, 0, this);
Otherwise you should create a BI from the whole JPanel.
General information:
http://java.sun.com/j2se/1.3/docs/guide/2d/spec/j2d-image.fm3.html
Linda
--
God gave to you, now, you give to me,/I'd like to know
what you learned. The sky is blue and so is the sea.
What is the color, when black is burned? What is the color?
I am a child, Neil Young