【发布时间】: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 Approaches。
DrawOnComponent示例向您展示了如何将 Rectangle 对象列表绘制到面板上。 -
好吧,我迷路了,哈哈,我不明白里面的一切,因为它是点击等等
标签: java swing arraylist panel