📄 login.java
字号:
package login;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.URLClassLoader;
import javax.imageio.ImageIO;
import javax.swing.*;
import model.*;
import control.Control;
import view.CentralPanel;
import java.awt.event.WindowListener;
import java.awt.Dimension;
import javax.swing.JFrame;
//设置皮肤
import com.incors.plaf.alloy.AlloyLookAndFeel;
/**
* @author 何晓飞 李强 何明
* @version 2.0 本类绘制出了本游戏的初始界面,实现了界面的跳转。 由于java提供的布局管理器不能很好的适用于本游戏的制作和控件的合理布局,
* 所以本游戏的面板全部手绘而成。并且经过相当长有段时间的精细定位。 也是为了提高面板的美观,本类中提供了一个私有的 JPanel类来
* 完成整体的布局。同时为了游戏的娱乐性,面板中加入了可供选择 连连看图片和音乐模式的按钮。 面板由标题,按钮,版权信息和蜗牛工
*
* 作组的动态介绍。 程序的特点是采用了事件监听模式来控制按钮事件,同时为了使界面而引入了JDialog
* 使用了radioButton来安排选项,同时引入了JTabbedPane来安排内容。 另外,线程的加入也是本程序的一大特色。
* 尽管程序还存在很多缺陷和bug,但是从学习的角度来说,蜗牛工作组的每个成员 都因此程序而获益匪浅。
*/
public class Login extends JFrame {
/* 序列号 */
private static final long serialVersionUID = 1L;
/* 版本 */
public static String version = "2.0";
/* 登陆音乐 */
private String LoginMusic = "登录音乐.mid";
/* 登陆声音 */
private Sound LoginSound;
/* control 对象 */
private Control control;
private Model model;
/**
* main函数设置了游戏界面的皮肤,同时创建了一个JFrame 将内容植入,设置了界面 的初始位置和大小以及关闭窗口的方式。
*/
public static void main(String[] args) {
// 引入了一个异常处理机制来设置游戏整体界面的皮肤
try {
// 引入外部皮肤并设置为游戏的皮肤
AlloyLookAndFeel.setProperty("alloy.isSandbox", "true");
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
UIManager.setLookAndFeel("com.incors.plaf.alloy.AlloyLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
/* 创建一个model对象 */
Model model = new Model();
JFrame LoginWindow = new Login(model); // 登陆界面
// 创建一个dimension对象获得屏幕大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
/* 如果是第一次没有加载信息为空,那么就把游戏界面放在屏幕的中间; 否则就加载游戏界面的位置信息 */
if (model.getLocation() == null)
LoginWindow.setLocation(screenSize.width / 2
- LoginWindow.getWidth() / 2, screenSize.height / 2
- LoginWindow.getHeight() / 2 - 100);
else
LoginWindow.setLocation(model.getLocation());
/* 锁定窗口 */
LoginWindow.setResizable(false);
/* 关闭窗口方式 */
LoginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*自动调整各组件*/
LoginWindow.pack();
/* 设置可见 */
LoginWindow.setVisible(true);
}
/**
* Login 的构造函数
*
* @param content
* initPanel实例
* @param loginSound
* sound实例
*/
public Login(Model model) {
/* 标题 */
super("连 连 看");
/* 大小 */
setSize(820, 553);
/* 窗口关闭方式 */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建一个initPane 对象 */
initPanel content = new initPanel();
/* 在contentPane中添加initPane对象 */
setContentPane(content);
/* 创建sound对象 */
LoginSound = new Sound(LoginMusic);
/* 播放登陆音乐 */
LoginSound.play(2);
this.model=model;
}
/**
* 本类定义了一个使用空布局的 panel并添加了四个组件,组件之间明确设定了界限。 预计 panel的大小固定为500*400像素 .
*/
private class initPanel extends JPanel {
/* 序列号 */
private static final long serialVersionUID = 1L;
Pane pane; // pane 实例
JLabel initBackground;//背景
JButton load; // 装载按钮
JButton newGame;// 新游戏按钮
JButton quit;// 退出按钮
/**
* initPanel的构造方法 initPanel的构造方法将所有的控件合理的安排到面板上并设置背景色,边框和大小
*/
public initPanel() {
// 布局管理器设置为空,因此布局全部手工完成
setLayout(null);
setBackground(new Color(0, 0, 0));// 背景色
setBorder(BorderFactory.createEtchedBorder());// 设置边框
setPreferredSize(new Dimension(820, 553));// 设置大小
/*
* 创建各组件并加到contePane上去,如果不加到容器上去的话, 即使你设置了他们的边框他们也不会出现
*/
pane = new Pane(version);// 创建pane
add(pane);// 加入initPanel
// 创建分别设置四个按钮的监听器,提示语并添加至面板
// 这里不能使用toolkit的方法,因为打成jar包的时候按钮上的图标会因此丢失
// 这是eclipse本身的问题
newGame = new JButton("<html><font color=blue>新游戏</font></html>",
createImageIcon("/images/start.jpg"));// 创建新游戏按钮
newGame.addActionListener(new ListenerNewGame());// 添加监听
newGame.setToolTipText("开始新游戏");// 设置提示语
newGame.setOpaque(true);//设置透明
add(newGame);// 添加至面板
load = new JButton("<html><font color=blue>加 载</font></html>",
createImageIcon("/images/load.jpg"));// 创建装载按钮
load.addActionListener(new ListenerLoad());// 添加监听
load.setToolTipText("装载上次保存过的游戏");// 设置提示语
load.setOpaque(true);
add(load);// 添加至面板
quit = new JButton("<html><font color=blue>退 出</font></html>",
createImageIcon("/images/quit.jpg"));// 创建退出按钮
quit.addActionListener(new ListenerQuit());// 添加监听
quit.setToolTipText("退出游戏");// 设置提示语
quit.setOpaque(true);
add(quit);// 添加至面板
initBackground=new JLabel(createImageIcon("/images/initBackground.jpg"));
add(initBackground);
// 设置控件的具体位置
initBackground.setBounds(0,0,820,553);
pane.setBounds(30, 100, 500, 400);
newGame.setBounds(600, 170, 120, 40);
load.setBounds(600, 270, 120, 40);
quit.setBounds(600, 370, 120, 40);
}
// createImageIcon函数通过地址创建图标
protected ImageIcon createImageIcon(String path) {
// 获取URL
java.net.URL imgURL = initPanel.class.getResource(path);
if (imgURL != null) {
// 返回图标
return new ImageIcon(imgURL);
} else {
// 出错信息
System.err.println("fail to load file:" + path);
return null;
}
}
// newName按钮的监听器类
class ListenerNewGame implements ActionListener {
public void actionPerformed(ActionEvent event) {
// 开始游戏
newGame();
}
}
// load按钮的监听类
class ListenerLoad implements ActionListener {
public void actionPerformed(ActionEvent event) {
// 装载游戏
loadGame();
}
}
// quit按钮的监听类
class ListenerQuit implements ActionListener {
public void actionPerformed(ActionEvent event) {
// 探出JOptionPane以供用户确认
int response = JOptionPane.showConfirmDialog(null, "真的要退出吗?",
" 友情提示", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {
// 停止登陆声音
LoginSound.stop();
System.exit(0);//退出
}
}
}
// 开始游戏函数,实现了界面的跳转
public void newGame() {
// 弹出JoptionPane供用户确认
int response = JOptionPane.showConfirmDialog(null, "立即开始游戏?",
" 友情提示", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {
// 确认后停止登陆音乐移初initPanel组件
removeAll();
LoginSound.stop(); // 停止登录音乐
// 添加centralPanel组件并为其添加监视
CentralPanel cen = new CentralPanel(model);
cen.getLblLevel().setText("第1关");
// 添加监视
model.addObserver(cen);
control = new Control(model, cen);
cen.addController(control);
// 将centralPanel添加至面板
this.add(cen);
// 设置可见
setVisible(true);
}
}
// 装载游戏函数
public void loadGame() {
int i = LoadAndSave.loadGameData(model);// 获得保存的关数
if (i != 0) {
// 弹出JoptionPane供用户确认
int response = JOptionPane
.showConfirmDialog(null, "上次保存的是第" + i + "关,确定要继续吗?",
" 友情提示", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {
// 确认后移除所有组件停止音乐重新装载游戏
removeAll();
LoginSound.stop(); // 停止登录音乐
CentralPanel cen = new CentralPanel(model);
cen.getLblLevel().setText(
" 第 " + i + " 关");
// 添加监视
model.addObserver(cen);
control = new Control(model, cen);
cen.addController(control);
// 设置可见
cen.setVisible(true);
this.add(cen);
setVisible(true);
}
} else {
JOptionPane.showMessageDialog(null, "无存档游戏!");
}
}
} // 结束嵌套类
/**
* @return control
*/
public Control co() {
return control;
}
/**
* 此面板显示一个160*160的checkrBoard 申请一个线程并动态的显示蜗牛工作组的情况和本程序的 基本概况
*/
private static class Pane extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
// 背景
private Image back;
// 线程
private Thread runner;
private int y;
// string类型的数组
private String[] msg;
// pane 的构造方法
public Pane(String version) {
// 获取图片
try {
URLClassLoader urlLoader = (URLClassLoader) Pane.class
.getClassLoader();
URL url = urlLoader.findResource("images/back.jpg");//获取背景
back = ImageIO.read(url);
} catch (Exception e) {
e.printStackTrace();
}
// 内容
msg = new String[] { " 水果连连看 " + version, "", "",
"编写语言:Java", "", "设计单位:蜗牛工作组", "", "设计人员:何晓飞,李强,何明", "",
"联系方式:kevin1987725@163.com", "",
"完成日期:2007 年 5 月", "", "本软件仅供玩家娱乐使用", "",
"使用者切忌","请勿用于任何商业行为!", "", "谢谢合作!" };
this.y = msg.length * 20 - 80;
// 判断线程是否运行
if (this.runner == null) {
this.runner = new Thread(this);
this.runner.start();
}
}
// paintCoponent函数将组件画出
public void paintComponent(Graphics g) {
Graphics2D comp = (Graphics2D) g;
// 设置颜色
comp.setColor(Color.gray);
// 画矩形
comp.fillRect(0, 0, this.getWidth(), this.getHeight());
// 画图片
comp.drawImage(back, 0, 0, this.getWidth(), this.getHeight(), this);
// 设置字体
comp.setFont(new Font("楷体", 0, 18));
// 重绘
comp.setColor(new Color(25, 25, 25));
for (int i = 0; i < msg.length; i++) {
comp.drawString(msg[i], 40, y + i * 20);
}
}
// 线程必写函数run,碰到interruption则线程结束
public void run() {
try {
while (true) {
Thread.sleep(100);
if (--y < -msg.length * 20)
y = this.getHeight() + 20;
repaint();
}
} catch (InterruptedException ignore) {
}
}
}
// 添加控制函数
public void addController(Object obj) {
if (obj instanceof WindowListener) {
addWindowListener((WindowListener) obj);
}
}
} // initPane类结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -