package diandeng;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author 斌斌
*
*/
public class Lighting extends JFrame
{
private Container c;
private int level;
private final int TOTALLEVEL = 5;//游戏总关卡数
private final int WIDTH, HEIGHT;//当前屏幕的大小(宽度、高度)
private JButton buttons[][];
private boolean buttonValue[][];
static Icon light, unlight;//灯亮和灯灭时的图标
private JMenuItem exit;
private JPanel centerPanel = new JPanel(), southPanel = new JPanel();
private JButton gameState, newGameButton, exitButton;
private boolean isWin;
private ButtonHandler lis;
public Lighting()
{
super("点灯");
addBgMusic();
c = getContentPane();
c.setVisible(false);
level = 1;
centerPanel.setLayout(new GridLayout(level + 2, level + 2));
southPanel.setLayout(new GridLayout(1, 2));
JMenuBar menu = new JMenuBar();//菜单栏
JMenu game = new JMenu("游戏(G)");
game.setMnemonic('G');
JMenuItem start = new JMenuItem("开始(S)");
start.setMnemonic('S');
start.addActionListener(new startListener());
exit = new JMenuItem("关闭(W)");
exit.setMnemonic('W');
exit.setEnabled(false);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exit.setEnabled(false);
c.setVisible(false);
}
});
game.add(start);
game.add(exit);
JMenu help = new JMenu("帮助(H)");
help.setMnemonic('H');
JMenuItem instruction = new JMenuItem("游戏说明(I)");
instruction.setMnemonic('I');
instruction.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "一、点击\"游戏\" \"开始\"启动游戏主界面;"
+ "\n二、主界面中的方格相当于一盏盏的灯,但现在都是黑色的——即灯全灭了。"
+ "如果一盏灯是白色的,那么就表示这盏灯是亮的。" + "\n三、每当改变一盏灯的状态(点亮或熄灭)时,"
+ "它附近(上下左右及本身)的灯的状态也随之改变。" + "\n\n游戏任务:点亮所有的灯。");
}
});
JMenuItem aboutUs = new JMenuItem("关于我们(U)");
aboutUs.setMnemonic('U');
aboutUs.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "谢谢您的使用!\n"
+ "\n如果您需要了解更多相关信息,请E-mail我——"
+ "\n胡海斌:335855364@qq.com\n杨腾远:271275817@qq.com");
}
});
JMenuItem aboutGame = new JMenuItem("关于点灯(G)");
aboutGame.setMnemonic('G');
aboutGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null," 点灯\n\n"
+ "版本:1.3\n公告:本计算机程序是个人作品,在保留\n作者的前提下,您可以自由传播本程序。");
}
});
help.add(instruction);
help.addSeparator();
help.add(aboutUs);
help.add(aboutGame);
menu.add(game);
menu.add(help);
setJMenuBar(menu);
gameState = new JButton("第 " + level + " 局,共 " + TOTALLEVEL + " 局");
southPanel.add(gameState);
newGameButton = new JButton("重开(N)");
newGameButton.setMnemonic('N');
southPanel.add(newGameButton);
newGameButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
for (int i = 0; i < buttonValue.length; i++)
{
for (int j = 0; j < buttonValue[i].length; j++)
{
buttonValue[i][j] = false;
buttons[i][j].setIcon(unlight);
}
}
}
});
exitButton = new JButton("关闭(W)");
exitButton.setMnemonic('W');
southPanel.add(exitButton);
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
exit.setEnabled(false);
c.setVisible(false);
}
});
c.add(southPanel, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
WIDTH = tk.getScreenSize().width;
HEIGHT = tk.getScreenSize().height;
Image img = tk.getImage(this.getClass().getClassLoader().getResource(
"images/y.gif"));
this.setIconImage(img);
light = new ImageIcon(this.getClass().getClassLoader().getResource(
"images/light.gif"));
unlight = new ImageIcon(this.getClass().getClassLoader().getResource(
"images/unlight.gif"));
buttons = new JButton[((GridLayout) (centerPanel.getLayout()))
.getRows()][((GridLayout) (centerPanel.getLayout()))
.getColumns()];
buttonValue = new boolean[buttons.length][buttons[0].length];
lis = new ButtonHandler();
for (int i = 0; i < buttons.length; i++)
{
for (int j = 0; j < buttons[i].length; j++)
{
buttons[i][j] = new JButton(unlight);
buttonValue[i][j] = false;
buttons[i][j].addActionListener(lis);
centerPanel.add(buttons[i][j]);
}
}
c.add(centerPanel);
this.setResizable(false);
this.setBounds(WIDTH / 2 - 300, HEIGHT / 2 - 300, 600, 600);// 窗体居中显示,大小为600*600
this.setVisible(true);
}
/**
* 添加背景音乐
*/
private void addBgMusic()
{
URL soundURL = this.getClass().getClassLoader().getResource(
"sounds/bgsound.wav");
Line.Info linfo = new Line.Info(Clip.class);
try
{
Line line = AudioSystem.getLine(linfo);
Clip buhClip = (Clip) line;
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
buhClip.open(ais);
buhClip.start();
} catch (LineUnavailableException e1)
{
e1.printStackTrace();
} catch (UnsupportedAudioFileException e1)
{
e1.printStackTrace();
} catch (IOException e1)
{
e1.printStackTrace();
}
}
private class startListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
exit.setEnabled(true);
c.setVisible(true);
centerPanel.removeAll();
level = 1;
// 初始化南部
southPanel.removeAll();
gameState = new JButton("第 " + level + " 局,共 " + TOTALLEVEL + " 局");
southPanel.add(gameState);
southPanel.add(newGameButton);
southPanel.add(exitButton);
c.add(southPanel, BorderLayout.SOUTH);
// 初始化中部
centerPanel.setLayout(new GridLayout((level - 1) % TOTALLEVEL + 3,
(level - 1) % TOTALLEVEL + 3));
buttons = new JButton[((GridLayout) (centerPanel.getLayout()))
.getRows()][((GridLayout) (centerPanel.getLayout()))
.getColumns()];
buttonValue = new boolean[buttons.length][buttons[0].length];
for (int i = 0; i < buttons.length; i++)
{
for (int j = 0; j < buttons[i].length; j++)
{
buttons[i][j] = new JButton(unlight);
buttonValue[i][j] = false;
buttons[i][j].addActionListener(lis);
centerPanel.add(buttons[i][j]);
}
}
c.add(centerPanel);
c.validate();//验证此容器及其所有子组件。
}
}
/**
* @author binbin
*
* 按键监听器
*
*/
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
/**
* 核心算法
*/
for (int i = 0; i < buttons.length; i++)
{
for (int j = 0; j < buttons[i].length; j++)
{
if (e.getSource()
评论16
最新资源