improvements

This commit is contained in:
Ninja-Jambon 2024-03-27 14:24:27 +01:00
parent 491cd35136
commit fc18dea7f4
3 changed files with 39 additions and 6 deletions

View file

@ -22,31 +22,56 @@ import javax.imageio.ImageIO;
import java.net.URL;
import universe.Universe;
public class Grid extends JPanel {
private int width;
private int height;
private JButton[][] mat;
private int selected;
private Universe universe;
public Grid(int width, int height) {
public Grid(int width, int height, Universe universe) {
super(new GridLayout(height, width));
this.width = width;
this.height = height;
this.selected = 0;
this.universe = universe;
this.mat = new JButton[height][width];
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
this.mat[i][j] = new JButton();
this.mat[i][j].setBackground(Color.WHITE);
switch (this.universe.getGrid()[i + 1][j + 1]) {
case -1:
this.mat[i][j].setBackground(Color.RED);
break;
case 0:
this.mat[i][j].setBackground(Color.WHITE);
break;
}
this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width)));
final int coord_i = i;
final int coord_j = j;
final int final_selected = this.selected;
final Universe tempUniverse = this.universe;
this.mat[i][j].addActionListener(e -> {
System.out.println(coord_i + " " + coord_j);
switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) {
case 0:
this.mat[coord_i][coord_j].setBackground(Color.RED);
this.universe.addObstacle(coord_i + 1, coord_j + 1);
break;
case -1:
this.mat[coord_i][coord_j].setBackground(Color.WHITE);
this.universe.removeObstacle(coord_i + 1, coord_j + 1);
break;
}
//Universe.print(this.universe.getGrid(), this.width + 2, this.height + 2, 4, 4);
});
super.add(this.mat[i][j]);