-
Bug
-
Resolution: Fixed
-
P4
-
6
-
b53
-
x86
-
linux_redhat_9.0
Swing's painting code does the following check to avoid painting heavyweight children:
if (isLightweightComponent(comp)) {
... paint child ...
}
isLightweightComponent will return false if the component is a swing component but not parented to a heavy weight component. This means if you paint a containment hierarchy to an image only the component you invoke paint on will be painted.
Here's a test case:
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
public class PaintTest {
public static void main(final String[] args) throws Exception {
JPanel panel = new JPanel(new FlowLayout());
JButton b = new JButton("Button");
JLabel l = new JLabel("Label");
l.setOpaque(true);
JTree t = new JTree();
panel.add(b);
panel.add(l);
panel.add(t);
panel.setSize(panel.getPreferredSize());
panel.doLayout();
saveComponentImage(panel, "panel.jpg");
Window window = new JWindow();
window.add(panel);
window.pack();
saveComponentImage(panel, "panel2.jpg");
window.dispose();
}
private static void saveComponentImage(JComponent comp, String filename) throws Exception {
BufferedImage im = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = im.getGraphics();
comp.paint(g);
g.dispose();
ImageIO.write(im, "JPEG", new File(filename));
}
}
###@###.### 2005-1-07 17:05:03 GMT
if (isLightweightComponent(comp)) {
... paint child ...
}
isLightweightComponent will return false if the component is a swing component but not parented to a heavy weight component. This means if you paint a containment hierarchy to an image only the component you invoke paint on will be painted.
Here's a test case:
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
public class PaintTest {
public static void main(final String[] args) throws Exception {
JPanel panel = new JPanel(new FlowLayout());
JButton b = new JButton("Button");
JLabel l = new JLabel("Label");
l.setOpaque(true);
JTree t = new JTree();
panel.add(b);
panel.add(l);
panel.add(t);
panel.setSize(panel.getPreferredSize());
panel.doLayout();
saveComponentImage(panel, "panel.jpg");
Window window = new JWindow();
window.add(panel);
window.pack();
saveComponentImage(panel, "panel2.jpg");
window.dispose();
}
private static void saveComponentImage(JComponent comp, String filename) throws Exception {
BufferedImage im = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = im.getGraphics();
comp.paint(g);
g.dispose();
ImageIO.write(im, "JPEG", new File(filename));
}
}
###@###.### 2005-1-07 17:05:03 GMT