From aae0e78bacfafb9a9a5033e0dcff6b897442eb21 Mon Sep 17 00:00:00 2001 From: fdai7012 Date: Wed, 17 Jan 2024 16:11:32 +0100 Subject: [PATCH] Added input storage to ui --- src/main/java/pacmanGame/MyKeyListener.java | 29 +++++++++++++++++++++ src/main/java/pacmanGame/Program.java | 21 ++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/main/java/pacmanGame/MyKeyListener.java diff --git a/src/main/java/pacmanGame/MyKeyListener.java b/src/main/java/pacmanGame/MyKeyListener.java new file mode 100644 index 0000000..9f48971 --- /dev/null +++ b/src/main/java/pacmanGame/MyKeyListener.java @@ -0,0 +1,29 @@ +package pacmanGame; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.ArrayList; + +import javax.swing.JFrame; + +public class MyKeyListener implements KeyListener { + + public ArrayList inputList = new ArrayList<>(); + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + } + + @Override + public void keyPressed(KeyEvent e) { + inputList.add(e.getKeyChar()); + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/pacmanGame/Program.java b/src/main/java/pacmanGame/Program.java index c4664ee..274dbe3 100644 --- a/src/main/java/pacmanGame/Program.java +++ b/src/main/java/pacmanGame/Program.java @@ -1,19 +1,29 @@ package pacmanGame; import java.awt.*; + import javax.swing.*; public class Program { - public static JTextArea textArea; + public MyKeyListener input; + public JTextArea textArea; + public JFrame frame; public static void main(String[] args) { - createAndShowGUI(); + Program instance = new Program(); + instance.StartGame(); + } + + public void StartGame() { + + CreateAndShowGUI(); + } - private static void createAndShowGUI() { - JFrame frame = new JFrame("PacmaaaAAAYYYHAAAaaam"); + public void CreateAndShowGUI() { + frame = new JFrame("PacmaaaAAAYYYHAAAaaam"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 665); @@ -30,5 +40,8 @@ public class Program { frame.add(textArea, BorderLayout.CENTER); frame.setLocationRelativeTo(null); frame.setVisible(true); + + input = new MyKeyListener(); + frame.addKeyListener(input); } }