【问题标题】:How to put Object into a panel?如何将对象放入面板?
【发布时间】:2018-11-28 14:53:16
【问题描述】:

所以我有一个对象列表,具有随机的高度和重量。我也将这些对象的随机数放入一个变量中。

我要做的是将所有这些对象打印到正确的面板中(我有 2 个面板)。

首先,我的 GUI 和对象类(块)是 2 个独立的类。进入 GUI,我正在这样做:

    private JPanel initPanelBloc() {
    panelBloc = new JPanel();
    bloc = new Bloc(false);
    panelBloc.add(bloc);
    return panelBloc;
    }

我的集团课程:

public class Bloc extends JPanel{
private int hauteur, largeur, nombreBloc;
private boolean premierPassage = true;
private ArrayList<Bloc> listeBlocRestant;
private Random rand = new Random();

public Bloc(boolean premierPassage) {
    this.hauteur = 10 + rand.nextInt(50 - 10);
    this.largeur = 10 + rand.nextInt(50 - 10);      
    listeBlocRestant = new ArrayList<Bloc>();
    if(premierPassage == true) {
        this.nombreBloc = 5 + rand.nextInt(30 - 5);
        insererBlocList();
    }
}

public ArrayList<Bloc> insererBlocList(){
    premierPassage = false;
    for(int i=0; i<nombreBloc; i++) {       
        Bloc bloc = new Bloc(false);
        listeBlocRestant.add(bloc);
    }
    return listeBlocRestant;
}

public void paintComponent(Graphics2D g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.fillRect(10, 20, this.largeur, this.hauteur);   
}

我还有第三堂课,我称之为 GUI 类:

    public Optimisation() {
    this.aff = new InterfaceGraphique();
}

它在上面的类中,我需要做我想做的事。

我没有写我想做的事,因为我仍然不知道该怎么做。我是否应该为每个循环创建一个并获取块列表以及我希望将它们打印在面板上的每个块,并在 blocs 之间更改 x 和 y(fillRect 的)?我真的迷路了,我昨天试着想这个,但仍然没有头绪..

真诚的

【问题讨论】:

  • Custom Painting ApproachesDrawOnComponent 示例向您展示了如何将 Rectangle 对象列表绘制到面板上。
  • 好吧,我迷路了,哈哈,我不明白里面的一切,因为它是点击等等

标签: java swing arraylist panel


【解决方案1】:

我迷路了,我不明白那里的一切,因为它是通过点击等等

嗯,点击确实与绘画概念无关。

绘画概念是将要绘画的对象存储在 ArrayList 中。然后在paintComponent() 方法中遍历ArrayList 来绘制每个对象。

在我的示例中,您有一个方法addRectangle(...),它一次添加一个 Rectangle 对象。您可以通过调用此方法手动添加 Rectangle,而无需使用鼠标。这允许您添加不同大小/位置/颜色的矩形。

例如你只需将代码更改如下:

private static void createAndShowGUI()
{
    DrawingArea drawingArea = new DrawingArea();
    drawingArea.addRectangle(new Rectangle(10, 10, 200, 100), Color.RED);
    drawingArea.addRectangle(new Rectangle(210, 110, 20, 100), Color.BLUE);

现在运行代码时会出现红色矩形。

重点是:

  1. 您需要一种方法来将要绘制的对象添加到您的类中
  2. 然后您需要在您的paintComponent() 方法中绘制这些对象。您无法按照当前的方式对绘画进行硬编码。

在您的代码中,您的 Bloc 对象需要包含绘制区块所需的信息。

【讨论】:

  • 嗯,我明白了。所以就我而言,我已经将对象存储到 ArrayList listeBlocRestant 中。所以现在,我需要做什么,它要做这样的事情:for(Bloc bloc : listeBlocRestant.contains(bloc)) 然后,对于每个块,我需要调用一个函数来在面板中绘制它?如果我花时间理解我真的很抱歉,我正在尝试学习新事物..
  • So in my case, I already store the object into the ArrayList - 是的,但在我看来,所有 Bloc 对象都是用相同的属性创建的,所以当你尝试绘制它们时,你只会看到一个 Bloc。您需要一种方法来区分一个集团与另一个集团。也许您需要向类添加一个方法来更改特定的 Bloc 及其属性。 I'm trying to study new things没问题。您花时间阅读了建议并提出了后续问题。看来您现在了解大局了。
  • Bloc object are created with the same properties 它们的尺寸不同,正如你所看到的,我正在这样做:this.hauteur = 10 + rand.nextInt(50 - 10); this.largeur = 10 + rand.nextInt(50 - 10);(hauteur 表示高度,largeur 表示宽度)。还不够吗?
  • they don't have the same size - 错过了。如果您的逻辑只是拥有块的“大小”属性,那么当您遍历 ArrayLIst 时,您需要管理每一行的绘制方式以及何时需要自己换行。您可以完全控制每个 Bloc 对象的属性和每个 Bloc 的绘制。
  • 所以如果我理解得很好,我需要这样做:public void drawRect() { for(Bloc bloc : listeBlocRestant) { //here I call the function addRectangle which draw it in the panel ? } } public void addRectangle(Rectangle rectangle) { //draw the Rectangle in the panel ? } 我真的不知道如何把它作为评论说得好,哈哈,它很难像这样阅读
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 2017-05-24
  • 1970-01-01
相关资源
最近更新 更多