improvements
This commit is contained in:
parent
491cd35136
commit
fc18dea7f4
3 changed files with 39 additions and 6 deletions
|
@ -172,6 +172,13 @@ public class Universe {
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeObstacle(int pos_i, int pos_j) {
|
||||||
|
if (this.grid[pos_i][pos_j] == -1) {
|
||||||
|
this.grid[pos_i][pos_j] = 0;
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
|
||||||
public int possibleChoices(Situation s) {
|
public int possibleChoices(Situation s) {
|
||||||
int i = s.pos_i;
|
int i = s.pos_i;
|
||||||
int j = s.pos_j;
|
int j = s.pos_j;
|
||||||
|
|
|
@ -22,31 +22,56 @@ import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import universe.Universe;
|
||||||
|
|
||||||
public class Grid extends JPanel {
|
public class Grid extends JPanel {
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
private JButton[][] mat;
|
private JButton[][] mat;
|
||||||
private int selected;
|
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));
|
super(new GridLayout(height, width));
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.selected = 0;
|
this.selected = 0;
|
||||||
|
this.universe = universe;
|
||||||
|
|
||||||
this.mat = new JButton[height][width];
|
this.mat = new JButton[height][width];
|
||||||
|
|
||||||
for (int i = 0; i < this.height; i++) {
|
for (int i = 0; i < this.height; i++) {
|
||||||
for (int j = 0; j < this.width; j++) {
|
for (int j = 0; j < this.width; j++) {
|
||||||
this.mat[i][j] = new JButton();
|
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)));
|
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_i = i;
|
||||||
final int coord_j = j;
|
final int coord_j = j;
|
||||||
|
final int final_selected = this.selected;
|
||||||
|
final Universe tempUniverse = this.universe;
|
||||||
|
|
||||||
this.mat[i][j].addActionListener(e -> {
|
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]);
|
super.add(this.mat[i][j]);
|
||||||
|
|
|
@ -18,7 +18,7 @@ import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import universe.*;
|
import universe.Universe;
|
||||||
|
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame {
|
||||||
|
|
||||||
|
@ -96,8 +96,9 @@ public class Window extends JFrame {
|
||||||
int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width"));
|
int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width"));
|
||||||
int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height"));
|
int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height"));
|
||||||
|
|
||||||
|
this.universe.changeUniverseDim(width + 2, height + 2);
|
||||||
this.panel.remove(this.grid);
|
this.panel.remove(this.grid);
|
||||||
this.grid = new Grid(width, height);
|
this.grid = new Grid(width, height, this.universe);
|
||||||
this.panel.add(this.grid);
|
this.panel.add(this.grid);
|
||||||
super.pack();
|
super.pack();
|
||||||
super.repaint();
|
super.repaint();
|
||||||
|
@ -111,7 +112,7 @@ public class Window extends JFrame {
|
||||||
this.grid.setSelected(0);
|
this.grid.setSelected(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2);
|
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe);
|
||||||
this.panel.add(grid, BorderLayout.CENTER);
|
this.panel.add(grid, BorderLayout.CENTER);
|
||||||
|
|
||||||
super.setJMenuBar(menuBar);
|
super.setJMenuBar(menuBar);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue