Browse Source

Added new Class Field instead of JPanel to safe Childobject

remotes/origin/develop
fdai6040 2 years ago
parent
commit
9fdb1a80a7
  1. 34
      src/main/java/src/Field.java
  2. 26
      src/main/java/src/GameField.java

34
src/main/java/src/Field.java

@ -0,0 +1,34 @@
package src;
import javax.swing.*;
public class Field extends JPanel {
private int x;
private int y;
private JLabel jLabel;
public JLabel getjLabel() {
return jLabel;
}
public void setMyJLabel(JLabel jLabel){
this.jLabel = jLabel;
}
public int getMyX() {
return x;
}
public int getMyY() {
return y;
}
public void setMyX(int x) {
this.x = x;
}
public void setMyY(int y) {
this.y = y;
}
}

26
src/main/java/src/GameField.java

@ -9,10 +9,10 @@ import java.awt.event.MouseListener;
public class GameField extends JPanel {
private Notes[][] noteField;
private JPanel[][] gameField;
private Field[][] gameField;
private JLabel[][] labelField;
private JPanel activePanel = null;
private Field activePanel = null;
private static final Color colorDefault = new Color(100, 100, 255, 255);
private static final Color colorOnEntered = new Color(0, 0, 255, 100);
@ -30,7 +30,7 @@ public class GameField extends JPanel {
return labelField;
}
public JPanel getActivePanel() {
public Field getActivePanel() {
return activePanel;
}
@ -48,7 +48,7 @@ public class GameField extends JPanel {
this.setVisible(true);
this.setLayout(null);
gameField = new JPanel[nrOfFields][];
gameField = new Field[nrOfFields][];
labelField = new JLabel[nrOfFields][];
noteField = new Notes[nrOfFields][];
@ -56,14 +56,17 @@ public class GameField extends JPanel {
for (int i = 0, addI = 1; i < nrOfFields; i++) {
gameField[i] = new JPanel[nrOfFields];
gameField[i] = new Field[nrOfFields];
labelField[i] = new JLabel[nrOfFields];
noteField[i] = new Notes[nrOfFields];
for (int j = 0, addJ = 1; j < nrOfFields; j++) {
JPanel panel = gameField[i][j];
gameField[i][j] = new JPanel();
gameField[i][j] = new Field();
gameField[i][j].setMyY(j);
gameField[i][j].setMyX(i);
gameField[i][j] = new Field();
gameField[i][j].setBorder(new LineBorder(Color.BLACK, 2));
gameField[i][j].setLayout(null);
gameField[i][j].setBounds(
@ -79,6 +82,7 @@ public class GameField extends JPanel {
gameField[i][j].add(noteField[i][j]);
JLabel lf = labelField[i][j];
gameField[i][j].setMyJLabel(lf);
labelField[i][j] = new JLabel("", SwingConstants.CENTER);
labelField[i][j].setBounds(0, 0, size, size);
labelField[i][j].setBackground(Color.GREEN);
@ -98,7 +102,8 @@ public class GameField extends JPanel {
@Override
public void mousePressed(MouseEvent e) {
JPanel component = (JPanel) e.getComponent();
Field component = (Field) e.getComponent();
if (activePanel == null) {
activePanel = component;
} else {
@ -177,4 +182,9 @@ public class GameField extends JPanel {
}
}
public JLabel getActiveLabel(){
JLabel jLabel = getActivePanel().getjLabel();
return jLabel;
}
}
Loading…
Cancel
Save