在用 Java 2D 进行画图时可以非常方便的设置线条的粗细及接头的形状。下面是一个简单的例子供大家参考:
float thick = 0.5f; //设置画刷的粗细为 0.5
BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
Stroke stroke = g.getStroke(); //得到当前的画刷
g.setStroke(new BasicStroke(thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
g.draw(new Line2D.Float(x1, y1, x2, y2)); 画线
g.setStroke( stroke ); //将画刷复原
更详细的使用方法请参阅 JDK 文档。
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
标签: