I am a bit to new to this Java bit of coding. The problem is that I have a different class for KeyListener and an image that I want to display if the user presses a key, but the image is from JLabel and I want to add it in JFrame of the main class but I am not understanding how I would do that. Yes I did see another question, this one:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Map<KeyHandler.Direction, JPanel> mapPanels;
private KeyHandler keyHandler;
public TestPane() {
mapPanels = new HashMap<>();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(createPanel(KeyHandler.Direction.LEFT_UP), gbc);
gbc.gridx ;
add(createPanel(KeyHandler.Direction.UP), gbc);
gbc.gridx ;
add(createPanel(KeyHandler.Direction.RIGHT_UP), gbc);
gbc.gridx = 0;
gbc.gridy ;
add(createPanel(KeyHandler.Direction.LEFT), gbc);
gbc.gridx ;
add(createPanel(), gbc);
gbc.gridx ;
add(createPanel(KeyHandler.Direction.RIGHT), gbc);
gbc.gridx = 0;
gbc.gridy ;
add(createPanel(KeyHandler.Direction.LEFT_DOWN), gbc);
gbc.gridx ;
add(createPanel(KeyHandler.Direction.DOWN), gbc);
gbc.gridx ;
add(createPanel(KeyHandler.Direction.RIGHT_DOWN), gbc);
keyHandler = new KeyHandler(getInputMap(WHEN_IN_FOCUSED_WINDOW), getActionMap(), new KeyHandlerListener() {
@Override
public void keyHandlerDidPress(KeyHandler.Direction direction) {
activate(direction);
}
});
}
protected JPanel createPanel() {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
panel.setBorder(new LineBorder(Color.DARK_GRAY));
return panel;
}
protected JPanel createPanel(KeyHandler.Direction direction) {
JPanel panel = createPanel();
mapPanels.put(direction, panel);
return panel;
}
protected void activate(KeyHandler.Direction direction) {
JPanel panel = mapPanels.get(direction);
panel.setBackground(Color.BLUE);
Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setBackground(null);
}
});
timer.setRepeats(false);
timer.start();
}
}
public interface KeyHandlerListener {
public void keyHandlerDidPress(KeyHandler.Direction direction);
}
public class KeyHandler {
public enum Direction {
UP, DOWN, LEFT, RIGHT,
LEFT_UP, RIGHT_UP,
LEFT_DOWN, RIGHT_DOWN
}
public KeyHandler(InputMap inputMap, ActionMap actionMap, KeyHandlerListener listener) {
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "Press.leftUp");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "Press.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_E, 0), "Press.rightUp");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "Press.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "Press.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0), "Press.leftDown");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0), "Press.down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, 0), "Press.rightDown");
actionMap.put("Press.leftUp", new DirectionAction(Direction.LEFT_UP, listener));
actionMap.put("Press.up", new DirectionAction(Direction.UP, listener));
actionMap.put("Press.rightUp", new DirectionAction(Direction.RIGHT_UP, listener));
actionMap.put("Press.left", new DirectionAction(Direction.LEFT, listener));
actionMap.put("Press.right", new DirectionAction(Direction.RIGHT, listener));
actionMap.put("Press.leftDown", new DirectionAction(Direction.LEFT_DOWN, listener));
actionMap.put("Press.down", new DirectionAction(Direction.DOWN, listener));
actionMap.put("Press.rightDown", new DirectionAction(Direction.RIGHT_DOWN, listener));
}
protected class DirectionAction extends AbstractAction {
private Direction direction;
private KeyHandlerListener listener;
public DirectionAction(Direction direction, KeyHandlerListener listener) {
this.direction = direction;
this.listener = listener;
}
@Override
public void actionPerformed(ActionEvent e) {
listener.keyHandlerDidPress(direction);
}
}
}
}