패널의 요구 사항에 따라 레이아웃해야합니다. 패널이 아직 화면에 실현되지 않은 경우, 그것은 당신이
다음 예는 패널이 화면에 표시되지 않는 것으로 가정 할 예상대로 ... 렌더링되지 않을 수 있습니다
setSize(getPreferredSize());
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
printAll(g);
g.dispose();
try {
ImageIO.write(image, "png", new File([location goes here]);
} catch (IOException e) {
e.printStackTrace();
}
구성 요소가 화면에 실현되지 않은 경우
당신은 paint
를 직접 호출하지 않도록해야합니다, 당신의 자원을 작성하는 경우, 당신은 폐기해야한다, 또한 printAll
를 사용하는 대신에, 예외를 던질 수)
업데이트 됨
나는이 간단한 예를했습니다. 상단에 스크린 샷, 왼쪽에 jpeg, 오른쪽에 png.
JPEG는 30킬로바이트 및 PNG가 320킬로바이트입니다
나는 그것을 만들려면이 사용
...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class PaintComponent {
public static void main(String[] args) {
new PaintComponent();
}
public PaintComponent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel paintPane;
public TestPane() {
paintPane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
paintPane.add(new JLabel("I'm a label"), gbc);
paintPane.add(new JTextField("I'm a text field", 20), gbc);
paintPane.add(new JLabel(new ImageIcon("some\pretty\picture")), gbc);
setLayout(new BorderLayout());
add(paintPane);
JButton paint = new JButton("Capture");
paint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage image = new BufferedImage(paintPane.getWidth(), paintPane.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
paintPane.printAll(g);
g.dispose();
try {
ImageIO.write(image, "jpg", new File("Paint.jpg"));
ImageIO.write(image, "png", new File("Paint.png"));
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
add(paint, BorderLayout.SOUTH);
}
}
}
내가 만들 것을 실제로 올바른 파일을보고하고 있는지 확인)
조언 해 주셔서 감사합니다. 그래도 이미지를 내보낼 때 내 JPanel이 화면에 표시됩니다. 또한 어떤 형식의 이미지를 내보낼지라도 메모리 크기면에서 모두 동일한 크기입니다. 그게 기대 되니? – user2589692
예기치 않은 생각이들 것입니다. 당신이 기대하는 이미지입니까? – MadProgrammer
그래, 그냥 JPanel에 표시된 이미지의 품질과 똑같지는 않습니다. – user2589692