[Java] 纯文本查看 复制代码
package cn.it.lsz.ui;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class MainFrame extends Frame implements KeyListener {
public MainFrame() {
treeInit();
targetInit();
sheepInit();
wolfInit();
backgroundInit();
setMainFrameUI();
this.addKeyListener(this);
}
int wx;
int wy;
JLabel [][]sheeps = new JLabel[12][16];
int [][]datas = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
private void treeInit() {
Icon ic = new ImageIcon("tree.png");
for(int i=0;i<datas.length;i++) {
for(int j=0;j<datas[i].length;j++) {
if(datas[i][j] == 1) {
JLabel lab_tree = new JLabel(ic);
lab_tree.setBounds(12+50*j, 38+50*i, 50, 50);
this.add(lab_tree);
}
}
}
}
private void targetInit() {
Icon i = new ImageIcon("target.png");
JLabel lab_target1 = new JLabel(i);
lab_target1.setBounds(712, 238, 50, 50);
this.add(lab_target1);
JLabel lab_target2 = new JLabel(i);
lab_target2.setBounds(712, 288, 50, 50);
this.add(lab_target2);
JLabel lab_target3 = new JLabel(i);
lab_target3.setBounds(712, 338, 50, 50);
this.add(lab_target3);
}
private void sheepInit() {
Icon i = new ImageIcon("sheep-no.png");
JLabel lab_sheep1 = new JLabel(i);
lab_sheep1.setBounds(12+50*6, 38+50*4, 50, 50);
this.add(lab_sheep1);
datas[4][6] = 4;
sheeps[4][6] = lab_sheep1;
JLabel lab_sheep2 = new JLabel(i);
lab_sheep2.setBounds(12+50*6, 38+50*6, 50, 50);
this.add(lab_sheep2);
datas[6][6] = 4;
sheeps[6][6] = lab_sheep2;
JLabel lab_sheep3 = new JLabel(i);
lab_sheep3.setBounds(12+50*6, 38+50*8, 50, 50);
this.add(lab_sheep3);
datas[8][6] = 4;
sheeps[8][6] = lab_sheep3;
}
private void wolfInit() {
wx = 4;
wy = 6;
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf = new JLabel(i);
lab_wolf.setBounds(12+50*wx, 38+wy*50, 50, 50);
this.add(lab_wolf);
}
JLabel lab_wolf;
private void backgroundInit() {
Icon i = new ImageIcon("bg.png");
JLabel lab_bg = new JLabel(i);
lab_bg.setBounds(12, 38, 800, 600);
this.add(lab_bg);
}
private void setMainFrameUI() {
this.setLayout(null);
this.setTitle("推箱子小游戏");
this.setLocation(110, 100);
this.setSize(826, 650);
this.setVisible(true);
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == 37) {
if(datas[wy][wx-1] == 1) {
return ;
}
if(datas[wy][wx-1] == 4) {
if(datas[wy][wx-2]!=1 && datas[wy][wx-2]!=4) {
datas[wy][wx-1] = 0;
datas[wy][wx-2] = 4;
sheeps[wy][wx-1].setLocation(12+50*wx-100, 38+50*wy);
sheeps[wy][wx-2] = sheeps[wy][wx-1];
sheeps[wy][wx-1] = null;
wx = wx-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i = new ImageIcon("wolf-zb.png");
lab_wolf.setIcon(i);
}
}
if(datas[wy][wx-1] == 0) {
wx = wx-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i = new ImageIcon("wolf-zb.png");
lab_wolf.setIcon(i);
}
}
if(key == 38) {
if(datas[wy-1][wx] == 1) {
return ;
}
if(datas[wy-1][wx] == 4) {
if(datas[wy-2][wx]!=1 &&datas[wy-2][wx]!=4) {
datas[wy-1][wx] = 0;
datas[wy-2][wx] = 4;
sheeps[wy-1][wx].setLocation(12+50*wx, 38+50*wy-100);
sheeps[wy-2][wx] = sheeps[wy-1][wx];
sheeps[wy-1][wx] = null;
wy = wy-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i = new ImageIcon("wolf-sm.png");
lab_wolf.setIcon(i);
}
}
if(datas[wy-1][wx] == 0) {
wy = wy-1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i = new ImageIcon("wolf-sm.png");
lab_wolf.setIcon(i);
}
}
if(key == 39) {
if(datas[wy][wx+1] == 1) {
return ;
}
if(datas[wy][wx+1] == 4) {
if(datas[wy][wx+2]!=1&&datas[wy][wx+2]!=4) {
datas[wy][wx+1] = 0;
datas[wy][wx+2] = 4;
sheeps[wy][wx+1].setLocation(12+50*wx+100, 38+50*wy);
sheeps[wy][wx+2] = sheeps[wy][wx+1];
sheeps[wy][wx+1] = null;
wx = wx+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i = new ImageIcon("wolf-ym.png");
lab_wolf.setIcon(i);
}
}
if(datas[wy][wx+1] == 0) {
wx = wx+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i = new ImageIcon("wolf-ym.png");
lab_wolf.setIcon(i);
}
}
if(key == 40) {
if(datas[wy+1][wx] == 1) {
return ;
}
if(datas[wy+1][wx] == 4) {
if(datas[wy+2][wx]!=1 && datas[wy+2][wx]!=4) {
datas[wy+1][wx] = 0;
datas[wy+2][wx] = 4;
sheeps[wy+1][wx].setLocation(12+50*wx, 38+50*wy+100);
sheeps[wy+2][wx] = sheeps[wy+1][wx];
sheeps[wy+1][wx] = null;
wy = wy+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf.setIcon(i);
}
}
if(datas[wy+1][wx] == 0) {
wy = wy+1;
int x = (int)lab_wolf.getLocation().getX();
int y = (int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i = new ImageIcon("wolf-zm.png");
lab_wolf.setIcon(i);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}