From a041e3d82c35312e12170611860ee422d2e9a947 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Sat, 27 Dec 2014 18:56:03 +0100 Subject: [PATCH] code factorization for direction buttons Signed-off-by: philippe lhardy --- java/org/artisanlogiciel/games/Display.java | 65 +++++++++------------ 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index 44606b4..9dc4ed8 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -35,10 +35,10 @@ public class Display extends JFrame { Object param; - JPanel pane = new JPanel(); MazeComponent maze; MazeControler controler; LabyModel model; + JPanel controlPanel; Display(LabyModel model, MazeComponent comp) { @@ -47,10 +47,9 @@ public class Display extends JFrame this.param=param; this.model=model; Container con = this.getContentPane(); - con.add(pane); con.add(new JScrollPane(comp), BorderLayout.CENTER); controler = new MazeControler(); - con.add(controler,BorderLayout.SOUTH); + con.add(controler,BorderLayout.NORTH); } void resolve() @@ -87,7 +86,7 @@ public class Display extends JFrame { public MazeControler() { - JPanel controlPanel = new JPanel(); + controlPanel = new JPanel(); JButton button=new JButton("Resolve"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) @@ -97,7 +96,7 @@ public class Display extends JFrame resolve(); } }); - JButton north = new JButton("North"); + Action goNorth = new AbstractAction() { public void actionPerformed(ActionEvent evt) @@ -107,13 +106,8 @@ public class Display extends JFrame goNorth(); } }; - KeyStroke kup=KeyStroke.getKeyStroke("UP"); - getInputMap().put(kup,"goNorth"); - getActionMap().put("goNorth",goNorth); - controlPanel.getInputMap().put(kup,"goNorth"); - controlPanel.getActionMap().put("goNorth",goNorth); - north.getActionMap().put("goNorth",goNorth); - JButton east = new JButton("East"); + JButton north = addDirection("North", "UP", goNorth); + Action goEast = new AbstractAction() { public void actionPerformed(ActionEvent evt) { @@ -122,13 +116,9 @@ public class Display extends JFrame goEast(); } }; - east.addActionListener(goEast); - KeyStroke kright=KeyStroke.getKeyStroke("RIGHT"); - getInputMap().put(kright,"goEast"); - getActionMap().put("goEast",goEast); - controlPanel.getInputMap().put(kright,"goEast"); - controlPanel.getActionMap().put("goEast",goEast); - JButton west = new JButton("West"); + JButton east = addDirection("East", "RIGHT", goEast); + + Action goWest = new AbstractAction() { public void actionPerformed(ActionEvent evt) { @@ -137,13 +127,8 @@ public class Display extends JFrame goWest(); } }; - west.addActionListener(goWest); - KeyStroke kwest=KeyStroke.getKeyStroke("LEFT"); - getInputMap().put(kwest,"goWest"); - getActionMap().put("goWest",goWest); - controlPanel.getInputMap().put(kwest,"goWest"); - controlPanel.getActionMap().put("goWest",goWest); - JButton south = new JButton("South"); + JButton west = addDirection("West", "LEFT", goWest); + Action goSouth = new AbstractAction() { public void actionPerformed(ActionEvent evt) { @@ -152,19 +137,12 @@ public class Display extends JFrame goSouth(); } }; - south.addActionListener(goSouth); - KeyStroke kdown=KeyStroke.getKeyStroke("DOWN"); - controlPanel.getInputMap().put(kdown,"goSouth"); - controlPanel.getActionMap().put("goSouth",goSouth); - getInputMap().put(KeyStroke.getKeyStroke("DOWN"),"goSouth"); - getActionMap().put("goSouth",goSouth); - controlPanel.getInputMap().put(KeyStroke.getKeyStroke("DOWN"),"goSouth"); - controlPanel.getActionMap().put("goSouth",goSouth); + JButton south = addDirection("South", "DOWN", goSouth); - controlPanel.add(button,BorderLayout.NORTH); + add(button,BorderLayout.CENTER); controlPanel.add(north,BorderLayout.NORTH); - controlPanel.add(east,BorderLayout.EAST); controlPanel.add(west,BorderLayout.WEST); + controlPanel.add(east,BorderLayout.EAST); controlPanel.add(south,BorderLayout.SOUTH); add(controlPanel,BorderLayout.NORTH); final JSlider slider=new JSlider(2,40); @@ -174,10 +152,23 @@ public class Display extends JFrame setWallSize(slider.getValue()); } }); - add(slider,BorderLayout.SOUTH); + add(slider,BorderLayout.EAST); } } + + private JButton addDirection(final String direction, String key, Action goAction) + { + String actionName = "go" + direction; + JButton button = new JButton(direction); + button.addActionListener(goAction); + KeyStroke keystroke=KeyStroke.getKeyStroke(key); + //getInputMap().put(keystroke,actionName); + //getActionMap().put(actionName,goAction); + controlPanel.getInputMap().put(keystroke,actionName); + controlPanel.getActionMap().put(actionName,goAction); + return button; + } private static class MazeComponent extends JComponent implements MazeCreationListener,