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;
|
Object param;
|
||||||
JPanel pane = new JPanel();
|
|
||||||
MazeComponent maze;
|
MazeComponent maze;
|
||||||
MazeControler controler;
|
MazeControler controler;
|
||||||
LabyModel model;
|
LabyModel model;
|
||||||
|
JPanel controlPanel;
|
||||||
|
|
||||||
Display(LabyModel model, MazeComponent comp)
|
Display(LabyModel model, MazeComponent comp)
|
||||||
{
|
{
|
||||||
@@ -47,10 +47,9 @@ public class Display extends JFrame
|
|||||||
this.param=param;
|
this.param=param;
|
||||||
this.model=model;
|
this.model=model;
|
||||||
Container con = this.getContentPane();
|
Container con = this.getContentPane();
|
||||||
con.add(pane);
|
|
||||||
con.add(new JScrollPane(comp), BorderLayout.CENTER);
|
con.add(new JScrollPane(comp), BorderLayout.CENTER);
|
||||||
controler = new MazeControler();
|
controler = new MazeControler();
|
||||||
con.add(controler,BorderLayout.SOUTH);
|
con.add(controler,BorderLayout.NORTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void resolve()
|
void resolve()
|
||||||
@@ -87,7 +86,7 @@ public class Display extends JFrame
|
|||||||
{
|
{
|
||||||
public MazeControler()
|
public MazeControler()
|
||||||
{
|
{
|
||||||
JPanel controlPanel = new JPanel();
|
controlPanel = new JPanel();
|
||||||
JButton button=new JButton("Resolve");
|
JButton button=new JButton("Resolve");
|
||||||
button.addActionListener(new ActionListener() {
|
button.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent evt)
|
public void actionPerformed(ActionEvent evt)
|
||||||
@@ -97,7 +96,7 @@ public class Display extends JFrame
|
|||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
JButton north = new JButton("North");
|
|
||||||
Action goNorth = new AbstractAction()
|
Action goNorth = new AbstractAction()
|
||||||
{
|
{
|
||||||
public void actionPerformed(ActionEvent evt)
|
public void actionPerformed(ActionEvent evt)
|
||||||
@@ -107,13 +106,8 @@ public class Display extends JFrame
|
|||||||
goNorth();
|
goNorth();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
KeyStroke kup=KeyStroke.getKeyStroke("UP");
|
JButton north = addDirection("North", "UP", goNorth);
|
||||||
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");
|
|
||||||
Action goEast = new AbstractAction() {
|
Action goEast = new AbstractAction() {
|
||||||
public void actionPerformed(ActionEvent evt)
|
public void actionPerformed(ActionEvent evt)
|
||||||
{
|
{
|
||||||
@@ -122,13 +116,9 @@ public class Display extends JFrame
|
|||||||
goEast();
|
goEast();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
east.addActionListener(goEast);
|
JButton east = addDirection("East", "RIGHT", 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");
|
|
||||||
Action goWest = new AbstractAction() {
|
Action goWest = new AbstractAction() {
|
||||||
public void actionPerformed(ActionEvent evt)
|
public void actionPerformed(ActionEvent evt)
|
||||||
{
|
{
|
||||||
@@ -137,13 +127,8 @@ public class Display extends JFrame
|
|||||||
goWest();
|
goWest();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
west.addActionListener(goWest);
|
JButton west = addDirection("West", "LEFT", 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");
|
|
||||||
Action goSouth = new AbstractAction() {
|
Action goSouth = new AbstractAction() {
|
||||||
public void actionPerformed(ActionEvent evt)
|
public void actionPerformed(ActionEvent evt)
|
||||||
{
|
{
|
||||||
@@ -152,19 +137,12 @@ public class Display extends JFrame
|
|||||||
goSouth();
|
goSouth();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
south.addActionListener(goSouth);
|
JButton south = addDirection("South", "DOWN", 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);
|
|
||||||
|
|
||||||
controlPanel.add(button,BorderLayout.NORTH);
|
add(button,BorderLayout.CENTER);
|
||||||
controlPanel.add(north,BorderLayout.NORTH);
|
controlPanel.add(north,BorderLayout.NORTH);
|
||||||
controlPanel.add(east,BorderLayout.EAST);
|
|
||||||
controlPanel.add(west,BorderLayout.WEST);
|
controlPanel.add(west,BorderLayout.WEST);
|
||||||
|
controlPanel.add(east,BorderLayout.EAST);
|
||||||
controlPanel.add(south,BorderLayout.SOUTH);
|
controlPanel.add(south,BorderLayout.SOUTH);
|
||||||
add(controlPanel,BorderLayout.NORTH);
|
add(controlPanel,BorderLayout.NORTH);
|
||||||
final JSlider slider=new JSlider(2,40);
|
final JSlider slider=new JSlider(2,40);
|
||||||
@@ -174,11 +152,24 @@ public class Display extends JFrame
|
|||||||
setWallSize(slider.getValue());
|
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
|
private static class MazeComponent extends JComponent
|
||||||
implements MazeCreationListener,
|
implements MazeCreationListener,
|
||||||
MazeResolutionListener
|
MazeResolutionListener
|
||||||
|
|||||||
Reference in New Issue
Block a user