javaQQ
June 11th, 2006, 04:43 PM
I am trying to draw an image of one JPanel onto another JPanel, using the code below.
Instead of a picture of the source JPanel, i get only a white rectangle on the target JPanel.
I would greatly appreciate any help on fixing my code.
A JPanel, "sourcePanel" exists.
...
GraphicsConfiguration gfxConfig =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage sourcePanelImage =
gfxConfig.createCompatibleImage(sourcePanel.getWidth(),
sourcePanel.getHeight());
Graphics2D sourceGraphics =
(Graphics2D) sourcePanelImage.getGraphics();
sourcePanel.paint (sourceGraphics);
TargetPanel targetPanel = new TargetPanel( sourcePanelImage.getWidth(), sourcePanelImage.getHeight(), sourcePanelImage );
PopFrame popFrame2 = new PopFrame( targetPanel, sourcePanelImage.getWidth(), sourcePanelImage.getHeight() );
...
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
class TargetPanel extends JPanel {
int width;
int height;
BufferedImage image;
public TargetPanel( int width, int height, BufferedImage image ) {
this.width = width;
this.height = height;
this.image = image;
setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
setPreferredSize( new Dimension( width, height ) );
setMinimumSize( new Dimension( width, height ) );
setMaximumSize( new Dimension( width, height ) );
setBackground( Color.orange );
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, borders
g.drawImage( image, 0, 0, width, height, this );
}
}
...
Println's show that the image exists:
"sourcePanelImage.getWidth() = 170
sourcePanelImage.getHeight() = 180
sourcePanelImage = BufferedImage@b4ee5e: type = 3 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 170 height = 180 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0
"
but the result is only a white rectangle on "targetPanel"
Many thanks in advance for a solution.
Instead of a picture of the source JPanel, i get only a white rectangle on the target JPanel.
I would greatly appreciate any help on fixing my code.
A JPanel, "sourcePanel" exists.
...
GraphicsConfiguration gfxConfig =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage sourcePanelImage =
gfxConfig.createCompatibleImage(sourcePanel.getWidth(),
sourcePanel.getHeight());
Graphics2D sourceGraphics =
(Graphics2D) sourcePanelImage.getGraphics();
sourcePanel.paint (sourceGraphics);
TargetPanel targetPanel = new TargetPanel( sourcePanelImage.getWidth(), sourcePanelImage.getHeight(), sourcePanelImage );
PopFrame popFrame2 = new PopFrame( targetPanel, sourcePanelImage.getWidth(), sourcePanelImage.getHeight() );
...
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
class TargetPanel extends JPanel {
int width;
int height;
BufferedImage image;
public TargetPanel( int width, int height, BufferedImage image ) {
this.width = width;
this.height = height;
this.image = image;
setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
setPreferredSize( new Dimension( width, height ) );
setMinimumSize( new Dimension( width, height ) );
setMaximumSize( new Dimension( width, height ) );
setBackground( Color.orange );
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, borders
g.drawImage( image, 0, 0, width, height, this );
}
}
...
Println's show that the image exists:
"sourcePanelImage.getWidth() = 170
sourcePanelImage.getHeight() = 180
sourcePanelImage = BufferedImage@b4ee5e: type = 3 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 170 height = 180 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0
"
but the result is only a white rectangle on "targetPanel"
Many thanks in advance for a solution.