Java:多次重新着色SVG图像

时间:2015-01-19 14:50:40

标签: java svg

我正在尝试使用此答案中的信息绘制地理图表:https://stackoverflow.com/a/26868280/518以“colore”SVG图形和 从这个答案:https://stackoverflow.com/a/11436655/518来转换它在BufferedImage中。我已经完成了,但每次我想刷新图表我都重新加载了SVG。

我的问题是:每次我想刷新图表时都没有重新加载SVG文件?

我粘贴我的工作代码,我认为注释第一行并取消注释节点替换代码可能会起作用,但它产生一个未改变的BufferedImage

protected void refreshImage(List<GeographicChartData>geoDataList)throws Exception{
    loadWorldMap(); // reads the SVG file into svgDoc property

    SVGStyleElement newColorNode=(SVGStyleElement)svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, "style");
    newColorNode.setAttributeNS(null, "type", "text/css");
    if(geoDataList!=null && geoDataList.size()>0){                      
        for(GeographicChartData geoData:geoDataList){
            double ppto=geoData.getPptoAmount();
            if(ppto!=0){
                Color pctColor=pct2Color.interpolateColor((geoData.getRealAmount()-ppto)/ppto);
                String colorString="."+geoData.getCountryIso()+" {fill: #"+getColorString(pctColor)+";}";                                       
                newColorNode.appendChild(svgDoc.createCDATASection(colorString));
            }

        }           
    }

    /*****
     * I think that this has to replace the color node and refresh the BufferedImage produced, but it doesn't.
styleNode is a property setted on loadWorldMap Method
     */

    /*if(oldColorNode==null){
        styleNode.getParentNode().appendChild(newColorNode);
    }else{
        styleNode.getParentNode().replaceChild(newColorNode, oldColorNode);         
    }
    oldColorNode=newColorNode;*/

    styleNode.getParentNode().appendChild(newColorNode); // the code above don't work, so i need this

    TranscoderInput transcoderInput = new TranscoderInput(svgDoc);
    ImageTranscoder imageTranscoder=new BufferedImageTranscoder();

    imageTranscoder.transcode(transcoderInput, null);       
    labelMap.setIcon(new ImageIcon(worldMapImage[0]));
}

这是BufferedImageTranscoder的代码:

class BufferedImageTranscoder extends ImageTranscoder{
    public BufferedImage createImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }


    public void writeImage(BufferedImage image, TranscoderOutput out) throws TranscoderException {
       worldMapImage[0] = image;
    }
}

0 个答案:

没有答案