From 3ea6037b6ef3fc574fe57ba76201216314942e2e Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 13 Jan 2023 20:46:09 +0100 Subject: [PATCH] GUI DTSUD-67 added interactive actions on gamefield in order to show active field selected while mouse clicked or mouse over --- src/main/java/src/GameField.java | 55 ++++++++++++++++++++++++++++++++ src/test/java/GameFieldTest.java | 6 ++++ src/test/java/MainTest.java | 8 ++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/java/src/GameField.java b/src/main/java/src/GameField.java index 342abff..bb8c9e6 100644 --- a/src/main/java/src/GameField.java +++ b/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]); } diff --git a/src/test/java/GameFieldTest.java b/src/test/java/GameFieldTest.java index 65cec8d..7b720da 100644 --- a/src/test/java/GameFieldTest.java +++ b/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 { } } } + } \ No newline at end of file diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java index 9fd2fb4..07fd1ab 100644 --- a/src/test/java/MainTest.java +++ b/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() {