为 Jpanel 制作图形,但属于不同的类

发布于 2024-12-22 20:50:31 字数 679 浏览 1 评论 0原文

我的程序用于为不同数据结构的算法设置动画,我需要创建所有将移动的对象。

我研究过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-12-29 20:50:31

(一个友好的请愿书:包名称以小写字母开头,方法名称以小写字母开头。)

如果我理解正确,则可以执行以下操作:

您可以为绘制某些内容的对象提供一个接口:

public interface Drawable {
    void paint(Graphics2D g);
}

然后您的 ListObject 可以实现 Drawable 。

动画(JPanel)可以有:

public void addDrawable(Drawable drawable) {
    drawables.add(drawable);
}

在它的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:

public interface Drawable {
    void paint(Graphics2D g);
}

Your ListObject could then implements Drawable.

Animated (the JPanel) could have:

public void addDrawable(Drawable drawable) {
    drawables.add(drawable);
}

And in its paintComponent you could paint them.

In a way you are building your own light-weight components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文