2014-03-04 2 views
0

사용자가 JLabel에 그린 이미지를 저장하는 GUI를 만들려고합니다. 다음은 saveImage 메소드에 대한 코드입니다. 파일을 저장할 위치를 선택할 수 있도록 상자를 팝업 할 수는 있지만 실제로 저장된 파일은 없습니다.JLabel에서 이미지 저장하는 방법

public void saveImage() 
    { 
     BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     Graphics2D image2 = image.createGraphics(); 
     image2.setBackground(Color.WHITE); 
     image2.clearRect(0, 0, this.getWidth(), this.getHeight()); 
     this.paintAll(image2); 
     try 
     { 
      File output = new File("rectangle.png"); 
      ImageIO.write(image, "Rectangles", output); 
      final JFileChooser fchooser = new JFileChooser("."); 
      int retvalue = fchooser.showSaveDialog(RectangleLabel.this); 
      if (retvalue == JFileChooser.APPROVE_OPTION) 
      { 
       fchooser.setSelectedFile(output); 
       File f = fchooser.getSelectedFile(); 
      } 
     } 
     catch(IOException ie) 
     { 

     } 


    } 
+0

그래서 ... 코드는 어디에 있습니까? – Frakcool

답변

2

첫째, 당신은

ImageIO.write(img, "png", f); 

Writing/Saving an Image에서 봐 ...
BufferedImage img = new BufferedImage(label.getWidth(), label.getHeight(), BufferedImage.TYPE_INT_ARGB); 
Graphics2D g2d = img.createGraphics(); 
label.printAll(g2d); 
g2d.dispose(); 

그런 다음 당신이 그것을 저장해야 ... 구성 요소의 이미지를 만들 필요가 자세한 내용

관련 문제