为 Jpanel 制作图形,但属于不同的类
我的程序用于为不同数据结构的算法设置动画,我需要创建所有将移动的对象。
我研究过使用 Graphics 类,但看起来您必须创建一种方法来在具有面板的类中绘制线条和框。有没有一种方法可以从具有面板的类的实例中进行绘图?
现在我有使用标签工作的列表,如下所示。
anim
是将动画对象放置到其中的面板。我正在尝试为图形创建一个名为 PathObject
的类,它需要图形中的 drawline()
方法,但我找不到将 Graphics 添加到 的方法动画
。 任何帮助都会很棒。
package Objects;
import javax.swing.*;
import Algorithms.Animated;
/** Animated object to be displayed as part of a list */
public class ListObject<T extends Number> extends AnimObject<T>
{
// Constructor
public ListObject(Animated anim, T val)
{
super(anim, val);
Setscale(20, val.intValue());
}
}
My program is used to animate algorithms for different data structures and I need to create all the objects that will move around.
I looked into using the Graphics class but it looks like you have to make a method to draw lines and boxes in the class that has the panels. Is there a way of drawing from an instance of the class that has the panels?
Right now I have lists working by using labels as below.
anim
is the panel that the animated objects will be placed into. I'm trying to make a class called PathObject
for graphs which needs the drawline()
method in graphics but I can't find a way of adding Graphics to anim
.
Any help would be great.
package Objects;
import javax.swing.*;
import Algorithms.Animated;
/** Animated object to be displayed as part of a list */
public class ListObject<T extends Number> extends AnimObject<T>
{
// Constructor
public ListObject(Animated anim, T val)
{
super(anim, val);
Setscale(20, val.intValue());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(一个友好的请愿书:包名称以小写字母开头,方法名称以小写字母开头。)
如果我理解正确,则可以执行以下操作:
您可以为绘制某些内容的对象提供一个接口:
然后您的 ListObject 可以实现 Drawable 。
动画(JPanel)可以有:
在它的paintComponent中你可以绘制它们。
在某种程度上,您正在构建自己的轻量级组件。
(One friendly petition: package names in small letters, method names with beginning small letter.)
If I understand you correctly, the following would do:
You could have an interface for objects that draw something:
Your ListObject could then
implements Drawable
.Animated (the JPanel) could have:
And in its paintComponent you could paint them.
In a way you are building your own light-weight components.