从外部 class 方法清除图形
Clearing Graphics from an outer class method
所以我有一个大学项目使用 swing 制作绘画程序,我需要清除 Graphics 对象,但是从外部的方法 class 然后再次绘制所有形状(刷新图形对象)作为我正在通过此方法传递图形对象。
class 负责保存我在此图形上绘制的所有形状(在 ArrayList 中)。
那么,如果我无法调用存在于 Jpanel class 中的 Super.paintComponent,我该怎么做呢?
as I'm passing the graphics object through this method .
您不应传递 Graphics 对象。 paintComponent() 方法(或调用的任何方法)应始终使用传递给 paintComponent() 方法的 Graphics 对象。
SO how can I do this if I can't call Super.paintComponent that exists in the Jpanel class .
在您进行自定义绘画的 class 中,您创建了一个 clear()
方法。这将简单地从 ArrayList 中删除所有 Shape 对象,然后调用 repaint()。
请参阅 Custom Painting Approaches 中的 DrawOnComponent
示例,该示例演示了这是如何完成的。
找到一个非常简单的答案 "Draw white rectangle, then draw shapes again"
那将简单地解决我的问题:)
所以我有一个大学项目使用 swing 制作绘画程序,我需要清除 Graphics 对象,但是从外部的方法 class 然后再次绘制所有形状(刷新图形对象)作为我正在通过此方法传递图形对象。
class 负责保存我在此图形上绘制的所有形状(在 ArrayList 中)。
那么,如果我无法调用存在于 Jpanel class 中的 Super.paintComponent,我该怎么做呢?
as I'm passing the graphics object through this method .
您不应传递 Graphics 对象。 paintComponent() 方法(或调用的任何方法)应始终使用传递给 paintComponent() 方法的 Graphics 对象。
SO how can I do this if I can't call Super.paintComponent that exists in the Jpanel class .
在您进行自定义绘画的 class 中,您创建了一个 clear()
方法。这将简单地从 ArrayList 中删除所有 Shape 对象,然后调用 repaint()。
请参阅 Custom Painting Approaches 中的 DrawOnComponent
示例,该示例演示了这是如何完成的。
找到一个非常简单的答案 "Draw white rectangle, then draw shapes again"
那将简单地解决我的问题:)