All you have to do is "get rendering result" of one component and reuse it. That takes:
- intercept rendering process - override paint(Graphics) method
- create own image to render to
- render your image to original graphics
- render your image to other component - override paint(Graphics) method
First three steps can be done like this:
@Override
public void paint(Graphics originalGraphics) {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration c = e.getDefaultScreenDevice().getDefaultConfiguration();
//create own image to paint to
BufferedImage image = c.createCompatibleImage(getWidth(), getHeight());
Graphics2D reusableGraphics = image.createGraphics();
//let it paint into our graphics
super.paint(reusableGraphics);
// draw image on this component
originalGraphics.drawImage(image, 0, 0, null);
// draw image on other component
otherComponent.setMirrorImage(image);
}
In otherComponent you have to save image and paint it when required:
@Override
public void paint(Graphics g) {
if (mirroredImage == null) {
super.paintAll(g);
} else {
g.drawImage(mirroredImage, 0, 0, getWidth() * 3 / 4, getHeight() * 3 / 4, null);
}
}
public void setMirrorImage(BufferedImage mirroredImage) {
this.mirroredImage = mirroredImage;
repaint();
}
You can take a look here for full example