> 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 ?
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);
}
}
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)
> 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...
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 );
}
}
}
}