我正在尝试将 JScrollPane(大于屏幕)的主视口和标题保存到 PNG 图像文件中。我创建了 3 个扩展 JPanel 的类(MainTablePanel、MapsHeaderPanel 和 ItemsHeaderPanel)并将它们设置为视口。他们每个人都有这个方法:
public BufferedImage createImage() {
BufferedImage bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
paint(g);
g.dispose();
return bi;
}
每个类还有一个paint方法,它绘制背景然后调用super.paint()来绘制一些标签。例如:
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(new Color(255, 255, 0, 50));
// for loop that paints some vertical yellow lines
for(int i=0; i<getWidth(); i+=K.mW){
g.fillRect(i-1, 0, 2, getHeight());
if(i%(K.mW*5)==0){
g.fillRect(i-2, 0, 4, getHeight());
}
}
// called to pain some rotated JLabels
super.paint(g);
}
然后,我从外部 JFrame 尝试使用以下代码将它们保存到 PNG 文件:
BufferedImage tableImg = mainTableP.createImage();
BufferedImage topImg = mapsHeaderP.createImage();
BufferedImage leftImg = itemsHeaderP.createImage();
ImageIO.write(tableImg, "png", new File(s.homeDir+"/table.png"));
ImageIO.write(topImg, "png", new File(s.homeDir+"/top.png"));
ImageIO.write(leftImg, "png", new File(s.homeDir+"/left.png"));
这是应用程序运行的截图:screenshot
这是导出的标题:顶部
如果我评论“super.paint(g)”指令,我会获得正确的图像(因此显然没有所有 JLables)。似乎第二个油漆 (super.paint(g)) 被绘制到了 BufferedImage 中,并在其 JPanel 之外获取了元素。有人可以向我解释这种行为吗?谢谢你。
========== 为 SSCCE 编辑 =====================================
这应该编译。您可以按原样执行它,在 c:\ 中您会发现两个图像(top.png 和 left.png)应该与两个标题相同。不幸的是,他们不是。背景未绘制。此外(特别是如果您查看 left.png),您可以看到标签被绘制了两次并移动了(请注意,例如,“Left test 21”)。
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setSize(800, 600);
JScrollPane scrollP = new JScrollPane();
scrollP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MyPanel top = new MyPanel();
for(int i=0; i<30; i++){
JLabel label = new JLabel("Test "+i);
label.setOpaque(false);
label.setBounds(50*i, 40, 50, 20);
label.setForeground(Color.GREEN);
top.add(label);
}
top.setLayout(null);
top.setOpaque(false);
top.setPreferredSize(new Dimension(50*30, 200));
top.validate();
MyPanel left = new MyPanel();
for(int i=0; i<30; i++){
JLabel label = new JLabel("Left test "+i);
label.setBounds(0, 50*i, 100, 20);
label.setForeground(Color.RED);
left.add(label);
}
left.setLayout(null);
left.setOpaque(false);
left.setPreferredSize(new Dimension(200, 50*30));
MyPanel center = new MyPanel();
center.setLayout(null);
center.setOpaque(false);
center.setPreferredSize(new Dimension(50*30, 50*30));
scrollP.setViewportView(center);
scrollP.setColumnHeaderView(top);
scrollP.setRowHeaderView(left);
scrollP.setBounds(0, 50, 750, 500);
frame.add(scrollP);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
try{
BufferedImage topImg = top.createImage();
ImageIO.write(topImg, "png", new File("C:/top.png"));
BufferedImage leftImg = left.createImage();
ImageIO.write(leftImg, "png", new File("C:/left.png"));
}catch(Exception e){
e.printStackTrace();
}
}
}
class MyPanel extends JPanel{
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(new Color(255, 255, 0, 50));
for(int i=0; i<getWidth(); i+=50){
g.fillRect(i-1, 0, 2, getHeight());
}
super.paint(g); // COMMENT this line to obtain background images
}
public BufferedImage createImage() {
BufferedImage bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
paint(g);
g.dispose();
return bi;
}
}