我一直致力于一个简单的绘画程序,最近我在绘制颜色组件中重绘形状时遇到了麻烦。
@Override
public void paintComponent(final Graphics theGraphics) {
super.paintComponent(theGraphics);
final Graphics2D g2d = (Graphics2D) theGraphics;
if (!preDrawnShapes.isEmpty() && !preDrawnShapeThickness.isEmpty()) {
for (int i = 0; i < preDrawnShapes.size(); i++) {
g2d.setStroke(new BasicStroke(preDrawnShapeThickness.get(i)));
g2d.draw(preDrawnShapes.get(i));
}
}
g2d.setStroke(new BasicStroke(currentThickness));
if (myShape != null) {
g2d.draw(myShape);
preDrawnShapeThickness.add(currentThickness);
}
}
这个paintComponenent应该首先重绘先前绘制的形状,然后绘制从用户输入创建的新形状。
由于某种原因,绘制新形状时的形状厚度设置为当前厚度,但我之前绘制的任何形状的默认厚度为1.
preDrawnShapes是一个Shapes ArrayList,而preDrawnShapeThickiness是一个Float Arraylist。
我在这里错过了什么吗?
更新:我发现PreDrawnShapeThickness只存储零浮点数。我不确定为什么。
答案 0 :(得分:1)
增量绘画有两种常用方法:
保留要绘制的对象列表。然后paintComponent()方法遍历List并绘制每个对象。因此,在您的情况下,自定义对象将包含您要绘制的Shape和形状厚度的int值。
将每个Object直接绘制到BufferedImage。然后你可以使用JLabel将BufferedImage显示为Icon,或者使用自定义绘画来绘制BufferedImage。
查看Custom Painting Approaches以了解这两种方法的示例。