EDIT
I want to make a program wich allows me to randomly creates panels
, I have a MVC
implementation.
Here the scenario :
When I click button Next, I want to update the number of panels(Its a list of panels within a panelContainer
) but the GUI won't. When I use the debugger, it looks correct. I empty the panelsContainerand and the valuesList and I fill it with new ones, but in the GUI, the old panels stay and the new are just added. So I find my self with 20 to N panels.
I think the problem is in Test()
(GameView)
the old one was like this :
public void test(int number, List<Integer> panels){
myListFigures.clear();
nbpanels = number;
myListTmp = panels;
panelsContrainer.removeAll();
panelsContrainer.revalidate();
setWork(nbpanels,myListTmp);
panelsContrainer.revalidate();
}
My code:
GameModel: class where the numbers of panels and their values is created.
public class GameModel {
private boolean isDragging;
private int numberToAcheve;
private int numberOfCut;
private int numberOfPanels;
private int sum;
private List<Integer> myListRandomValue = new ArrayList<>();
private List<Integer> myListPanelsValue = new ArrayList<>();
GameModel(){
this.isDragging = false;
this.numberToAcheve = 0;
this.numberOfCut= 0;
this.numberOfPanels = 0;
this.sum = 0;
this.allWork();
}
public void allWork(){
int maximum = 6;
int minimum = 3;
Random rn = new Random();
StringBuilder sb = new StringBuilder();
int randomNumber = 0;
int range = 0;
// decoupage()
range = maximum - minimum + 1;
numberOfCut = rn.nextInt(range) + minimum;
// NumberAttribution()
for(int instance = 0; instance < numberOfCut;instance++){
if (Math.random() > 0.5) {
randomNumber = 1 + (int) (Math.random() * 9);
} else {
randomNumber = 10 + (int) (Math.random() * 90);
}
myListRandomValue.add(randomNumber);
}
// panelConcat()
for(int i=0; i < myListRandomValue.size(); i++){
sb.append(myListRandomValue.get(i));
}
for(int j=0; j< sb.length(); j++){
myListPanelsValue.add(Integer.parseInt(sb.substring(j,j+1)));
}
numberOfPanels = myListPanelsValue.size();
}
public int numberToAcheve(){
for(int i = 0; i < myListPanelsValue.size(); i++)
{
sum += myListPanelsValue.get(i);
}
return sum;
}
public int getNumberToAcheve() {
return numberToAcheve;
}
public int getSum() {
return sum;
}
public int getNumberOfPanels() {
return numberOfPanels;
}
public List<Integer> getMyListPanelsValue() {
return myListPanelsValue;
}
}
GameView : class contain all components and adds panels with values by setWork()
to GUI
public class GameView {
public List<Integer> myListTmp = new ArrayList<>();
public int nbpanels = 0;
public List<Figure> myListFigures = new ArrayList<>();
private JPanel panelsContrainer = new JPanel(new GridLayout(1,0));
private JPanel controlsContrainer = new JPanel();
private final JFrame frame;
private JButton next = new JButton("Next");
public GameView(){
frame = new JFrame("");
frame.getContentPane().setLayout(new BorderLayout());
controlsContrainer.add(next);
frame.getContentPane().add(panelsContrainer, BorderLayout.NORTH);
frame.getContentPane().add(controlsContrainer, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,300);
frame.setVisible(true);
}
public JButton getNext() {
return next;
}
public void setWork(int numberOfPanels, List<Integer> panelsValues){
for(int instance = 0; instance < numberOfPanels; instance++){
myListFigures.add(new Figure(panelsValues.get(instance)));
panelsContrainer.add(myListFigures.get(instance));
}
}
public void test(int number, List<Integer> panels){
myListFigures.clear();
nbpanels = number;
myListTmp = panels;
}
}
GameController:
public class GameController {
private GameModel model;
private GameView view;
public GameController(GameModel model, GameView view) {
this.model = model;
this.view = view;
this.view.setWork(this.model.getNumberOfPanels(),this.model.getMyListPanelsValue());
}
public void contol(){
view.getNext().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action();
}
});
}
private void action(){
model.allWork();
view.test(model.getNumberOfPanels(),model.getMyListPanelsValue());
}
}