我在做一个UML编辑器,要求从数据库中读出UML元素,生成一张图片,能够在生成的BufferedImage上再画上一些做修饰用的图片。比如说在一个类标题前画上一个标识可见性的标志。
public Image(OutputStream out,
ArrayList<AbstractNode> nodes, ArrayList<AbstractEdge> edges) {
this.out = out;
this.nodes = nodes;
this.edges = edges;
}
public void writeImage() {
Size size = getSize();
BufferedImage bi = new BufferedImage(
size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D)bi.getGraphics();
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, size.width, size.height);
// draw
int i;
if (nodes != null) {
for (i=0; i<nodes.size(); i++) {
nodes.get(i).draw(g2d);
}
}
if (edges != null) {
for (i=0; i<edges.size(); i++) {
edges.get(i).draw(g2d);
}
}
// end draw
g2d.dispose();
bi.flush();
// create image
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}