Browse Source

Refactoring: getCoordinateFromLabel()

Lorenz Hohmann 3 years ago
parent
commit
2ce6f14a14
  1. 39
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java

39
src/main/java/de/tims/fleetstorm/gui/GameLogic.java

@ -169,13 +169,13 @@ public class GameLogic extends JPanel {
JLabel matchfieldLabel = new JLabel("Spielfeld");
gameInfoContentPanel.add(matchfieldLabel);
matchfieldValue = new JLabel("Eigenes");
matchfieldValue = new JLabel("-");
gameInfoContentPanel.add(matchfieldValue);
JLabel moveLabel = new JLabel("Am Zug");
gameInfoContentPanel.add(moveLabel);
moveValue = new JLabel("Du");
moveValue = new JLabel("-");
gameInfoContentPanel.add(moveValue);
for (int x = 0; x < 10; x++) {
@ -194,9 +194,14 @@ public class GameLogic extends JPanel {
field.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
field.setBackground(Color.RED);
field.revalidate();
field.repaint();
if (!playerMove)
return;
int[] coords = getCoordinateFromLabel(field);
int coordX = coords[0];
int coordY = coords[1];
// TODO add shoot function
}
});
}
@ -248,12 +253,19 @@ public class GameLogic extends JPanel {
return enemy5Ship;
}
private int[] getCoordinateFromLabel(JPanel panel) {
String panelName = panel.getName();
String[] panelNameSplit = panelName.split(":");
int coordX = Integer.parseInt(panelNameSplit[0]);
int coordY = Integer.parseInt(panelNameSplit[1]);
return new int[] { coordX, coordY };
}
private void updateField(boolean playerMove, Matchfield matchfield) {
for (JPanel coordinatePanel : this.fields) {
String panelName = coordinatePanel.getName();
String[] panelNameSplit = panelName.split(":");
int coordX = Integer.parseInt(panelNameSplit[0]);
int coordY = Integer.parseInt(panelNameSplit[1]);
int[] coords = this.getCoordinateFromLabel(coordinatePanel);
int coordX = coords[0];
int coordY = coords[1];
// reset field
coordinatePanel.setBackground(null);
@ -278,6 +290,15 @@ public class GameLogic extends JPanel {
coordinatePanel.revalidate();
coordinatePanel.repaint();
}
// set game infos
if (this.playerMove) {
this.getMatchfieldValue().setText("Gegner");
this.getMoveValue().setText("Du");
} else {
this.getMatchfieldValue().setText("Eigenes");
this.getMoveValue().setText("Gegner");
}
}
/**

Loading…
Cancel
Save