I am currently working on my personal project. The idea is to import a picture and crop it into n-pieces of bufferedimage then draw them with random sequence to a java canvas. After that save the canvas to image file. Right now iam stuck at saving the canvas and i have searching for the solutions but none of them work. Here my save procedure. Please help, thanks in advance
我目前正在做我的个人项目。其想法是导入图片并将其裁剪成n块,然后将其随机序列绘制到java画布中。然后将画布保存到图像文件中。现在我还在保存画布,我在寻找解决方案,但是没有一个能工作。在这里我保存过程。请帮忙,谢谢。
private void save() {
int r = jFileChooser1.showSaveDialog(this);
File file2 = null;
if(r == jFileChooser1.APPROVE_OPTION){
file2 = jFileChooser1.getSelectedFile();
Graphics g2d = canvas.getGraphics();
BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
canvas.paint(g2);
g2.dispose();
g2d.dispose();
try
{
ImageIO.write(bi, "jpg", file2);
}
catch (Exception ex) {
System.out.println("Error saving");
}
}
}
Update : Now i get it, after using paintComponents now i can save the picture. Thanks @madProgrammer
更新:现在我明白了,在使用了paintComponents之后,现在我可以保存图片了。由于@madProgrammer
public class CaptureImage extends javax.swing.JFrame {
MyPanel canvas;
BufferedImage[] processedImage;
public CaptureImage(BufferedImage[] proc) {
processedImage = proc;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private void createAndShowGUI() {
System.out.println("Created GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Output Picture");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
save();
System.exit(0);//cierra aplicacion
}
});
canvas = new MyPanel(processedImage);
f.add(canvas);
f.setSize(400, 400);
f.setVisible(true);
}
public void save() {
JFileChooser jFileChooser1 = new JFileChooser();
int r = jFileChooser1.showSaveDialog(this);
File file2 = null;
if (r == jFileChooser1.APPROVE_OPTION) {
file2 = jFileChooser1.getSelectedFile();
BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
canvas.print(g2);
try {
ImageIO.write(bi, "jpg", file2);
g2.dispose();
} catch (Exception ex) {
System.out.println("Error saving");
}
}
}
}
class MyPanel extends JPanel {
private Image[] processedImage;
public MyPanel(Image[] processedImage) {
this.processedImage = processedImage;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
repaint();
}
});
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int loopj = 10;
int loopi = 10;
int l = 0;
int jPieces = 100;
int[] r = new int[jPieces];
int rand = 0;
for (int i = 0; i < r.length; i++) {
rand = (int) (Math.random() * (r.length));
if (Arrays.binarySearch(r, 0, r.length, rand) <= -1) {
r[i] = rand;
}
}
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < loopi; i++) {
for (int j = 0; j < loopj; j++) {
g2d.drawImage(processedImage[r[l]], i * 30, j * 30, this);
l++;
}
}
}
}
1 个解决方案
#1
0
There's no need to use getGraphics
and you certainly shouldn't be disposing of it.
没有必要使用getGraphics,当然也不应该处理它。
Instead of using paint
, which could include double buffering, you should try using printAll
您应该尝试使用printAll,而不是使用可以包含双缓冲的油漆。
BufferedImage bi = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
canvas.printAll(g2);
g2.dispose();
See Component#printAll
for more details.
有关详细信息,请参阅组件#printAll。
You should also be using the size of the canvas
to determine the size of the image. The above example assumes that the canvas
has already been sized appropriately...
您还应该使用画布的大小来确定图像的大小。上面的例子假设画布已经适当地大小了…
#1
0
There's no need to use getGraphics
and you certainly shouldn't be disposing of it.
没有必要使用getGraphics,当然也不应该处理它。
Instead of using paint
, which could include double buffering, you should try using printAll
您应该尝试使用printAll,而不是使用可以包含双缓冲的油漆。
BufferedImage bi = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
canvas.printAll(g2);
g2.dispose();
See Component#printAll
for more details.
有关详细信息,请参阅组件#printAll。
You should also be using the size of the canvas
to determine the size of the image. The above example assumes that the canvas
has already been sized appropriately...
您还应该使用画布的大小来确定图像的大小。上面的例子假设画布已经适当地大小了…