Browse Source

GUI DTSUD-67 added interactive actions on gamefield in order to show active field selected while mouse clicked or mouse over

remotes/origin/develop
fdai6499 2 years ago
parent
commit
3ea6037b6e
  1. 55
      src/main/java/src/GameField.java
  2. 6
      src/test/java/GameFieldTest.java
  3. 8
      src/test/java/MainTest.java

55
src/main/java/src/GameField.java

@ -3,6 +3,8 @@ package src;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class GameField extends JPanel {
@ -10,6 +12,12 @@ public class GameField extends JPanel {
private JPanel[][] gameField;
private JLabel[][] labelField;
private JPanel activePanel = null;
private static final Color colorDefault = new Color(0, 0, 255, 255);
private static final Color colorOnEntered = new Color(0, 0, 255, 100);
private static final Color colorOnWhenActive = new Color(0, 255, 150, 200);
public Notes[][] getNoteField() {
return noteField;
}
@ -22,6 +30,10 @@ public class GameField extends JPanel {
return labelField;
}
public JPanel getActivePanel() {
return activePanel;
}
/**
* represents the game filed of sudoku
* @param widthHeight describes the field size in pixels which will be seperated into single sudoku fields
@ -49,6 +61,7 @@ public class GameField extends JPanel {
for (int j = 0, addJ = 1; j < nrOfFields; j++) {
JPanel panel = gameField[i][j];
gameField[i][j] = new JPanel();
gameField[i][j].setBorder(new LineBorder(Color.BLACK, 2));
gameField[i][j].setLayout(null);
@ -64,6 +77,7 @@ public class GameField extends JPanel {
noteField[i][j].setBounds(border, border, size-2*border, size-2*border);
gameField[i][j].add(noteField[i][j]);
JLabel lf = labelField[i][j];
labelField[i][j] = new JLabel("", SwingConstants.CENTER);
labelField[i][j].setBounds(0, 0, size, size);
labelField[i][j].setBackground(Color.GREEN);
@ -74,6 +88,47 @@ public class GameField extends JPanel {
labelField[i][j].setBackground(Color.CYAN);
labelField[i][j].setText("0");
gameField[i][j].addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
JPanel component = (JPanel) e.getComponent();
if (activePanel == null) {
activePanel = component;
} else {
activePanel.setBackground(colorDefault);
activePanel = component;
}
component.setBackground(colorOnWhenActive);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
JPanel component = (JPanel) e.getComponent();
if (component != activePanel) {
component.setBackground(colorOnEntered);
}
}
@Override
public void mouseExited(MouseEvent e) {
JPanel component = (JPanel) e.getComponent();
if (!component.getBackground().equals(colorOnWhenActive)) {
component.setBackground(colorDefault);
}
}
});
gameField[i][j].add(labelField[i][j]);
}

6
src/test/java/GameFieldTest.java

@ -1,8 +1,13 @@
import org.junit.Before;
import src.GameField;
import src.Main;
import src.Notes;
import org.junit.jupiter.api.*;
import java.awt.*;
import static org.junit.jupiter.api.Assertions.*;
@ -159,4 +164,5 @@ public class GameFieldTest {
}
}
}
}

8
src/test/java/MainTest.java

@ -2,9 +2,15 @@
import src.GameField;
import org.junit.jupiter.api.*;
import src.Main;
import src.MainFrame;
import java.awt.*;
import static org.junit.jupiter.api.Assertions.*;
public class MainTest {
public class MainTest {
@Test
public void testFieldTest() {

Loading…
Cancel
Save