迷宫Main函数

 package maze;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import maze.eng.manage;
import maze.eng.mapBuilderChilderOne;
import maze.eng.mapBuilderChilderTwo;
import maze.eng.mapBuilderFarther;
import maze.eng.walkControl;

public class Main extends JFrame
{
    int keepGUI = 19;
    int b = (keepGUI - 2) * 2 + 2;
    private JLabel[][] area = new JLabel[b][b];
    int xContStar;
    int yContStar;
    int xContEnd;
    int yContEnd;
    mapBuilderFarther builderKeeper;
    
    manage theManage;
    ImageIcon iconsOne = new ImageIcon(getClass().getResource("1.jpg"));
    ImageIcon iconsTwo = new ImageIcon(getClass().getResource("2.jpg"));
    ImageIcon iconsThr = new ImageIcon(getClass().getResource("3.jpg"));
    ImageIcon iconsFou = new ImageIcon(getClass().getResource("4.jpg"));
    
    private JMenuItem jmiOpen = new JMenuItem("新游戏");
    private JMenuItem jmiClose = new JMenuItem("退出");
    private JMenuItem chooseOne = new JMenuItem("简单");
    private JMenuItem chooseTwo = new JMenuItem("中等");
    private JMenuItem chooseThr = new JMenuItem("困难");
    private JMenuItem chooseFou = new JMenuItem("模式1");
    private JMenuItem chooseFiv = new JMenuItem("模式2");
    
    public Main(String title)
    {
        super(title);
        
        xContStar = 0;
        yContStar = 0;
        xContEnd = 0;
        yContEnd = 0;
        
        builderKeeper = new mapBuilderChilderOne();
        
        final walkControl control = new walkControl();
        
        JMenu JmenuOne = new JMenu("开始");
        JmenuOne.add(jmiOpen);
        JmenuOne.add(jmiClose);
        
        JMenu JmenuTwo = new JMenu("难度");
        JmenuTwo.add(chooseOne);
        JmenuTwo.add(chooseTwo);
        JmenuTwo.add(chooseThr);
        JmenuTwo.add(chooseFou);
        JmenuTwo.add(chooseFiv);
        
        JMenuBar jmenuBar = new JMenuBar();
        jmenuBar.add(JmenuOne);
        jmenuBar.add(JmenuTwo);
        
        theManage = builderKeeper.getMap(b);
        
        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(keepGUI,keepGUI));
        
        final JPanel contentButton = new JPanel();
        contentButton.setLayout(new BorderLayout());
        contentButton.add(jmenuBar,BorderLayout.WEST);
        
        Container contentPaneAll = getContentPane();
        
        int count = 0;
        
        contentPaneAll.add(contentPane, BorderLayout.CENTER);
        contentPaneAll.add(contentButton, BorderLayout.NORTH);
                    
        addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent ev)
            {
                int key = ev.getKeyCode();
                
                control.walkCont(key, theManage);
                
                printWhe();
                int cI = 0;
                int cJ = 0;
                for(int i = xContStar; i < xContEnd; i++)
                {
                    cJ = 0;
                    for(int j = yContStar; j < yContEnd; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[cI][cJ].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[cI][cJ].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[cI][cJ].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[cI][cJ].setIcon(iconsFou);
                        }
                        cJ++;
                    }
                    cI++;
                }
            }
        });
        
        chooseOne.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                b = keepGUI;
                theManage = builderKeeper.getMap(b);
                
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        chooseTwo.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                b = (keepGUI - 2) * 2 + 2;
                theManage = builderKeeper.getMap(b);
                 
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        chooseThr.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                b = (keepGUI - 2) * 6 + 2;
                theManage = builderKeeper.getMap(b);
                 
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        chooseFou.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                builderKeeper = new mapBuilderChilderOne();
                theManage = builderKeeper.getMap(b);
                
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        chooseFiv.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                builderKeeper = new mapBuilderChilderTwo();
                theManage = builderKeeper.getMap(b);
                
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        jmiOpen.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                theManage = builderKeeper.getMap(b);
                 
                xContStar = 0;
                yContStar = 0;
                xContEnd = 0;
                yContEnd = 0;
                
                for(int i = 0; i < keepGUI; i++)
                {
                    for(int j = 0; j < keepGUI; j++)
                    {
                        if(theManage.getManage(i + 1, j + 1) == '1')
                        {
                            area[i][j].setIcon(iconsOne);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '2')
                        {
                            area[i][j].setIcon(iconsTwo);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '3')
                        {
                            area[i][j].setIcon(iconsThr);
                        }
                        else if(theManage.getManage(i + 1, j + 1) == '4')
                        {
                            area[i][j].setIcon(iconsFou);
                        }
                    }
                }
            }
        });
        
        jmiClose.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        
        for(int i = 0; i < keepGUI; i++)
        {
            for(int j = 0; j < keepGUI; j++)
            {
                if(theManage.getManage(i + 1, j + 1) == '1')
                {
                    area[i][j] = new JLabel(iconsOne);
                }
                else if(theManage.getManage(i + 1, j + 1) == '2')
                {
                    area[i][j] = new JLabel(iconsTwo);
                }
                else if(theManage.getManage(i + 1, j + 1) == '3')
                {
                    area[i][j] = new JLabel(iconsThr);
                }
                else if(theManage.getManage(i + 1, j + 1) == '4')
                {
                    area[i][j] = new JLabel(iconsFou);
                }
            }
        }
        
        for(int i = 0; i < keepGUI; i++)
        {
            for(int j = 0; j < keepGUI; j++)
            {
                contentPane.add(area[j][i]);
            }
        }
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    
    public static void main(String[] args) 
    {
        Main one = new Main("找苹果 -- 牟天工作室");  
    }
    
    private void printWhe()
    {
        int x = theManage.getX();
        int y = theManage.getY();
        
        if(x > keepGUI - 1 && x < b - keepGUI + 2)
        {
            xContStar = ((x - 2) / (keepGUI - 2)) * (keepGUI - 2);
            xContEnd = ((x - 2) / (keepGUI - 2) + 1) * (keepGUI - 2) + 2;
        }
        else if(x <= keepGUI - 1)
        {
            xContStar = 0;
            xContEnd = keepGUI;
        }
        else
        {
            xContStar = b - keepGUI;
            xContEnd = b;
        }
        
        if(y > keepGUI - 1 && y < b - keepGUI + 2)
        {
            yContStar = ((y - 2) / (keepGUI - 2)) * (keepGUI - 2);
            yContEnd = ((y - 2) / (keepGUI - 2) + 1) * (keepGUI - 2) + 2;
        }
        else if(y <= keepGUI - 1)
        {
            yContStar = 0;
            yContEnd = keepGUI;
        }
        else
        {
            yContStar = b - keepGUI;
            yContEnd = b;
        }
                
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值