From 69544687ba0baa3780517b7430ae1c71c8cc07f9 Mon Sep 17 00:00:00 2001 From: steyer <steyer10@gmail.com> Date: Mon, 9 Dec 2019 16:33:32 +0100 Subject: [PATCH] GameWindow + GameBoardTable --- src/game/GameBoardTable.java | 17 +++++++++++++++++ src/game/GameCellRenderer.java | 29 +++++++++++++++++++++++++++++ src/game/GameModel.java | 1 - src/game/GameWindow.java | 8 +++----- 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/game/GameBoardTable.java create mode 100644 src/game/GameCellRenderer.java diff --git a/src/game/GameBoardTable.java b/src/game/GameBoardTable.java new file mode 100644 index 0000000..6024049 --- /dev/null +++ b/src/game/GameBoardTable.java @@ -0,0 +1,17 @@ +package game; + +import javax.swing.*; +import javax.swing.table.TableModel; + +public class GameBoardTable extends JTable { + public GameBoardTable (TableModel model){ + super(model); + + int height = getRowHeight(); + for (int i = 0; i<model.getColumnCount(); i++){ + getColumnModel().getColumn(i).setPreferredWidth(height); + } + + setDefaultRenderer(Boolean.class, new GameCellRenderer()); + } +} diff --git a/src/game/GameCellRenderer.java b/src/game/GameCellRenderer.java new file mode 100644 index 0000000..a86c391 --- /dev/null +++ b/src/game/GameCellRenderer.java @@ -0,0 +1,29 @@ +package game; + +import javax.swing.*; +import javax.swing.table.TableCellRenderer; +import java.awt.*; + +public class GameCellRenderer implements TableCellRenderer { + private TableCellRenderer defaultRenderer; + + public GameCellRenderer() { + defaultRenderer = new JTable().getDefaultRenderer(Object.class); + } + + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, + boolean hasFocus, int row, int column) { + + Component component = defaultRenderer.getTableCellRendererComponent(table, null, + false, false, row, column); + + + if ((boolean) table.getValueAt(row, column)) { + component.setBackground(Color.DARK_GRAY); + } else { + component.setBackground(Color.WHITE); + } + + return component; + } +} diff --git a/src/game/GameModel.java b/src/game/GameModel.java index d1b1442..dafc77e 100644 --- a/src/game/GameModel.java +++ b/src/game/GameModel.java @@ -72,7 +72,6 @@ public class GameModel extends AbstractTableModel { public void setValueAt(Object aValue, int rowIndex, int columnIndex) { board.get(rowIndex, columnIndex).setAlive((boolean) aValue); - } public void step() { diff --git a/src/game/GameWindow.java b/src/game/GameWindow.java index 33c7182..d184d4d 100644 --- a/src/game/GameWindow.java +++ b/src/game/GameWindow.java @@ -7,6 +7,8 @@ import java.awt.*; public class GameWindow extends JFrame { + //TODO JMenu + private GameModel model; private JTable table; JFileChooser fileChooser; @@ -14,18 +16,14 @@ public class GameWindow extends JFrame { public GameWindow(GameModel model){ super("Game of Life"); this.model = model; - table = new JTable(model); + table = new GameBoardTable(model); fileChooser = new JFileChooser(); FileFilter filter = new FileNameExtensionFilter("JSON File (.json)", "json"); fileChooser.setFileFilter(filter); - //table.setPreferredSize(new Dimension(300, 300)); //TODO igazodjon a táblához setLayout(new BorderLayout()); add(table, BorderLayout.CENTER); - //setSize(300, 300); TODO törölni - - JPanel bottomPanel = new JPanel(); JButton startButton = new JButton("Start"); JButton stopButton = new JButton("Stop"); -- GitLab