2014-12-27 2 views
2

HTML img 태그를 이미지로 렌더링하는 JEditorPane을 얻을 수 없습니다. 표시되는 것은 모두 자리 표시 자 그래픽입니다. 아래는 제 코드입니다. 미리 감사드립니다.Java JEditorPane이 이미지를 표시하지 않습니다.

내가 볼 무엇 :

enter image description here

내 코드 :

import java.awt.*; 
import java.io.File; 
import java.net.URL; 
import java.util.Hashtable; 

import javax.swing.*; 
import javax.swing.text.html.HTMLEditorKit; 

public class test 
{ 
    private static Hashtable image_cache; 

public static void main(String[] args) 
{ 
    image_cache = new Hashtable(); 

    URL img_url = null; 

    try 
    { 
     img_url = new File("C:/img/mypic.png").toURI().toURL(); 
     Image img = Toolkit.getDefaultToolkit().createImage (img_url); 
     image_cache.put(img_url.toURI(), img); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    String html = "<html>" + 
      "<body>"+ 
      "<img src=\"" + img_url.toString() + "\">" + 
      "</body>" + 
      "</html>"; 

    JEditorPane swingbox = new JEditorPane(); 
    swingbox.setEditorKit(new HTMLEditorKit()); 
    swingbox.setContentType("text/html"); 
    swingbox.setText(html); 
    swingbox.getDocument().putProperty("imageCache", image_cache); 

    JFrame frame=new JFrame("Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(swingbox); 
    frame.setSize(800,600); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 
} 

답변

4

문제는 당신의 코드입니다 :

swingbox.getDocument().putProperty("imageCache", image_cache); 

을 주석이 라인과 작동합니다 벌금. 약간의 파기 후에 나는 문제가 image_cache.put (img_url.toURI(), img)과 함께 있음을 발견했다. image_cache.put (img_url, img)

맞춤 이미지 캐시는 나중에 코드를 디버그하는 데 도움이 될 수 있습니다. 다음은 저에게 효과가있는 약간의 변화를 보여주는 예입니다. ImageCache 클래스를 만들고 이되면 이미지가 캐시에서 반환되거나 이미지가 만들어지고 캐시에 저장되고 찾을 수 없으면 반환하도록합니다.

예제 코드 :

public class TestClass { 

    private static ImageCache image_cache; 

    public static void main(String[] args) { 
     URL img_url = null; 
     image_cache = new ImageCache(); 

     try 
     { 
      img_url = new File("C:/Users/User/Images/image.png").toURI().toURL(); 
      Image img = Toolkit.getDefaultToolkit().createImage (img_url); 
      image_cache.put(img_url, img); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     String html = "<html>" + 
       "<body>"+ 
       "<img src=\"" + img_url.toString() + "\">" + 
       "</body>" + 
       "</html>"; 

     JEditorPane swingbox = new JEditorPane(); 
     swingbox.setEditorKit(new HTMLEditorKit()); 
     swingbox.setContentType("text/html"); 
     swingbox.setText(html); 



     JFrame frame=new JFrame("Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(swingbox); 

     Dictionary cache=(Dictionary)swingbox.getDocument().getProperty("imageCache"); 

     // put the cache if it is not present. it should be null in the beginning 
     if (cache==null) { 
      swingbox.getDocument().putProperty("imageCache",image_cache); 
     } 

     frame.setSize(800,600); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 

    static class ImageCache extends Hashtable { 

     public Object get(Object key) { 

      Object result = super.get(key); 

      if (result == null){ 
       result = Toolkit.getDefaultToolkit().createImage((URL) key); 
       put(key, result); 
      } 

      return result; 
     } 
    } 

} 
+0

감사합니다! 그래서 이것은 작동합니다. – SoupMonster

+0

놀랍습니다. 실제 솔루션은 훨씬 간단합니다. 내가 제공 한 Custom ImageCache를 사용할 수도 있지만 실제 문제는 어리 석다. image_cache.put (img_url, img)이 아니어야합니다. image_cache.put (img_url.toURI(), img) ;; – ZakiMak

관련 문제