Skip to content

Instantly share code, notes, and snippets.

@mambax
Created July 15, 2015 06:13
Show Gist options
  • Save mambax/ad78d37918de6320733b to your computer and use it in GitHub Desktop.
Save mambax/ad78d37918de6320733b to your computer and use it in GitHub Desktop.
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class CBallMaze extends JFrame {
//Below is where I have declared all the different objects I have used
// throughout my program
private JButton buttonRight, buttonLeft, buttonUp, buttonDown, buttonTL,
buttonTR, buttonBL, buttonBR, buttonCenter, optionOne, optionTwo,
optionThree, optionExit, scenarioAct, scenarioRun, scenarioReset,
compassPH;
private JButton[] game = new JButton[208];
private JPanel panelCentre, panelRight, panelBottom, buttonPanel,
compassPanel, optionsPanel, selectionPanel, panelAct, panelRun,
panelReset, panelSlider;
private JTextField optionTF, squareTF, directionTF;
private JLabel option, square, direction, compassDirection;
private JSlider speedSlider;
private String firstOption = "1", secondOption = "2", thirdOption = "3",
upDirection = "North", rightDirection = "East", downDirection =
"South", leftDirection = "West";
private int i;
private int[] map = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3,
3, 1, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3,
3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3,
3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 1, 3, 3,
3, 1, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 3, 1, 3, 3,
3, 3, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, };
final ImageIcon iconCompassNorth = new ImageIcon("north.jpg");
final ImageIcon iconCompassWest = new ImageIcon("west.jpg");
final ImageIcon iconCompassSouth = new ImageIcon("south.jpg");
final ImageIcon iconCompassEast = new ImageIcon("east.jpg");
ImageIcon iconReset = new ImageIcon("Reset.jpg");
ImageIcon iconRun = new ImageIcon("Run.jpg");
ImageIcon iconAct = new ImageIcon("Act.jpg");
ImageIcon iconSand, iconWhite, iconBall, iconEnd;
MoveAction actions = new MoveAction();
int indexOfBall = 0;
public CBallMaze(String title) {
super(title);
}
public static void main(String[] args) {
CBallMaze cBallMaze = new CBallMaze("CBallMaze Ball Maze Application");
cBallMaze.setSize(775, 650);
cBallMaze.createGUI();
cBallMaze.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());
//Panels
panelCentre = new JPanel();
panelCentre.setPreferredSize(new Dimension(625, 450));
panelCentre.setBackground(Color.BLACK);
window.add(panelCentre);
panelCentre.setLayout(new GridLayout(13, 16));
panelRight = new JPanel();
panelRight.setPreferredSize(new Dimension(180, 450));
panelRight.setBackground(Color.WHITE);
window.add(panelRight, BorderLayout.EAST);
optionsPanel = new JPanel();
optionsPanel.setPreferredSize(new Dimension(150, 100));
optionsPanel.setBackground(Color.WHITE);
panelRight.add(optionsPanel, BorderLayout.EAST);
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(175, 100));
buttonPanel.setBackground(Color.WHITE);
panelRight.add(buttonPanel, BorderLayout.EAST);
selectionPanel = new JPanel();
selectionPanel.setPreferredSize(new Dimension(175, 150));
selectionPanel.setBackground(Color.WHITE);
panelRight.add(selectionPanel, BorderLayout.EAST);
ImageIcon cw = new ImageIcon("west.jpg");
compassPanel = new JPanel();
compassPanel.setPreferredSize(new Dimension(175, 300));
compassPanel.setBackground(Color.WHITE);
panelRight.add(compassPanel, BorderLayout.EAST);
panelBottom = new JPanel();
panelBottom.setPreferredSize(new Dimension(875, 50));
panelBottom.setBackground(Color.WHITE);
window.add(panelBottom, BorderLayout.SOUTH);
panelAct = new JPanel();
panelAct.setPreferredSize(new Dimension(125, 40));
panelAct.setBackground(Color.WHITE);
panelBottom.add(panelAct, BorderLayout.SOUTH);
panelRun = new JPanel();
panelRun.setPreferredSize(new Dimension(125, 40));
panelRun.setBackground(Color.WHITE);
panelBottom.add(panelRun, BorderLayout.SOUTH);
panelReset = new JPanel();
panelReset.setPreferredSize(new Dimension(125, 40));
panelReset.setBackground(Color.WHITE);
panelBottom.add(panelReset, BorderLayout.SOUTH);
panelSlider = new JPanel();
panelSlider.setPreferredSize(new Dimension(200, 40));
panelSlider.setBackground(Color.WHITE);
panelBottom.add(panelSlider, BorderLayout.SOUTH);
//Displays
option = new JLabel("Option: ");
optionsPanel.add(option, BorderLayout.LINE_START);
option.setEnabled(true);
option.setForeground(Color.BLACK);
option.setHorizontalAlignment(JLabel.LEFT);
optionTF = new JTextField("1");
optionsPanel.add(optionTF, BorderLayout.LINE_END);
optionTF.setEnabled(true);
optionTF.setPreferredSize(new Dimension(50, 25));
optionTF.setHorizontalAlignment(JTextField.CENTER);
square = new JLabel("Square: ");
optionsPanel.add(square, BorderLayout.LINE_START);
square.setEnabled(true);
square.setForeground(Color.BLACK);
square.setHorizontalAlignment(JLabel.LEFT);
squareTF = new JTextField("");
optionsPanel.add(squareTF, BorderLayout.LINE_END);
squareTF.setEnabled(true);
squareTF.setPreferredSize(new Dimension(50, 25));
squareTF.setHorizontalAlignment(JTextField.CENTER);
direction = new JLabel("Direction: ");
optionsPanel.add(direction, BorderLayout.LINE_START);
direction.setEnabled(true);
direction.setForeground(Color.BLACK);
direction.setHorizontalAlignment(JLabel.LEFT);
directionTF = new JTextField("Still");
optionsPanel.add(directionTF, BorderLayout.LINE_END);
directionTF.setEnabled(true);
directionTF.setPreferredSize(new Dimension(50, 25));
directionTF.setHorizontalAlignment(JTextField.CENTER);
//buttons
buttonTL = new JButton("");
buttonPanel.add(buttonTL);
buttonTL.setPreferredSize(new Dimension(45, 25));
buttonTL.setEnabled(false);
buttonUp = new JButton("^");
buttonPanel.add(buttonUp);
buttonUp.setPreferredSize(new Dimension(45, 25));
buttonUp.addActionListener(actions);
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
directionTF.setText(upDirection);
compassDirection.setIcon(iconCompassNorth);
}
});
buttonTR = new JButton("");
buttonPanel.add(buttonTR);
buttonTR.setPreferredSize(new Dimension(45, 25));
buttonTR.setEnabled(false);
buttonLeft = new JButton("<");
buttonPanel.add(buttonLeft);
buttonLeft.setPreferredSize(new Dimension(45, 25));
buttonLeft.addActionListener(actions);
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
directionTF.setText(leftDirection);
compassDirection.setIcon(iconCompassWest);
}
});
buttonCenter = new JButton("");
buttonPanel.add(buttonCenter);
buttonCenter.setPreferredSize(new Dimension(45, 25));
buttonCenter.setEnabled(false);
buttonRight = new JButton(">");
buttonPanel.add(buttonRight);
buttonRight.setPreferredSize(new Dimension(45, 25));
buttonRight.addActionListener(actions);
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
directionTF.setText(rightDirection);
compassDirection.setIcon(iconCompassEast);
}
});
buttonBL = new JButton("");
buttonPanel.add(buttonBL);
buttonBL.setPreferredSize(new Dimension(45, 25));
buttonBL.setEnabled(false);
buttonDown = new JButton("v");
buttonPanel.add(buttonDown);
buttonDown.setPreferredSize(new Dimension(45, 25));
buttonDown.addActionListener(actions);
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
directionTF.setText(downDirection);
compassDirection.setIcon(iconCompassSouth);
}
});
buttonBR = new JButton("");
buttonPanel.add(buttonBR);
buttonBR.setPreferredSize(new Dimension(45, 25));
buttonBR.setEnabled(false);
optionOne = new JButton("Option One");
selectionPanel.add(optionOne);
optionOne.setPreferredSize(new Dimension(125, 25));
optionOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionTF.setText(firstOption);
}
});
optionTwo = new JButton("Option Two");
selectionPanel.add(optionTwo);
optionTwo.setPreferredSize(new Dimension(125, 25));
optionTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionTF.setText(secondOption);
}
});
optionThree = new JButton("Option Three");
selectionPanel.add(optionThree);
optionThree.setPreferredSize(new Dimension(125, 25));
optionThree.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionTF.setText(thirdOption);
}
});
optionExit = new JButton("Exit");
selectionPanel.add(optionExit);
optionExit.setPreferredSize(new Dimension(125, 25));
scenarioAct = new JButton("Act");
scenarioAct.setIcon(iconAct);
panelAct.add(scenarioAct);
scenarioAct.addActionListener(actions);
scenarioRun = new JButton("Run");
scenarioRun.setIcon(iconRun);
panelRun.add(scenarioRun);
scenarioRun.addActionListener(actions);
scenarioReset = new JButton("Reset");
scenarioReset.setIcon(iconReset);
panelReset.add(scenarioReset);
scenarioReset.addActionListener(actions);
JSlider speedSlider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
speedSlider.setMajorTickSpacing(10);
speedSlider.setMinorTickSpacing(5);
speedSlider.setPaintTicks(true);
speedSlider.setBackground(Color.WHITE);
speedSlider.setLabelTable(speedSlider.createStandardLabels(10));
panelSlider.add(speedSlider);
compassDirection = new JLabel();
compassPanel.add(compassDirection);
try {
iconSand = new ImageIcon("sand.jpg");
} catch (Exception e) {
System.err.println("Sand Icon " + e);
}
try {
iconBall = new ImageIcon("sand60x60.png");
} catch (Exception e) {
System.err.println("Ball Icon " + e);
}
try {
iconWhite = new ImageIcon("white32x32.jpg");
} catch (Exception e) {
System.err.println("White Icon " + e);
}
try {
iconEnd = new ImageIcon("sandstone.jpg");
} catch (Exception e) {
System.err.println("End Icon" + e);
}
for (i = 0; i < 208; i++) {
game[i] = new JButton();
if (map[i] == 1) {
game[i].setIcon(iconSand);
}
if (map[i] == 2) {
game[i].setIcon(iconBall);
}
if (map[i] == 3) {
game[i].setIcon(iconWhite);
}
if (map[i] == 4) {
game[i].setIcon(iconEnd);
}
game[i].setBorder(null);
game[i].setPreferredSize(new Dimension(32, 32));
game[i].addActionListener(new MoveAction());
panelCentre.add(game[i]);
}
KeyboardFocusManager manager = KeyboardFocusManager
.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
game[indexOfBall].setText("o");
}
class MoveAction implements ActionListener, KeyListener {
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case ">":
moveRight();
break;
case "<":
moveLeft();
break;
case "v":
moveDown();
break;
case "^":
moveUp();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
moveRight();
break;
case KeyEvent.VK_LEFT:
moveLeft();
break;
case KeyEvent.VK_DOWN:
moveDown();
break;
case KeyEvent.VK_UP:
moveUp();
}
System.out.println(indexOfBall);
}
void moveRight() {
if (indexOfBall == 0 || indexOfBall % 16 != 15) {
game[indexOfBall].setText("");
indexOfBall = indexOfBall + 1;
game[indexOfBall].setText("o");
} else {
System.out.println("You are at the right end of the maze");
}
}
void moveLeft() {
if ( indexOfBall % 16 != 0) {
game[indexOfBall].setText("");
indexOfBall = indexOfBall - 1;
game[indexOfBall].setText("o");
} else {
System.out.println("You are at the left end of the maze");
}
}
void moveUp() {
if (indexOfBall > 12) {
game[indexOfBall].setText("");
indexOfBall = indexOfBall - 16;
game[indexOfBall].setText("o");
} else {
System.out.println("You are at the top end of the maze");
}
}
void moveDown() {
if (indexOfBall < 192) {
game[indexOfBall].setText("");
indexOfBall = indexOfBall + 16;
game[indexOfBall].setText("o");
} else {
System.out.println("You are at the bottom end of the maze");
}
}
}
private class MyDispatcher implements KeyEventDispatcher {
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
actions.keyPressed(e);
} else if (e.getID() == KeyEvent.KEY_RELEASED) {
actions.keyReleased(e);
} else if (e.getID() == KeyEvent.KEY_TYPED) {
actions.keyTyped(e);
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment