Guest User

Untitled

a guest
Apr 1st, 2015
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package test;
  2.  
  3. import java.awt.Color;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.KeyEvent;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Test {
  11.  
  12. Element element = new Element(5, 7);
  13. private JPanel[][] cells;
  14.  
  15. JLabel title;
  16. JButton exitButton;
  17. JPanel mazePanel;
  18.  
  19. public void printFrame() {
  20.  
  21. JFrame frame;
  22. mazePanel = new JPanel();
  23. frame = new JFrame("The Maze");
  24. frame.setSize(600, 600);
  25.  
  26. mazePanel.setLayout(new GridLayout(10, 10));
  27.  
  28. cells = new JPanel[10][10];
  29.  
  30. mazePanel.setBackground(Color.cyan);
  31.  
  32. for (int j = 0; j < 10; j++) {
  33. for (int i = 0; i < 10; i++) {
  34. JPanel cell = new JPanel();
  35. if (i == element.getX() && j == element.getY()) {
  36. cell.setBackground(Color.RED);
  37. } else {
  38. cell.setBackground(Color.GRAY);
  39. }
  40. cells[i][j] = cell;
  41. mazePanel.add(cells[i][j]);
  42.  
  43. }
  44.  
  45. }
  46.  
  47. frame.add(mazePanel);
  48. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.  
  50. frame.pack();
  51. frame.setVisible(true);
  52. }
  53.  
  54. public Test() {
  55. RIGHTaction rightAction = new RIGHTaction();
  56.  
  57. printFrame();
  58.  
  59. mazePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
  60. KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "doRIGHTaction");
  61. mazePanel.getActionMap().put("doRIGHTaction", rightAction);
  62.  
  63. }
  64.  
  65. class RIGHTaction extends AbstractAction {
  66. public void actionPerformed(ActionEvent tf) {
  67.  
  68. System.out.println("The element is going RIGHT.");
  69. element.setX(element.getX() + 1);
  70. }
  71.  
  72. }
  73.  
  74. public static void main(String[] args) {
  75. new Test();
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment