📄 lianliankan.java
字号:
{
isPlaying = false;
}
int offset;
offset = a.x*Setting.COLUMN + a.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
offset = b.x*Setting.COLUMN + b.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton) e.getSource();
int offset = Integer.parseInt(button.getActionCommand());
int row, col;
row = Math.round(offset / Setting.COLUMN);
col = offset - row * Setting.COLUMN;
if (map.getMap()[row][col] < 1) { //如果上面没有图片
return;
}
selSound.start();
if (isSelected)
{
if (lastPoint.x == row && lastPoint.y == col)
{ //是上次选择的点
button.setBorder(LianLianKan.unSelected);
isSelected = false;
}
else
{
//判断是否可以消除
Point current = new Point(row, col);
if (map.test(lastPoint, current))
{
//消除前先取消当前选择点的边框,因为有可能是提示
dots[row * Setting.COLUMN + col].setBorder(LianLianKan.unSelected);
map.erase(current, lastPoint);
erase(current, lastPoint);
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(LianLianKan.unSelected);
lastPoint = new Point(0, 0);
isSelected = false;
erasSound.start();
}
//如果无法消除这两点,把此点设为当前点
else
{
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(LianLianKan.unSelected);
button.setBorder(LianLianKan.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
}
}
}
else
{
button.setBorder(LianLianKan.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
}
}
public void start()
{
isPlaying = true;
paint();
}
public void stop()
{}
public void run(){}
}
public class LianLianKan extends JFrame implements ActionListener
{
public static ImageIcon [] BlocksIcon = new ImageIcon[39];
public static ImageIcon protectImage = new ImageIcon("images/gameStarter.jpg");
public static JButton protection = new JButton(protectImage);
public static Border unSelected = BorderFactory.createLineBorder(Color.black,1);
public static Border Selected = BorderFactory.createLineBorder(new Color(110,145,212),1);
Dimension faceSize = new Dimension(780, 600);
Music bgSound;
JPanel toolBar = new JPanel();//工具栏
JPanel actionPanel = new JPanel();//用户操作栏
JPanel contentPanel = new JPanel();//容器,用来存放显示的游戏图块
JPanel timePanel = new JPanel();
Border emptyBorder = BorderFactory.createEmptyBorder();
JProgressBar timeBar;
//////////////////////////////////////////////////////////
public static int VALUE=10;//总时间3m
public static int value=VALUE;//初始时间
public static Timer timer;
/*
*定义的Menu相关的变量
*/
JMenuBar gameMenu;
JMenu MainMenu;
JMenu SetupMenu;
JMenu HelpMenu;
JMenuItem miStart;
JMenuItem miRefresh;
//JMenuItem miHint;
JMenuItem miExit;
JMenuItem miTest;
JMenu LevelsubMenu;
JMenuItem miPrimary;
JMenuItem miSecondary;
JMenuItem miHighLevel;
JMenuItem miAbout;
JButton [] dots = new JButton[Setting.ROW * Setting.COLUMN];
MapUI ui;
Map map;
public LianLianKan()
{
initResource();
map = new Map();
ui = new MapUI(map,dots);
initUI();
/*
*Frame框的设置
*/
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setBackground(new Color(0,0,0));
this.setSize(faceSize);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( (int) (screenSize.width - faceSize.getWidth()) / 2,
(int) (screenSize.height - faceSize.getHeight()) / 2);
this.setResizable(false);
this.setTitle("LianLianKan v1.01");//左上角显示的标语
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){}
});
}
//初始化系统所需资源
public void initResource()
{
for(int i = 0 ;i<BlocksIcon.length; i ++)
{
BlocksIcon[i] = new ImageIcon(("images/" + (i+1) + ".gif"));
}
//初始化游戏中的每一个元素块
for(int i =0;i<dots.length;i++)
{
dots[i] = new JButton();
dots[i].setActionCommand(""+i);
dots[i].setBorder(unSelected);
/////////////////////////////////////////////////////////////
dots[i].setBackground(new Color(0,0,0));
}
/**
* 初始化用户界面
*/
}
public void initUI()
{
bgSound =new Music("bg1.mid");
//bgSound.loop();
////////////////////////////////////////////////////////////////
Border border = BorderFactory.createLineBorder(Color.blue);
BorderLayout borderLayout = new BorderLayout();
toolBar.setBackground(new Color(52,76,120));
toolBar.setBorder(border);
toolBar.setPreferredSize(new Dimension(780, 48));
toolBar.setMinimumSize(new Dimension(780, 48));
toolBar.setLayout(new BorderLayout());
contentPanel.setBackground(new Color(0,0,0));
contentPanel.setBorder(border);
contentPanel.setPreferredSize(new Dimension(620,380));
contentPanel.setMinimumSize(new Dimension(620,380));
this.getContentPane().setLayout(borderLayout);
this.getContentPane().add(toolBar,BorderLayout.NORTH);
this.getContentPane().add(contentPanel,BorderLayout.CENTER);
//加入地图
contentPanel.add(protection,new FlowLayout());
contentPanel.add(ui);
//命令条的设置。。
gameMenu = new JMenuBar();
MainMenu = new JMenu("游戏");
HelpMenu = new JMenu("帮助");
SetupMenu = new JMenu("设置");
LevelsubMenu = new JMenu("级别");
gameMenu.add(MainMenu);
gameMenu.add(SetupMenu);
gameMenu.add(HelpMenu);
miStart = new JMenuItem("开始");
miStart.addActionListener(this);
miRefresh = new JMenuItem("刷新");
miRefresh.addActionListener(this);
//miHint = new JMenuItem("提示");
//miHint.addActionListener(this);
miExit = new JMenuItem("退出");
miExit.addActionListener(this);
MainMenu.add(miStart);
MainMenu.add(miRefresh);
//MainMenu.add(miHint);
MainMenu.add(miExit);
miTest = new JMenuItem("测试");
miTest.addActionListener(this);
miPrimary = new JMenuItem("初级");
miPrimary.addActionListener(this);
miSecondary = new JMenuItem("中级");
miSecondary.addActionListener(this);
miHighLevel = new JMenuItem("高级");
miHighLevel.addActionListener(this);
LevelsubMenu.add(miPrimary);
LevelsubMenu.add(miSecondary);
LevelsubMenu.add(miHighLevel);
SetupMenu.add(miTest);
SetupMenu.add(LevelsubMenu);
miAbout = new JMenuItem("关于");
miAbout.addActionListener(this);
HelpMenu.add(miAbout);
this.setJMenuBar(gameMenu);
//时间条
timeBar =new JProgressBar(0,VALUE);
timeBar.setStringPainted(true);
timeBar.setBorderPainted(true);
timeBar.setForeground(Color.gray);
timeBar.setBounds(10,20,5,20);
timeBar.setString("time");
timeBar.setValue(value);
timePanel.setBackground(new Color(0,0,0));
timePanel.add(timeBar,new FlowLayout());
toolBar.add(gameMenu,BorderLayout.WEST);
toolBar.add(timePanel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (obj == miStart)
{
protection.setIcon(null);
protection.setBackground(Color.black);
protection.setBorder(BorderFactory.createLineBorder(Color.black,1));
timer=new Timer(1000,new TimeListen());//the timer start the bar
timer.start();
map = new Map();
ui.setMap(map);
ui.start();
}
else if (obj == miRefresh)
{
ui.refresh();
}
else if(obj == miPrimary)
{
new Setting().setPrimaryLevel();
}
else if(obj == miSecondary)
{
new Setting().setSecondaryLevel();
}
else if(obj == miHighLevel)
{
new Setting().setHighLevel();
}
else if (obj == miAbout)
{
JOptionPane.showMessageDialog(this,
"LianLianKan V1.01\n"+
"Author:Eric Ban && Andy Wei",
"Information about..",
JOptionPane.INFORMATION_MESSAGE);
}
else if(obj == miExit)
{
System.exit(0);
}
else if(obj == miTest)
{
new Setting().setTestLevel();
}
}
class TimeListen implements ActionListener
{
public TimeListen()
{value = VALUE;}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == timer)
{
if(ui.isGameOver())
{
timer.stop();
JOptionPane.showMessageDialog(new LianLianKan(),
"Congraduation!!!\nYou Pass this level!",
"Pass Over",
JOptionPane.INFORMATION_MESSAGE);
}
if(value == 0)
{
timer.stop();
timeBar.setValue(0);
int n =JOptionPane.showConfirmDialog(new LianLianKan(),
"Sorry:\n"+
"You have not complete the game.\n"+
"GAME OVER\n"+
"Would you want a new game?",
"Time Is Over",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
value = VALUE;
ui.clearMap();
protection.setIcon(protectImage);
contentPanel.add(protection,new FlowLayout());
}
else if(n == JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
else
{
timeBar.setValue(value--);
}
if(value <= VALUE/3)
{
timeBar.setForeground(Color.red);
}
else
timeBar.setForeground(Color.gray);
}
}
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
LianLianKan lianlian = new LianLianKan();
lianlian.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -