2013-03-06 60 views
0

我有一個.svg文件來創建一個具有與邊界相關的監聽器的地圖(我猜它叫做可縮放矢量圖形的「節點」)。但是,我想通過JPanel將此文件添加爲BufferedImage。 (我是一個關於擺動組件的新手,所以如果你有更好的想法請分享)。此外,我用Batik庫,並瞭解它的一些信息,我用這個代碼示例來獲得這個.svg文件爲BufferedImage的當讀取一個svg文件爲BufferedImage時,java.lang.NoClassDefFoundError

package org.pulpcore.tools.res; 

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import org.apache.batik.dom.svg.SVGDOMImplementation; 
import org.apache.batik.transcoder.TranscoderException; 
import org.apache.batik.transcoder.TranscoderInput; 
import org.apache.batik.transcoder.TranscoderOutput; 
import org.apache.batik.transcoder.TranscodingHints; 
import org.apache.batik.transcoder.image.ImageTranscoder; 
import org.apache.batik.util.SVGConstants; 
import org.apache.commons.io.FileUtils; 

public class SVGRasterizer { 

    /** 
    Rasterizes an SVG file using Batik. 

    @param svgFile The svg file. 
    @return The rasterized image. 
    @throws IOException if the svg file could not be rasterized. 
    */ 
    public static BufferedImage rasterize(File svgFile) throws IOException { 

     final BufferedImage[] imagePointer = new BufferedImage[1]; 

     // Rendering hints can't be set programatically, so 
     // we override defaults with a temporary stylesheet. 
     // These defaults emphasize quality and precision, and 
     // are more similar to the defaults of other SVG viewers. 
     // SVG documents can still override these defaults. 
     String css = "svg {" + 
       "shape-rendering: geometricPrecision;" + 
       "text-rendering: geometricPrecision;" + 
       "color-rendering: optimizeQuality;" + 
       "image-rendering: optimizeQuality;" + 
       "}"; 
     File cssFile = File.createTempFile("batik-default-override-", ".css"); 
     FileUtils.writeStringToFile(cssFile, css); 

     TranscodingHints transcoderHints = new TranscodingHints(); 
     transcoderHints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE); 
     transcoderHints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, 
       SVGDOMImplementation.getDOMImplementation()); 
     transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, 
       SVGConstants.SVG_NAMESPACE_URI); 
     transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg"); 
     transcoderHints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().toString()); 

     try { 

      TranscoderInput input = new TranscoderInput(new FileInputStream(svgFile)); 

      ImageTranscoder t = new ImageTranscoder() { 

       @Override 
       public BufferedImage createImage(int w, int h) { 
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
       } 

       @Override 
       public void writeImage(BufferedImage image, TranscoderOutput out) 
         throws TranscoderException { 
        imagePointer[0] = image; 
       } 
      }; 
      t.setTranscodingHints(transcoderHints); 
      t.transcode(input, null); 
     } 
     catch (TranscoderException ex) { 
      // Requires Java 6 
      //throw new IOException("Couldn't convert " + svgFile, ex); 
      ex.printStackTrace(); 
      throw new IOException("Couldn't convert " + svgFile); 
     } 
     finally { 
      cssFile.delete(); 
     } 

     return imagePointer[0]; 
    } 
} 

這是我的同班同學使用的GUI組件和部分在那裏我嘗試使用的BufferedImage

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class JRisk { 

    private JFrame mainMap; 

    public JRisk() throws IOException{ 

     initComponents(); 
    } 

    private void initComponents() throws IOException{ 

     mainMap = new JFrame(); 
     mainMap.setResizable(false); 

       JPanel mainPanel = new JPanel(); 

       File riskMap = new File("src\\src\\risk.svg"); 

       BufferedImage bufImage = SVGRasterizer.rasterize(riskMap); 

       mainMap.setIconImage(bufImage); 

     mainMap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       mainMap.add(mainPanel); 
       mainMap.pack(); 
       mainMap.setVisible(true); 
    } 

} 

最後,包含主要方法的測試程序。

import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.SwingUtilities; 


public class Test { 

    /** 
    * @param argsh 
    */ 
    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
         try { 
          new JRisk(); 
         } catch (IOException ex) { 
          Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
         } 
      } 
     }); 
    } 
} 

那麼我的問題是編譯器提供了一個錯誤是

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/w3c/dom/svg/SVGDocument 

,整個誤差

at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    at org.apache.batik.dom.svg.SVGDOMImplementation.createDocument(Unknown Source) 
    at org.apache.batik.dom.util.SAXDocumentFactory.startElement(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3063) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568) 
    at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source) 
    at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source) 
    at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source) 
    at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source) 
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source) 
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source) 
    at src.SVGRasterizer.rasterize(SVGRasterizer.java:74) 
    at src.JRisk.initComponents(JRisk.java:31) 
    at src.JRisk.<init>(JRisk.java:17) 
    at src.Test$1.run(Test.java:24) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) 
    at java.awt.EventQueue.access$000(EventQueue.java:101) 
    at java.awt.EventQueue$3.run(EventQueue.java:666) 
    at java.awt.EventQueue$3.run(EventQueue.java:664) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.svg.SVGDocument 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    ... 51 more 

我會非常非常感激,如果你能幫助這個話題甚至有用的建議!並感謝反正

+0

你是如何運行你的程序,通過IDE或命令行?你的類路徑是什麼樣的? – Byron 2013-03-06 20:25:52

+0

我從Netbeans運行它,我將.jar文件加載到項目中 – quartaela 2013-03-06 20:26:35

+1

您查看了這篇文章嗎?也許你可以下載你需要的jar並將它添加到你的項目中:http://stackoverflow.com/questions/7891250/batik-not-in-classpath – Byron 2013-03-06 20:33:59

回答

1

下後我的地址,你的問題:
Batik not in classpath

看起來好像jar文件可能不包含在蠟染下載,你可以下載所需要的jar庫,並將其添加到你的項目。

0

嘗試添加xml-apis.jar和/或xml-apis-ext.jar。這對我有效。

相關問題