Java Canvas从BufferedImage中绘制图像

时间:2014-03-16 00:46:03

标签: java image file canvas

我目前正在处理我的个人项目。我们的想法是导入一张图片并将其裁剪成n片的bufferedimage,然后用随机序列将它们绘制到java画布上。之后将画布保存到图像文件。现在iam坚持保存画布,我已经搜索了解决方案,但没有一个工作。这是我的保存程序。 请帮助,提前谢谢

 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");
    }
    }
}

更新: 现在我明白了,现在使用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 个答案:

答案 0 :(得分:0)

没有必要使用getGraphics,你当然不应该处理它。

您应该尝试使用paint

而不是使用可能包含双缓冲的printAll
BufferedImage bi = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB);  
Graphics2D g2 = bi.createGraphics();
canvas.printAll(g2);  
g2.dispose();  

有关详细信息,请参阅Component#printAll

您还应该使用canvas的大小来确定图像的大小。上面的示例假定canvas已经适当调整大小......