0

我目前正在研究 zkoss 提供的 zk 框架,我可以在网页中显示图像,但我想在某些字段上放置红线矩形。我正在尝试这个:

// 在缓冲的私有 void drawRectangles() 上绘制矩形

Graphics2D graphics = image.createGraphics();

Iterator iterator = setOfRectangles.iterator();
while(iterator.hasNext()) {
    Rectangle rect = (Rectangle)iterator.next();
    graphics.drawRoundRect(rect.x, rect.y, rect.height, rect.width, 5, 5);
}
graphics.dispose();

}

我在另一个对上传事件作出反应的方法中调用此方法。

我也在这个类中保存上传的图像。

但这不起作用,如果我可以在绘制所有矩形后获取图像,那么我可以将该图像放在网页中,但我不知道如何实现这一点。谢谢大家的阅读和回复。

4

2 回答 2

1

就一般 Java 而言,要导出 a,Graphics2D您实际上想要Graphics2D从 a中获取,BufferedImageBufferedImage您将其写入文件。

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
for(Rectangle rect : setOfRectangles) {
    graphics.drawRoundRect(rect.x, rect.y, rect.height, rect.width, 5, 5);
}
ImageIO.write(bi, "PNG", new File("myPicture.png"));

这与 ZK 无关,您会在 SO 和其他地方找到大量参考资料。你最终会得到一个可以像平常一样在 ZK 中显示的常规图像文件。

也就是说,我会推荐一种替代方法。根据您的具体用例,这可能可以使用纯 CSS 来完成。CSS-Tricks 有一个很好的教程,名为“ Text Blocks Over Image ”,它探讨了这个想法。您不需要任何文本div,只需border在其上加上 a,将background-coloras保留transparent,然后根据需要定位即可。

于 2013-06-18T21:45:15.217 回答
0

这就是我所做的及其工作:

Graphics2D graphics = image.createGraphics();

Iterator iterator = setOfRectangles.iterator();
while(iterator.hasNext()) {
    Rectangle rect = (Rectangle)iterator.next();
    float thickness = 3.0F;
    Stroke stroke = graphics.getStroke();
    graphics.setStroke(new BasicStroke(thickness));

    graphics.drawRoundRect(rect.x, rect.y, rect.height, rect.width, 5, 5);
    graphics.setStroke(stroke);
    graphics.setColor(Color.RED);
}

wholeImage.setContent(image);
graphics.dispose();

完成此操作后,将整个图像加载到

于 2013-06-21T06:03:46.247 回答