A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© dapandao 初级黑马   /  2018-8-13 08:51  /  451 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//class RunMain below package MainFrame

package MainFrame;

public class RunMain {
    public static void main(String[] args) {
        new MainFrame();
    }
}


//class VictoryFrame below package MainFrame
package MainFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class VictoryFrame extends JFrame {

    int currentlevel = 0;

    //constructor
    public VictoryFrame() {
        currentlevel = MainFrame.getcurrentlevel();
        this.setVisible(true);
        this.setBounds(700, 450, 100, 100);
        JButton button_next = new JButton("Continue");
        this.add(button_next);
        //reaction of pressing "Continue" button
        button_next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (currentlevel < 5) {
                    MainFrame.setcurrentlevel(currentlevel + 1);
                    new MainFrame();
                }
                dispose();
            }
        });
    }
}


//class MainFrame below package MainFrame
package MainFrame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame implements KeyListener {

    static int currentlevel = 0;

    public static int getcurrentlevel() {
        return currentlevel;
    }

    public static void setcurrentlevel(int level) {
        currentlevel = level;
    }

    // constructor
    public MainFrame() {

        initlevelmap(currentlevel);
        getMapLocation();
        initFrameUI();
        initTree();
        initWolf();
        initTarget();
        initSheeps();
        initBackgroud();
        this.addKeyListener(this);

    }

    // get different map according to different level
    private void initlevelmap(int level) {
        levelmap.add(level0);
        levelmap.add(level1);
        levelmap.add(level2);
        levelmap.add(level3);
        levelmap.add(level4);
        levelmap.add(level5);
        currentmap = levelmap.get(level);
    }

    // new ImageIcon objects, ready for using
    Icon bg = new ImageIcon("bg.png");
    Icon target = new ImageIcon("target.png");
    Icon sheep_cry = new ImageIcon("sheep-cry.png");
    Icon sheep = new ImageIcon("sheep.png");
    Icon tree = new ImageIcon("tree.png");
    Icon victory = new ImageIcon("victory.jpg");
    Icon wolf_down = new ImageIcon("wolf-down.png");
    Icon wolf_left = new ImageIcon("wolf-left.png");
    Icon wolf_right = new ImageIcon("wolf_right.png");
    Icon wolf_up = new ImageIcon("wolf_up.png");

    // JLabel objects
    JLabel j_bg = new JLabel(bg);
    JLabel j_sheep_cry = new JLabel(sheep_cry);
    JLabel j_victory = new JLabel(victory);
    JLabel j_wolf = new JLabel(wolf_down);

    // location of target, sheep, tree and wolf will be saved in arraylist/array
    ArrayList<int[]> targetloc = new ArrayList<int[]>();
    ArrayList<int[]> sheeploc = new ArrayList<int[]>();
    ArrayList<int[]> treeloc = new ArrayList<int[]>();
    int[] wolfloc = new int[2];

    // create JLabel collection for sheep, each specified sheep should be
    // distinguished
    ArrayList<JLabel> j_sheeps = new ArrayList<JLabel>();

    // current array map
    // map will indicate the locations of sheep, wolf and trees.
    int[][] currentmap = new int[16][12];

    // new maps for different levels, different number represent for:
    // 0: empty space, 1: tree, 2: target, 3: wolf, 4: sheep
    static int[][] level0 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 7
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 8
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 9
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };
    static int[][] level1 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 7
            { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 }, // 8
            { 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1 }, // 9
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };
    static int[][] level2 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 }, // 7
            { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1 }, // 8
            { 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1 }, // 9
            { 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };
    static int[][] level3 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1 }, // 7
            { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1 }, // 8
            { 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1 }, // 9
            { 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };
    static int[][] level4 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 }, // 7
            { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 8
            { 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1 }, // 9
            { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };
    static int[][] level5 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 1
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 3
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 4
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 5
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 6
            { 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1 }, // 7
            { 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1 }, // 8
            { 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1 }, // 9
            { 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1 }, // 10
            { 1, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1 }, // 11
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
            { 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, // 13
            { 1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1 }, // 14
            { 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, // 15
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } // 16
    };

    // add maps into levelmap collection
    ArrayList<int[][]> levelmap = new ArrayList<int[][]>();

    // get the location of items from map
    // and put it into each location arraylist/array
    private void getMapLocation() {
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 12; j++) {
                if (currentmap[i][j] == 1) {
                    treeloc.add(new int[] { i, j });
                } else if (currentmap[i][j] == 2) {
                    // set value of target(2) to empty space(0)
                    currentmap[i][j] = 0;
                    targetloc.add(new int[] { i, j });
                } else if (currentmap[i][j] == 3) {
                    wolfloc = new int[] { i, j };
                } else if (currentmap[i][j] == 4) {
                    JLabel j_sheep = new JLabel(sheep);
                    j_sheeps.add(j_sheep);
                    sheeploc.add(new int[] { i, j });
                    // change value of sheep(4) in map to represent different sheep
                    currentmap[i][j] = 4 + sheeploc.size();
                }
            }
        }
    }

    // initial target in main frame according to target location arraylist
    private void initTarget() {
        for (int i = 0; i < targetloc.size(); i++) {
            int[] temp = targetloc.get(i);
            JLabel j_target = new JLabel(target);
            this.add(j_target);
            j_target.setBounds(temp[0] * 50, temp[1] * 50, 50, 50);
        }
    }

    // initial tree in main frame according to tree location arraylist
    private void initTree() {
        for (int i = 0; i < treeloc.size(); i++) {
            int[] temp = treeloc.get(i);
            JLabel j_tree = new JLabel(tree);
            this.add(j_tree);
            j_tree.setBounds(temp[0] * 50, temp[1] * 50, 50, 50);
        }
    }

    // initial sheep in main frame according to sheep location arraylist
    private void initSheeps() {
        for (int i = 0; i < sheeploc.size(); i++) {
            int[] temp = sheeploc.get(i);
            // JLabel j_sheep = new JLabel(sheep);
            this.add(j_sheeps.get(i));
            j_sheeps.get(i).setBounds(temp[0] * 50, temp[1] * 50, 50, 50);
        }
    }

    // initial wolf in main frame according to wolf location array
    private void initWolf() {
        this.add(j_wolf);
        j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
    }

    // initial background of main frame
    private void initBackgroud() {
        this.add(j_bg);
        j_bg.setBounds(0, 0, 800, 600);
    }

    // initial main frame UI
    private void initFrameUI() {
        this.setVisible(true);
        this.setBounds(400, 200, 810, 635);
        this.setTitle("Wolf and Sheeeps v0.1");
        this.setLayout(null);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    // below define the reaction to four direction key
    // KeyCode: up:38 down:40 left:37 right:39
    @Override
    public void keyReleased(KeyEvent e) {

        // reaction to key "up"
        if (e.getKeyCode() == 38) {
            // if 1 grid up is empty
            if (currentmap[wolfloc[0]][wolfloc[1] - 1] == 0) {
                // change wolf in map
                currentmap[wolfloc[0]][wolfloc[1] - 1] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                // change wolfloc and wolf display in screen
                wolfloc[1] -= 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
            }
            // if 1 grid up is sheep, 2 grid up is empty
            else if (currentmap[wolfloc[0]][wolfloc[1] - 1] >= 5 && currentmap[wolfloc[0]][wolfloc[1] - 2] == 0) {
                // change wolf and sheeps in map
                currentmap[wolfloc[0]][wolfloc[1] - 2] = currentmap[wolfloc[0]][wolfloc[1] - 1];
                currentmap[wolfloc[0]][wolfloc[1] - 1] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                // change wolfloc and wolf display in screen
                wolfloc[1] -= 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
                // get index no. according to map value, different value represent different
                // sheep
                int index = currentmap[wolfloc[0]][wolfloc[1] - 1] - 5;
                // change sheeploc value and sheep display in screen
                sheeploc.get(index)[1] -= 1;
                j_sheeps.get(index).setBounds(sheeploc.get(index)[0] * 50, sheeploc.get(index)[1] * 50, 50, 50);
            }
        }

        // reaction to key "down", structure is same as key "up"
        if (e.getKeyCode() == 40) {
            if (currentmap[wolfloc[0]][wolfloc[1] + 1] == 0) {
                currentmap[wolfloc[0]][wolfloc[1] + 1] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[1] += 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
            } else if (currentmap[wolfloc[0]][wolfloc[1] + 1] >= 5 && currentmap[wolfloc[0]][wolfloc[1] + 2] == 0) {
                currentmap[wolfloc[0]][wolfloc[1] + 2] = currentmap[wolfloc[0]][wolfloc[1] + 1];
                currentmap[wolfloc[0]][wolfloc[1] + 1] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[1] += 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
                int index = currentmap[wolfloc[0]][wolfloc[1] + 1] - 5;
                sheeploc.get(index)[1] += 1;
                j_sheeps.get(index).setBounds(sheeploc.get(index)[0] * 50, sheeploc.get(index)[1] * 50, 50, 50);
            }
        }

        // reaction to key "left", structure is same as key "up"
        if (e.getKeyCode() == 37) {
            if (currentmap[wolfloc[0] - 1][wolfloc[1]] == 0) {
                currentmap[wolfloc[0] - 1][wolfloc[1]] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[0] -= 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
            } else if (currentmap[wolfloc[0] - 1][wolfloc[1]] >= 5 && currentmap[wolfloc[0] - 2][wolfloc[1]] == 0) {
                currentmap[wolfloc[0] - 2][wolfloc[1]] = currentmap[wolfloc[0] - 1][wolfloc[1]];
                currentmap[wolfloc[0] - 1][wolfloc[1]] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[0] -= 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
                int index = currentmap[wolfloc[0] - 1][wolfloc[1]] - 5;
                sheeploc.get(index)[0] -= 1;
                j_sheeps.get(index).setBounds(sheeploc.get(index)[0] * 50, sheeploc.get(index)[1] * 50, 50, 50);
            }
        }

        // reaction to key "right", structure is same as key "up"
        if (e.getKeyCode() == 39) {
            if (currentmap[wolfloc[0] + 1][wolfloc[1]] == 0) {
                currentmap[wolfloc[0] + 1][wolfloc[1]] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[0] += 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
            } else if (currentmap[wolfloc[0] + 1][wolfloc[1]] >= 5 && currentmap[wolfloc[0] + 2][wolfloc[1]] == 0) {
                currentmap[wolfloc[0] + 2][wolfloc[1]] = currentmap[wolfloc[0] + 1][wolfloc[1]];
                currentmap[wolfloc[0] + 1][wolfloc[1]] = currentmap[wolfloc[0]][wolfloc[1]];
                currentmap[wolfloc[0]][wolfloc[1]] = 0;
                wolfloc[0] += 1;
                j_wolf.setBounds(wolfloc[0] * 50, wolfloc[1] * 50, 50, 50);
                int index = currentmap[wolfloc[0] + 1][wolfloc[1]] - 5;
                sheeploc.get(index)[0] += 1;
                j_sheeps.get(index).setBounds(sheeploc.get(index)[0] * 50, sheeploc.get(index)[1] * 50, 50, 50);
            }
        }

        // judge the victory condition
        for (int i = 0, sheepInTarget = 0; i < targetloc.size(); i++) {
            // if any target location is not filled by sheep, break
            if (currentmap[targetloc.get(i)[0]][targetloc.get(i)[1]] < 5)
                break;
            sheepInTarget++;
            // if all target filled by sheep, close main frame and new a victory frame
            if (sheepInTarget == targetloc.size()) {
                new VictoryFrame();
                this.dispose();
            }
        }
    }
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马