Java图形不呈现SVG(SVG蝾螈和BuffereImage)
我正在开发一个示例应用程序,它显示了一个射线照相,用户必须能够通过单击它来标记兴趣点(以及放置牙齿和绘制多段线) 该方法包含一个与图像关联的SVG文件,该文件具有简单的结构:每行定义一个点,因此当打开图像时,我们将原始图像和SVG的组合放置在一个框架上,并将其渲染为BuffereImage,方法是从图形中绘制图形 该框架有一个鼠标侦听器,可在SVG文件中添加一行,并在单击的像素上添加一个点。然后,它使用更新后的SVG重新渲染原始图像。问题是,当第一次加载图像时,它工作正常,但单击时不会更新,在关闭应用程序然后再次打开之前,它不会显示任何新点(SVG文件确实会更新,如文本编辑器上所示) 我试着在图像查看器上调用invalidate和repoint,但它不起作用 动态非常简单:加载图像文件,然后使用drawImages方法在顶部渲染相关文件。完成所有操作后,最终结果将放置在框架中。我正在使用SVG蝾螈库进行渲染,因为这非常简单。蜡染看起来更完整,但也更复杂Java图形不呈现SVG(SVG蝾螈和BuffereImage),java,svg,graphics,bufferedimage,Java,Svg,Graphics,Bufferedimage,我正在开发一个示例应用程序,它显示了一个射线照相,用户必须能够通过单击它来标记兴趣点(以及放置牙齿和绘制多段线) 该方法包含一个与图像关联的SVG文件,该文件具有简单的结构:每行定义一个点,因此当打开图像时,我们将原始图像和SVG的组合放置在一个框架上,并将其渲染为BuffereImage,方法是从图形中绘制图形 该框架有一个鼠标侦听器,可在SVG文件中添加一行,并在单击的像素上添加一个点。然后,它使用更新后的SVG重新渲染原始图像。问题是,当第一次加载图像时,它工作正常,但单击时不会更新,在关
public class Test {
Graphics g;
ImageViewer v;
BufferedImage imagenbase;
ArrayList<BufferedImage> images;
String basename;
int count = 0;
SVGUniverse universe;
SVGDiagram diagram = null;
public Test() {
v = new ImageViewer(1200, 1200, 1.0, this);
universe = new SVGUniverse();
}
public void drawSVGdot(int x, int y) {
File file = new File(basename + "extra.svg");
if (file.exists()) {
try {
List<String> lines;
Path path = file.toPath();
lines = Files.readAllLines(path, StandardCharsets.UTF_8);
String extra = " <circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"1\" style=\"fill:red;stroke:red\"/>";
lines.add(1, extra);
Files.write(path, lines, StandardCharsets.UTF_8);
} catch (IOException e) {
}
} else {
try {
file.createNewFile();
StringBuilder sb = new StringBuilder("");
int h = v.getHeight();
int w = v.getWidth();
sb.append("<svg width=\"" + w + "\" height=\"" + h + "\" style=\"fill:none;stroke-width:4\">");
sb.append("\n");
sb.append(" <circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"1\" style=\"fill:red;stroke:red\"/>");
sb.append("\n");
sb.append("</svg>");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.append(sb);
} catch (IOException e) {
} finally {
if (writer != null)
try {
writer.close();
} catch (IOException e) {
}
}
} catch (IOException e) {
}
}
loadRelatedImages(basename);
}
public void loadImage(String uri, String uriless) {
try {
basename = uriless;
imagenbase = ImageIO.read(new File(uri));
g = imagenbase.getGraphics();
v.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadRelatedImages(String uri) {
BufferedImage[] imgs = new BufferedImage[5];
for (int i = 1; i <= 4; i++) {
File file = new File(uri + i + ".svg");
if (file.exists())
imgs[i - 1] = SVGtoImage(file);
}
File file = new File(uri + "extra.svg");
if (file.exists())
imgs[4] = SVGtoImage(file);
BufferedImage imagenfinal = new BufferedImage(imagenbase.getWidth(), imagenbase.getHeight(),
BufferedImage.TYPE_INT_ARGB);
g = imagenfinal.getGraphics();
g.drawImage(imagenbase, 0, 0, null);
for (int i = 0; i < imgs.length; i++) {
if (imgs[i] != null) {
g.drawImage(imgs[i], 0, 0, null);
}
}
v.setImage(imagenfinal);
v.invalidate();
v.repaint();
v.setVisible(true);
}
public BufferedImage SVGtoImage(File f) {
BufferedImage bi = new BufferedImage(v.getHeight(), v.getWidth(), BufferedImage.TYPE_INT_ARGB);
try {
universe.loadSVG(f.toURI().toURL());
URI xmlBase = f.toURI();
diagram = universe.getDiagram(xmlBase);
Graphics2D g = (Graphics2D) bi.getGraphics();
diagram.render(g);
} catch (MalformedURLException e) {
} catch (SVGException e) {
}
return bi;
}
public static void main(String[] args) {
Test t = new Test();
String imagename = "7.jpg";
String withoutext = imagename.substring(0, imagename.lastIndexOf('.'));
t.loadImage(imagename, withoutext);
t.loadRelatedImages(withoutext);
}
}
公共类测试{
图形g;
图像查看器v;
缓冲图像数据库;
阵列图像;
字符串基名称;
整数计数=0;
斯夫古尼弗宇宙;
SVGDiagram图=空;
公开考试(){
v=新的ImageViewer(1200、1200、1.0,本);
宇宙=新的SVGUniverse();
}
公共作废图纸(整数x,整数y){
File File=新文件(basename+“extra.svg”);
if(file.exists()){
试一试{
列出行;
路径路径=file.toPath();
lines=Files.readAllLines(路径,StandardCharsets.UTF_8);
字符串extra=“”;
行。添加(1,额外);
写入(路径、行、标准字符集.UTF_8);
}捕获(IOE异常){
}
}否则{
试一试{
createNewFile();
StringBuilder sb=新的StringBuilder(“”);
inth=v.getHeight();
int w=v.getWidth();
某人加上(“”);
某人附加(“\n”);
某人加上(“”);
某人附加(“\n”);
某人加上(“”);
BufferedWriter=null;
试一试{
writer=newbufferedwriter(newfilewriter(file));
(某人);
}捕获(IOE异常){
}最后{
if(writer!=null)
试一试{
writer.close();
}捕获(IOE异常){
}
}
}捕获(IOE异常){
}
}
loadRelatedImages(basename);
}
公共void loadImage(字符串uri,字符串uri){
试一试{
basename=uriless;
imagenbase=ImageIO.read(新文件(uri));
g=imagenbase.getGraphics();
v、 setVisible(真);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共void loadRelatedImages(字符串uri){
BuffereImage[]imgs=新的BuffereImage[5];
对于(int i=1;我问你为什么有些东西不起作用,但是有很多空的catch块..这通常是一个很好的开始。在这个列表中,添加e.printStackTrace()
。此外,你应该创建一个显示问题的列表,而不仅仅是转储你的代码(我们无法运行)。这将有助于其他人帮助你。:-)你会问为什么有些东西不起作用,但是有很多空的catch块。这通常是一个很好的开始。在列表中,添加e.printStackTrace()
。此外,你应该创建一个显示问题的列表,而不仅仅是转储你的代码(我们无法运行)。这将帮助其他人帮助你。:-)