如何将球添加到我可以移动的迷宫中?

时间:2015-07-14 09:43:44

标签: java eclipse maze

我这里有一个迷宫和GUI似乎出错了,目标是使网格13 x 16并且有一个可以通过键盘上的箭头键或GUI控制的球,这个阶段无关紧要。

由于某些原因,我的迷宫形成不是很好,你可以在这里看到 - enter image description here

它应该看起来像enter image description here

我的代码如下:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CBallMaze extends JFrame implements ActionListener 
{
//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,
        };

    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(this);
        panelCentre.add(game[i]);

感谢您的帮助! 以下是面板中心的代码:

        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(21, 30));

0 个答案:

没有答案