Copy Graphics into an image

Hi all,I want to copy the Graphics g (in the method paint(g)) into an Image. How can I do this ? In another words, I'd like to make a copy what I'm drawing in an Image object to draw it in another panel.Thx for advises.
[242 byte] By [dams@boulota] at [2007-10-2 5:58:31]
# 1
How about drawing to an image first (use Component.createImage(), then getting the graphics of that image to use to draw using Image.getGraphics() ), when paint comes around just draw the image (Graphics.drawImage() ).
sierratecha at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 2

> How about drawing to an image first (use

> Component.createImage(), then getting the graphics of

> that image to use to draw using Image.getGraphics()

> ), when paint comes around just draw the image

> (Graphics.drawImage() ).

There's something I misunderstood, using the graphics of the image... I tried this solution : I have 2 panels, PanelA and PanelB. I want to display a part of PanelA in PanelB using an image.

So in PanelA, I should have something like this:

class PanelA extends JPanel {

...

paint(graphics g) {

... // draw my scene

Image copy = this.createComponent(areaWidth, areaHeight);

panelB.setImahe(copy);

}

}

And PanelB shoud be:

class PanelB extends JPanel {

...

paint(graphics g) {

super.paint(g);

g.drawImage(image);

}

}

But it doesn't work, it displays a white screen in panel B ! Does any body have an idea ?

dams@boulota at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 3

Try drawing to the image rather than the component:

class PanelA extends JPanel {

...

paint(graphics g) {

Image copy = this.createImage(areaWidth, areaHeight);

Graphics graphics = copy.getGraphics();

... // draw your scene using calls to graphics and not g

panelB.setImahe(copy);

}

}

And PanelB shoud be:

class PanelB extends JPanel {

...

paint(graphics g) {

super.paint(g);

g.drawImage(image);

}

}

sierratecha at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 4
> ... // draw your scene using calls to graphicsDo you mean something like this :panelB.paint(graphics);When I do this, it draws PanelB in my PanelA !
dams@boulota at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 5

What I meant was, using the variables I used in my post above, make the call graphics.fillRect() instead of g.fillRect(x) or graphics.frameRect(x) versus g.fillRect(x) (or whatever functions you are using to draw to the graphics object). Then you actually draw to the image graphics (variable graphics) and not the panel graphics (variable g)

sierratecha at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 6

> What I meant was, using the variables I used in my

> post above, make the call graphics.fillRect() instead

> of g.fillRect(x) or graphics.frameRect(x) versus

> g.fillRect(x) (or whatever functions you are using to

> draw to the graphics object). Then you actually draw

> to the image graphics (variable graphics) and not the

> panel graphics (variable g)

I'm dumb, I don't still understand...

In my mind, using graphics instead of g in panel b won't display the image of panel A in panel B... (and PanelB has only to draw the image of PanelA).

I'll tried to dev an example, It should be easier to discuss...

dams@boulota at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...
# 7

Take a look at this code...I set the layout to null so you can better see the panels and their dimensions

import java.applet.*;

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

public class ImageDraw extends Applet{

PanelA a;

PanelB b;

Image image;

public void init(){

a = new PanelA();

b = new PanelB();

setLayout(null)

a.setBounds(0, 0, 200, 200 );

b.setBounds(200, 0, 200, 200 );

add(a);

add(b);

image = a.createImage(200, 200 );

}

public void paint( Graphics g ){

super.paint(g);

a.repaint();

b.repaint();

}

class PanelA extends JPanel{

public void paint( Graphics gr ){

Graphics2D g = (Graphics2D)gr;

Graphics2D graphics = (Graphics2D)image.getGraphics();

graphics.fillRect( 20, 20, 40, 40 );

g.drawImage( image, new AffineTransform(), this );

g.drawString( "PanelA", 100, 100 );

}

class PanelB extends JPanel{

public void paint( Graphics gr){

Graphics2D g = (Graphics2D)gr;

g.drawImage( image, new AffineTransform(), this );

g.drawString( "PanelB", 100, 100 );

}

}

}

}

sierratecha at 2007-7-16 2:07:26 > top of Java-index,Security,Cryptography...