简体   繁体   English

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

[英]How do I use a separate method to call the paint component method and draw a rectangle?

I am using Swing to create a GUI which would draw a new rectangle every time a method runs.我正在使用 Swing 创建一个 GUI,它会在每次运行方法时绘制一个新的矩形。 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.我是一名具备 Java 基础知识的初学者,我刚刚开始使用 Swing 并创建用户界面。

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);    
    }
}

What you can do:你可以做什么:

  1. Create a list to store the passed arguments to the insert(int,int) method创建一个列表来存储传递给insert(int,int)方法的参数
  2. Fill that list in the insert(int,int) methodinsert(int,int)方法中填充该列表
  3. the paintComponents() -method can loop over the saved values and display them paintComponents() - 方法可以遍历保存的值并显示它们
// 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. (*) 记录是在 Java 14 中引入的,它们可用于在短时间内创建数据类。

They automatically generate Getters, the equals() , hashCode() and toString() -method.它们会自动生成 Getter、 equals()hashCode()toString()方法。

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() ) (我们不需要equals()hashCode()toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM