2012-03-20 125 views
0

我試圖在JPanel中顯示圖像。我正在使用swingx庫 - ImagePainter。所以我創建了一個背景畫家設置爲ImagePainter的JXPanel。在JPanel swing中顯示圖像時出現內存不足

現在,我需要在2個獨立的JFrame中的JPanel上顯示相同的圖像。當我這樣做時,我會遇到一個堆空間問題。在這種情況下,我是否共享ImagePainter的同一個實例?這會有幫助嗎?但這意味着如果我需要顯示多個獨特的圖像,我會得到一個內存不足的錯誤。無論如何,我做錯了什麼?

我使用的代碼如下:

public static ImagePainter getImagePainter(String imageLocation) { 
    ImagePainter imgPainter=null; 
    try { 
     imgPainter = new ImagePainter(ImageIO.read(new File(imageLocation))); 
    } catch (IOException e) { 
     throw new SwingObjectRunException(e, ErrorSeverity.SEVERE, CommonUI.class); 
    } 
    return imgPainter; 
} 

錯誤堆棧跟蹤如下:

Caused by: java.lang.OutOfMemoryError: Java heap space 
    at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:59) 
    at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397) 
    at java.awt.image.Raster.createWritableRaster(Raster.java:938) 
    at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1056) 
    at javax.imageio.ImageReader.getDestination(ImageReader.java:2879) 
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:980) 
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:948) 
    at javax.imageio.ImageIO.read(ImageIO.java:1422) 
    at javax.imageio.ImageIO.read(ImageIO.java:1282) 

更新:這是顯示問題的SSCCE: 您需要的JGoodies在你的類路徑中構造佈局jar和swingx jar來編譯。再次點擊圖片兩次,對我來說,我收到了一個內存不足的錯誤。我使用的圖像也被附加。

Image to use

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import org.jdesktop.swingx.JXPanel; 
import org.jdesktop.swingx.painter.ImagePainter; 

import com.jgoodies.forms.layout.CellConstraints; 
import com.jgoodies.forms.layout.FormLayout; 


public class Photo { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try{ 
      JFrame frame=new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      JPanel panel=new JPanel(); 
      panel.setLayout(new FormLayout("5dlu,fill:250px,15dlu", "5dlu,fill:150px,15dlu,fill:pref,5dlu")); 

      panel.add(getImagePanel(), new CellConstraints(2, 2)); 

      JButton btnOpenDiag=new JButton("Open Image Again"); 
      panel.add(btnOpenDiag,new CellConstraints(2, 4, 1, 1, CellConstraints.CENTER, CellConstraints.FILL)); 

      btnOpenDiag.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        try { 
         JDialog dialog=new JDialog(); 
         JPanel panel=new JPanel(); 
         panel.setLayout(new FormLayout("5dlu,fill:250px,15dlu", "5dlu,fill:150px,5dlu")); 
         panel.add(Photo.getImagePanel(), new CellConstraints(2, 2)); 
         dialog.setContentPane(panel); 
         dialog.pack(); 
         dialog.setVisible(true); 
        } catch (IOException e1) { 
         e1.printStackTrace(); 
        } 
       } 
      }); 

      frame.setContentPane(panel); 
      frame.pack(); 
      frame.setVisible(true); 
     }catch(Exception e){ 

     } 


    } 

    protected static JXPanel getImagePanel() throws IOException { 
     JXPanel imgPanel=new JXPanel(); 
     ImagePainter painter=new ImagePainter(ImageIO.read(new File(Photo.class.getResource("image.jpg").getFile()))); 
     painter.setFillHorizontal(true); 
     painter.setFillVertical(true); 
     imgPanel.setBackgroundPainter(painter); 
     return imgPanel; 
    } 

} 
+0

像素和位深度的圖像有多大? *「2個獨立的JFrames」*應用程序通常應該只有[一幀](http://stackoverflow.com/a/9554657/418556)。 – 2012-03-20 11:17:17

+0

imageLocation中的文件有多大?如果你指向一個較小的文件會發生什麼? – 2012-03-20 11:25:47

+0

你確定你不是以某種方式在循環中調用getImagePainter()嗎? – 2012-03-20 11:29:41

回答

0

你只是沒有足夠的內存。圖像得到多少內存取決於它的分辨率。

所以圖像可能很大,或者你一次有兩個以上的圖像。

接下來認爲你可以做的是增加java進程的內存(xmx)。

相關問題