gpt4 book ai didi

java - 将面板另存为图像

转载 作者:行者123 更新时间:2023-12-02 05:31:49 25 4
gpt4 key购买 nike

我的面板保存为 jpeg 图像,但绘图不可见。这是我的代码

BufferedImage img = new BufferedImage(painting.panel.getWidth(), painting.panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g1 = img.createGraphics();
painting.panel.paintAll(g1);
try {
ImageIO.write(img, "jpeg", new File(fd.getDirectory()+"\\"+fd.getFile()+".jpeg"));
System.out.println("panel saved as image");
} catch (Exception e) {
System.out.println("panel not saved" + e.getMessage());
}

但是当我打开文件时,绘图不可见。

最佳答案

对我来说就像一个魅力(查看下面提供的基本示例)。

一个常见的错误是忘记

  • 设置根组件的大小
  • 执行绘制组件的布局(通过在根组件及其所有子组件上调用 doLayout())

最后,最好使用 paint(Graphics) 而不是 paintAll(Graphics),如果您的组件未显示。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestPrint {

private static final int MY_WIDTH = 400;
private static final int MY_HEIGHT = 400;
private static final int STEPS = 20;

protected static void initUI() throws MalformedURLException {
Random r = new Random();
final List<Color> colors = new ArrayList<Color>();
for (int i = 0; i < STEPS; i++) {
colors.add(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
}
JPanel panel = new JPanel() {
@Override
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
for (int i = 0; i < STEPS; i++) {
g.setColor(colors.get(i));
g.fillRect(0 + (getWidth() / STEPS * i / 2), 0 + (getHeight() / STEPS * i / 2), getWidth() - (getWidth() / STEPS * i),
getHeight() - getHeight() / STEPS * i);
}
};

@Override
public Dimension getPreferredSize() {
return new Dimension(MY_WIDTH, MY_HEIGHT);
}
};
panel.setSize(panel.getPreferredSize());
layoutRecursively(panel);
BufferedImage bi = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
panel.paint(bi.getGraphics());
File file = new File("test.png");
try {
ImageIO.write(bi, "png", file);
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void layoutRecursively(Component component) {
component.doLayout();
if (component instanceof Container) {
Container container = (Container) component;
for (int i = 0; i < container.getComponentCount(); i++) {
layoutRecursively(container.getComponent(i));
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

我们得到如下图片:

enter image description here

关于java - 将面板另存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com