code factorization for direction buttons
Signed-off-by: philippe lhardy <philippe@pavilionartlogiciel>
This commit is contained in:
@@ -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,11 +152,24 @@ 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,
|
||||
MazeResolutionListener
|
||||
|
||||
Reference in New Issue
Block a user