Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import javax.swing.*;
- public class Test {
- Element element = new Element(5, 7);
- private JPanel[][] cells;
- JLabel title;
- JButton exitButton;
- JPanel mazePanel;
- public void printFrame() {
- JFrame frame;
- mazePanel = new JPanel();
- frame = new JFrame("The Maze");
- frame.setSize(600, 600);
- mazePanel.setLayout(new GridLayout(10, 10));
- cells = new JPanel[10][10];
- mazePanel.setBackground(Color.cyan);
- for (int j = 0; j < 10; j++) {
- for (int i = 0; i < 10; i++) {
- JPanel cell = new JPanel();
- if (i == element.getX() && j == element.getY()) {
- cell.setBackground(Color.RED);
- } else {
- cell.setBackground(Color.GRAY);
- }
- cells[i][j] = cell;
- mazePanel.add(cells[i][j]);
- }
- }
- frame.add(mazePanel);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- public Test() {
- RIGHTaction rightAction = new RIGHTaction();
- printFrame();
- mazePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
- KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "doRIGHTaction");
- mazePanel.getActionMap().put("doRIGHTaction", rightAction);
- }
- class RIGHTaction extends AbstractAction {
- public void actionPerformed(ActionEvent tf) {
- System.out.println("The element is going RIGHT.");
- element.setX(element.getX() + 1);
- }
- }
- public static void main(String[] args) {
- new Test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment