Initial Commit

- upload to github
This commit is contained in:
2022-10-03 01:18:19 +02:00
parent 36d76c6eb3
commit 6543c21363
40 changed files with 2782 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package berack96.games.minefield.object.view;
import javax.swing.ImageIcon;
/**
* Classe che contiene solamente le immagini da mostrare nel caso le si vogliano usare
*
* @author Jack
*
*/
public class CellIcons
{
public static final ImageIcon mine = createImageIcon("mine.png");
public static final ImageIcon flag = createImageIcon("flag.png");
public static final ImageIcon flagRight = createImageIcon("flagRight.png");
public static final ImageIcon flagWrong = createImageIcon("flagWrong.png");
public static final ImageIcon one = createImageIcon("1one.png");
public static final ImageIcon two = createImageIcon("2two.png");
public static final ImageIcon three = createImageIcon("3three.png");
public static final ImageIcon four = createImageIcon("4four.png");
public static final ImageIcon five = createImageIcon("5five.png");
public static final ImageIcon six = createImageIcon("6six.png");
public static final ImageIcon seven = createImageIcon("7seven.png");
public static final ImageIcon eight = createImageIcon("8eight.png");
private static final String path = "berack96/games/minefield/assets/icons/";
private static ImageIcon createImageIcon(String name)
{
java.net.URL imgURL = ClassLoader.getSystemResource(path+name);
if (imgURL != null)
return new ImageIcon(imgURL);
else
{
System.err.println("Couldn't find file: " + name);
return null;
}
}
}

View File

@@ -0,0 +1,213 @@
package berack96.games.minefield.object.view;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import berack96.games.minefield.object.Cell;
import berack96.games.minefield.object.CellStatus;
/**
* Classe che serve a restituire graficamente (stringa o jlabel) la cella che si desidera.
*
* @author Jack
*
*/
public class CellView implements Observer {
private final Cell cell;
private String string;
private JLabel label;
/**
* Costruttore: la cella passata, sara' ora osservvata dall'istanza creata
*
* @param cell La cella
*/
public CellView(Cell cell)
{
this.cell = cell;
cell.addObserver(this);
string = CellView.toString(cell);
label = CellView.toJLabel(cell);
}
/**
* Ritorna la rappresentazione della cella sottoforma di stringa:<br>
* [ ] - {@link CellStatus#UNCOVERED} 0 mine vicine<br>
* [n] - {@link CellStatus#UNCOVERED} con "n" mine vicine<br>
* [D] - {@link CellStatus#DANGEROUS}<br>
* [*] - {@link CellStatus#EXPLODED}<br>
* [V] - {@link CellStatus#GOTRIGHT}<br>
* [X] - {@link CellStatus#GOTWRONG}<br>
* [#] - {@link CellStatus#COVERED}<br>
*
* @return String
*/
public String toString()
{
return string;
}
/**
* Ritorna la rappresentazione della cella sottoforma di JLabel:<br>
* {@link CellStatus#UNCOVERED} bianco con eventuale numero<br>
* {@link CellStatus#DANGEROUS} con icona di una bandiera<br>
* {@link CellStatus#EXPLODED} con una icona di una mina<br>
* {@link CellStatus#GOTRIGHT} con una icona di una bandierina cerchiata di verde<br>
* {@link CellStatus#GOTWRONG} - con una icona di una bandierina crociata di rosso<br>
* {@link CellStatus#COVERED} - grigio chiaro<br>
*
* @return JLabel
*/
public JLabel toJLabel()
{
return label;
}
@Override
public void update(Observable arg0, Object arg1)
{
CellStatus status = cell.getStatus();
if(status == CellStatus.UNCOVERED)
{
label.setBackground(Color.WHITE);
label.setIcon(null);
if(cell.nearMine == 0)
string = "[ ]";
else
{
string = "["+cell.nearMine+"]";
switch(cell.nearMine)
{
case 1:
label.setIcon(CellIcons.one);
break;
case 2:
label.setIcon(CellIcons.two);
break;
case 3:
label.setIcon(CellIcons.three);
break;
case 4:
label.setIcon(CellIcons.four);
break;
case 5:
label.setIcon(CellIcons.five);
break;
case 6:
label.setIcon(CellIcons.six);
break;
case 7:
label.setIcon(CellIcons.seven);
break;
case 8:
label.setIcon(CellIcons.eight);
break;
}
}
}
else if(status == CellStatus.DANGEROUS)
{
string = "[D]";
label.setBackground(Color.LIGHT_GRAY);
label.setIcon(CellIcons.flag);
}
else if(status == CellStatus.EXPLODED)
{
string = "[*]";
label.setBackground(Color.LIGHT_GRAY);
label.setIcon(CellIcons.mine);
}
else if(status == CellStatus.GOTRIGHT)
{
string = "[V]";
label.setBackground(Color.LIGHT_GRAY);
label.setIcon(CellIcons.flagRight);
}
else if(status == CellStatus.GOTWRONG)
{
string = "[X]";
label.setBackground(Color.LIGHT_GRAY);
label.setIcon(CellIcons.flagWrong);
}
else
{
string = "[#]";
label.setBackground(Color.LIGHT_GRAY);
label.setIcon(null);
}
}
/**
* Metodo statico che serve, passata una cella, a trasformarla in stringa<br>
* secondo {@link #toString()}.
*
* @param cell La cella da vedere
* @return String
*/
public static String toString(Cell cell)
{
CellStatus status = cell.getStatus();
if(status == CellStatus.UNCOVERED)
{
if(cell.nearMine == 0)
return "[ ]";
else
return "["+cell.nearMine+"]";
}
else if(status == CellStatus.DANGEROUS)
return "[D]";
else if(status == CellStatus.EXPLODED)
return "[*]";
else if(status == CellStatus.GOTRIGHT)
return "[V]";
else
return "[#]";
}
/**
* Metodo statico che serve, passata una cella, a trasformarla in jpanel<br>
* secondo {@link #toJLabel()}.
*
* @param cell La cella da vedere
* @return JLabel
*/
public static JLabel toJLabel(Cell cell)
{
CellStatus status = cell.getStatus();
JLabel label = new JLabel();
label.setOpaque(true);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setPreferredSize(new Dimension(25, 25));
label.setSize(label.getPreferredSize());
label.setVisible(true);
if(status == CellStatus.UNCOVERED)
{
label.setBackground(Color.WHITE);
if(cell.nearMine != 0)
label.setText("["+cell.nearMine+"]");
}
else if(status == CellStatus.DANGEROUS)
label.setBackground(Color.BLACK);
else if(status == CellStatus.EXPLODED)
label.setBackground(Color.RED);
else if(status == CellStatus.GOTRIGHT)
label.setBackground(Color.GREEN);
else
label.setBackground(Color.LIGHT_GRAY);
return label;
}
}

View File

@@ -0,0 +1,119 @@
package berack96.games.minefield.object.view;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import berack96.games.minefield.listener.CellInFieldListener;
import berack96.games.minefield.object.Field;
/**
* Classe che mostra il campo da gioco in modo grafico (tramite stringhe o jpanel).
*
* @author Jack
*
*/
public class FieldView {
private final Field field;
private String string;
private JPanel panel;
/**
* Costruttore in cui si inserisce il campo e un eventuale listener<br>
* su ogni cella del campo
*
* @param field Il campo da vedere
* @param listener il controllore delle celle
*/
public FieldView(Field field, CellInFieldListener listener)
{
this.field = field;
string = toString(field);
panel = toJPanel(field, listener);
}
/**
* Restituisce il campo sottoforma di stringa.<br>
* Per valori celle vedi {@link CellView#toString()}.
*
* @return String
*/
public String toString()
{
string = toString(field);
return string;
}
/**
* Restituisce il campo sottoforma di jpanel.<br>
* Per valori celle vedi {@link CellView#toJLabel()}.
*
* @return JPanel
*/
public JPanel toJPanel()
{
return panel;
}
/**
* Metodo statico che serve, passato un campo, a trasformarlo in stringa<br>
* secondo {@link #toString()}.
*
* @param field il campo da vedere
* @return String
*/
public static String toString(Field field)
{
String string = new String();
for(int i=0; i<field.lines; i++)
{
for(int j=0; j<field.columns; j++)
string += CellView.toString(field.getCell(i, j));
string += "\n";
}
return string;
}
/**
* Metodo statico che serve, passato un campo, a trasformarlo in jpanel<br>
* secondo {@link #toJPanel()}.
*
* @param field il campo da vedere
* @param listener eventuale controller da applicare ad ogni cella
* @return JPanel
*/
public static JPanel toJPanel(Field field, CellInFieldListener listener)
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(field.lines, field.columns));
panel.setVisible(true);
for(int i=0; i<field.lines; i++)
for(int j=0; j<field.columns; j++)
{
JLabel cell = new CellView(field.getCell(i, j)).toJLabel();
cell.setName(i+"-"+j);
if(listener != null)
cell.addMouseListener(listener);
panel.add(cell);
}
Dimension dim = CellView.toJLabel(field.getCell(0, 0)).getPreferredSize();
panel.setPreferredSize(new Dimension(dim.height*field.columns, dim.width*field.lines));
panel.setSize(panel.getPreferredSize());
return panel;
}
}