如何使用单独的方法调用油漆组件方法并绘制矩形?

发布于 2025-02-10 07:36:36 字数 674 浏览 3 评论 0原文

我正在使用Swing创建一个GUI,每次运行方法都会绘制一个新的矩形。 我想使用会增加的变量并继续在系列中制作新的矩形。 我的目标是给出一个链接列表的视觉表示,并强调链接列表的头部指向下一个项目的概念,该项目指向下一个,依此类推。

我是Java的基本知识的初学者,而我才刚刚开始摆动并创建用户界面。

void insert(int i, int j) {
    //some code which would create a new rectangle and add to my GUI.
}

// this is my panel class
public class MyPanel extends JPanel{
    MyPanel() {
        this.setPreferredSize(new Dimension(500, 500)); 
    }

    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setPaint(Color.blue);
        g2D.setStroke(new BasicStroke(5));
        g2D.drawRect(i, j, 100, 50);    
    }
}

I am using Swing to create a GUI which would draw a new rectangle every time a method runs.
I want to use variables that would get incremented and continue making new rectangles in a series.
My goal is to give a visual representation of a linked list and to emphasize on the concept that a linked list has a head which points to the next item which points to the next and so on.

I am a beginner with basic knowledge of Java, and I'm just getting started with Swing and creating User interfaces.

void insert(int i, int j) {
    //some code which would create a new rectangle and add to my GUI.
}

// this is my panel class
public class MyPanel extends JPanel{
    MyPanel() {
        this.setPreferredSize(new Dimension(500, 500)); 
    }

    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setPaint(Color.blue);
        g2D.setStroke(new BasicStroke(5));
        g2D.drawRect(i, j, 100, 50);    
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

安稳善良 2025-02-17 07:36:36

您可以做什么:

  1. 创建一个列表以将传递的参数存储到insert(int,int)方法
  2. 填充该列表中的insert(int,int)方法
  3. <代码> paintcomponents() -Method可以在保存值上循环,并
// records are fairly new to Java. See (*) for what it does
private record Tuple(int i, int j) {}

// this list contains values added by the `insert(int, int)` method
private final List<Tuple> tuples = new ArrayList<>();

private void insert(final int i, final int j) {
    // do some stuff
    // ...

    // save passed values to a list using the helper class "Tuple"
    this.tuples.add(new Tuple(i, j));

    // repaint UI
    this.repaint();
}

@Override
public void paintComponents(final Graphics g) {
    super.paintComponents(g);
    // loop over saved values and do something with their values
    for (final Tuple tuple : this.tuples) {
        // draw rect for saved tuple
        g.drawRect(tuple.i * 10, tuple.j * 10, 20, 20);
    }
}

在Java 14中介绍了(*)记录(*)记录,它们可用于以简短的方式创建数据类。

他们会自动生成getters,equals()hashcode()toString() - method。

对于此示例,可以将记录重写为:(

private class Tuple {
  private final int i;
  private final int j;

  public Tuple(final int i, final int j) {
    this.i = i;
    this.j = j;
  }
}

我们不需要 equals()hashcode() and code> tostring())

What you can do:

  1. Create a list to store the passed arguments to the insert(int,int) method
  2. Fill that list in the insert(int,int) method
  3. the paintComponents()-method can loop over the saved values and display them
// records are fairly new to Java. See (*) for what it does
private record Tuple(int i, int j) {}

// this list contains values added by the `insert(int, int)` method
private final List<Tuple> tuples = new ArrayList<>();

private void insert(final int i, final int j) {
    // do some stuff
    // ...

    // save passed values to a list using the helper class "Tuple"
    this.tuples.add(new Tuple(i, j));

    // repaint UI
    this.repaint();
}

@Override
public void paintComponents(final Graphics g) {
    super.paintComponents(g);
    // loop over saved values and do something with their values
    for (final Tuple tuple : this.tuples) {
        // draw rect for saved tuple
        g.drawRect(tuple.i * 10, tuple.j * 10, 20, 20);
    }
}

(*) records were introduced in Java 14 and they can be used to create data classes in a short way.

They automatically generate Getters, the equals(), hashCode() and toString()-method.

For this example, the record could be be rewritten to:

private class Tuple {
  private final int i;
  private final int j;

  public Tuple(final int i, final int j) {
    this.i = i;
    this.j = j;
  }
}

(we don't need equals(), hashCode() and toString())

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