keys are bound to maze display

and get focus when mous mouse over it
This commit is contained in:
philippe lhardy
2017-12-02 19:18:17 +01:00
parent 3222df4256
commit 52d244c758

View File

@@ -497,10 +497,9 @@ public class Display extends JFrame
JButton button = new JButton(direction); JButton button = new JButton(direction);
button.addActionListener(goAction); button.addActionListener(goAction);
KeyStroke keystroke = KeyStroke.getKeyStroke(key); KeyStroke keystroke = KeyStroke.getKeyStroke(key);
panel.getInputMap().put(keystroke, actionName); // attach keys to maze
panel.getActionMap().put(actionName, goAction); maze.getInputMap().put(keystroke, actionName);
controlPanel.getInputMap().put(keystroke, actionName); maze.getActionMap().put(actionName, goAction);
controlPanel.getActionMap().put(actionName, goAction);
return button; return button;
} }
@@ -681,14 +680,19 @@ public class Display extends JFrame
// not set by default for debug, will show any path // not set by default for debug, will show any path
boolean showAll = false; boolean showAll = false;
Position getPosition(int x, int y)
{
int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth());
int pY = (int) ((double) (y - cp.getOffsetY()) / cp.getHeight());
return new Position(pX,pY);
}
@Override @Override
public void mouseDragged(MouseEvent e) { public void mouseDragged(MouseEvent e) {
// should find the cell ... // should find the cell ...
DirectionPosition last = null; DirectionPosition last = null;
int pX = (int) ((double) (e.getX() - cp.getOffsetX()) / cp.getWidth());
int pY = (int) ((double) (e.getY() - cp.getOffsetY()) / cp.getHeight());
short path = 0; short path = 0;
Position newPosition = new Position(pX,pY); Position newPosition = getPosition(e.getX(),e.getY());
if (drawingPath == null ) if (drawingPath == null )
{ {
drawingPath = new LinkedList<>(); drawingPath = new LinkedList<>();
@@ -724,6 +728,11 @@ public class Display extends JFrame
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
// System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); // System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')');
Position newPosition = getPosition(e.getX(),e.getY());
if ( ( newPosition.getX() >= 0 ) && ( newPosition.getY() >= 0 ) )
{
requestFocus();
}
} }
public void setShowAll(boolean showall ) public void setShowAll(boolean showall )
@@ -821,6 +830,10 @@ public class Display extends JFrame
{ {
this.map = map; this.map = map;
solvedPath=null; solvedPath=null;
// could be kept
drawingPath=null;
sX=0;
sY=0;
} }
public void setWallSize(int size) public void setWallSize(int size)