简体   繁体   中英

Java Canvas Draw Image from BufferedImage

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

 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

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++;
        }
    }
}
}

There's no need to use getGraphics and you certainly shouldn't be disposing of it.

Instead of using paint , which could include double buffering, you should try using 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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM