Kyodai.java
Upload User: shjixinjx
Upload Date: 2020-12-29
Package Size: 985k
Code Size: 10k
Category:

Games

Development Platform:

Java

  1. package kyodai;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.net.*;
  5. import java.awt.event.*;
  6. import javax.swing.border.*;
  7. import kyodai.map.*;
  8. import kyodai.topbar.*;
  9. /**
  10.  * 连连看主类
  11.  */
  12. public class Kyodai extends JFrame
  13. implements ActionListener {
  14. public static Color DarkColor = new Color(55, 77, 118); //暗色
  15. public static Color LightColor = new Color(111, 146, 212); //亮色
  16. public static ImageIcon[] BlocksIcon = new ImageIcon[39]; //游戏中方块的图标
  17. public static ImageIcon GuideIcon; //连线的图标
  18. public static Border unSelected = BorderFactory.createLineBorder(DarkColor, 1); //未选中时的边框
  19. public static Border Selected = BorderFactory.createLineBorder(Color.white, 1); //选中后的边框
  20. public static Border Hint = BorderFactory.createLineBorder(Color.green, 1); //提示的边框
  21. Dimension faceSize = new Dimension(780, 500);
  22. Image icon;
  23. private int counter = 0;
  24. JPanel toolBar = new JPanel(); //工具栏
  25. JPanel actionPanel = new JPanel(); //用户操作栏
  26. JPanel contentPanel = new JPanel(); //容器
  27. JPanel statusPanel = new JPanel(); //状态栏
  28. Border emptyBorder = BorderFactory.createEmptyBorder(); //未选中时的边框
  29. JButton startButton = new JButton(); //"开始"
  30. JButton refreshButton = new JButton(); //"刷新"
  31. JButton hintButton = new JButton(); //"提示"
  32. JButton bombButton = new JButton(); //"炸弹"
  33. JButton demoButton = new JButton(); //"演示"
  34. JButton setupButton = new JButton(); //设置
  35. JButton helpButton = new JButton(); //帮助
  36. JButton aboutButton = new JButton(); //关于
  37. JButton goTop10 = new JButton("Go top 10");
  38. HelpDialog helpDialog; //帮助对话框
  39. AboutDialog aboutDialog; //关于对话框
  40. public static JTextField statusField = new JTextField(
  41. "欢迎使用宝石连连看");
  42. ImageIcon imgStart, imgHint, imgRefresh, imgBomb, imgDemo;
  43. ImageIcon imgSetup, imgHelp, imgAbout;
  44. JButton[] dots = new JButton[Setting.ROW * Setting.COLUMN];
  45. Setting setting = new Setting();
  46. MapUI ui;
  47. Map map;
  48. ClockAnimate clock = new ClockAnimate(); //时钟
  49. ScoreAnimate score = new ScoreAnimate(); //分数
  50. AnimateDelete animateDelete = new AnimateDelete(dots);
  51. Music music = new Music();
  52. public Kyodai() {
  53. initResource();//初始化系统所需要的资源
  54. map = new Map();
  55. ui = new MapUI(map, dots);
  56. initUI();//初始化用户界面
  57. ui.setScore(score);
  58. ui.setClock(clock);
  59. ui.setTop10Button(goTop10);
  60. animateDelete.setSpeed(setting.Animate);
  61. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. this.pack();
  63. this.setSize(faceSize);
  64. //设置运行时窗口的位置
  65. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  66. this.setLocation( (int) (screenSize.width - faceSize.getWidth()) / 2,
  67.  (int) (screenSize.height - faceSize.getHeight()) / 2);
  68. this.setResizable(false);
  69. this.setTitle("宝石连连看"); //设置标题
  70. this.setIconImage(icon); //设置程序图标
  71. //设置动画光标
  72. URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
  73. URL url = urlLoader.findResource("images/cursor.gif");
  74. Image animateImage = new ImageIcon(url).getImage();
  75. Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(
  76. animateImage, new Point(0, 0), "cursor");
  77. this.setCursor(cursor);
  78. this.addWindowListener(
  79. new WindowAdapter() {
  80. public void windowClosing(WindowEvent we) {
  81. setting.save();
  82. }
  83. }
  84. );
  85. if (setting.Music == 1) {
  86. music.play();
  87. }
  88. }
  89. /**
  90.  * 初始化系统所需要的资源
  91. */
  92. public void initResource() {
  93. URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
  94. URL url;
  95. //程序图标
  96. icon = getImage("images/kyodai16.gif");
  97. for (int i = 0; i < BlocksIcon.length; i++) {
  98. BlocksIcon[i] = new ImageIcon(getImage("images/" + (i + 1) + ".gif"));
  99. }
  100. imgStart = new ImageIcon(getImage("images/start.gif"));
  101. imgRefresh = new ImageIcon(getImage("images/refresh.gif"));
  102. imgHint = new ImageIcon(getImage("images/hint.gif"));
  103. imgBomb = new ImageIcon(getImage("images/bomb.gif"));
  104. imgDemo = new ImageIcon(getImage("images/demo.gif"));
  105. imgSetup = new ImageIcon(getImage("images/setup.gif"));
  106. imgHelp = new ImageIcon(getImage("images/help.gif"));
  107. imgAbout = new ImageIcon(getImage("images/about.gif"));
  108. GuideIcon = new ImageIcon(getImage("images/dots.gif"));
  109. //初始化方块
  110. for (int i = 0; i < dots.length; i++) {
  111. dots[i] = new JButton();
  112. dots[i].setActionCommand("" + i);
  113. dots[i].setBorder(unSelected);
  114. dots[i].setBackground(DarkColor);
  115. }
  116. //读取用户设置
  117. setting.load();
  118. //初始化对话框
  119. helpDialog = new HelpDialog(this); //帮助对话框
  120. aboutDialog = new AboutDialog(this); //关于对话框
  121. }
  122. /**
  123.  * 初始化用户界面
  124. */
  125. public void initUI() {
  126. //界面整体布局
  127. Border border = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
  128. new Color(45, 92, 162),
  129. new Color(43, 66, 97),
  130. new Color(45, 92, 162),
  131. new Color(84, 123, 200));
  132. BorderLayout borderLayout = new BorderLayout();
  133. toolBar.setBackground(DarkColor);
  134. toolBar.setBorder(border);
  135. toolBar.setPreferredSize(new Dimension(780, 48));
  136. toolBar.setMinimumSize(new Dimension(780, 48));
  137. toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
  138. actionPanel.setBackground(LightColor);
  139. actionPanel.setBorder(border);
  140. actionPanel.setPreferredSize(new Dimension(160, 380));
  141. actionPanel.setMinimumSize(new Dimension(160, 380));
  142. contentPanel.setBackground(DarkColor);
  143. contentPanel.setBorder(border);
  144. contentPanel.setPreferredSize(new Dimension(620, 380));
  145. contentPanel.setMinimumSize(new Dimension(620, 380));
  146. statusPanel.setBackground(DarkColor);
  147. statusPanel.setBorder(border);
  148. statusPanel.setPreferredSize(new Dimension(620, 24));
  149. statusPanel.setMinimumSize(new Dimension(620, 24));
  150. statusPanel.setLayout(new BorderLayout());
  151. this.getContentPane().setLayout(borderLayout);
  152. this.getContentPane().add(toolBar, BorderLayout.NORTH);
  153. this.getContentPane().add(actionPanel, BorderLayout.EAST);
  154. this.getContentPane().add(contentPanel, BorderLayout.CENTER);
  155. this.getContentPane().add(statusPanel, BorderLayout.SOUTH);
  156. //加入地图
  157. contentPanel.add(ui);
  158. //加入计分
  159. actionPanel.add(score);
  160. //加入开始按钮
  161. actionPanel.add(startButton);
  162. startButton.setBorder(emptyBorder);
  163. startButton.setIcon(imgStart);
  164. startButton.addActionListener(this);
  165. //加入刷新按钮
  166. actionPanel.add(refreshButton);
  167. refreshButton.setBorder(emptyBorder);
  168. refreshButton.setIcon(imgRefresh);
  169. refreshButton.addActionListener(this);
  170. //加入提示按钮
  171. actionPanel.add(hintButton);
  172. hintButton.setBorder(emptyBorder);
  173. hintButton.setIcon(imgHint);
  174. hintButton.addActionListener(this);
  175. //加入炸弹按钮
  176. actionPanel.add(bombButton);
  177. bombButton.setBorder(emptyBorder);
  178. bombButton.setIcon(imgBomb);
  179. bombButton.addActionListener(this);
  180. //加入自动演示
  181. actionPanel.add(demoButton);
  182. demoButton.setBorder(emptyBorder);
  183. demoButton.setIcon(imgDemo);
  184. demoButton.addActionListener(this);
  185. //加入设置
  186. toolBar.add(setupButton);
  187. setupButton.setBorder(emptyBorder);
  188. setupButton.setIcon(imgSetup);
  189. setupButton.addActionListener(this);
  190. //加入帮助
  191. toolBar.add(helpButton);
  192. helpButton.setBorder(emptyBorder);
  193. helpButton.setIcon(imgHelp);
  194. helpButton.addActionListener(this);
  195. //加入关于
  196. toolBar.add(aboutButton);
  197. aboutButton.setBorder(emptyBorder);
  198. aboutButton.setIcon(imgAbout);
  199. aboutButton.addActionListener(this);
  200. //加入时钟
  201. actionPanel.add(clock);
  202. //加入状态栏
  203. statusPanel.add(statusField, BorderLayout.CENTER);
  204. statusField.setBorder(unSelected);
  205. statusField.setEditable(false);
  206. statusField.setForeground(Color.white);
  207. statusField.setBackground(DarkColor);
  208. //加入发送按钮
  209. statusPanel.add(goTop10, BorderLayout.EAST);
  210. goTop10.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  211. goTop10.setForeground(Color.white);
  212. goTop10.setBackground(DarkColor);
  213. goTop10.setFont(new Font("Serif", Font.PLAIN, 11));
  214. goTop10.addActionListener(this);
  215. goTop10.setEnabled(false);
  216. }
  217. public static void showHint(String str) {
  218. statusField.setText(str);
  219. }
  220. /**
  221.  * 通过给定的文件名获得图像
  222. */
  223. Image getImage(String filename) {
  224. URLClassLoader urlLoader = (URLClassLoader)this.getClass().
  225. getClassLoader();
  226. URL url = null;
  227. Image image = null;
  228. url = urlLoader.findResource(filename);
  229. image = Toolkit.getDefaultToolkit().getImage(url);
  230. MediaTracker mediatracker = new MediaTracker(this);
  231. try {
  232. mediatracker.addImage(image, 0);
  233. mediatracker.waitForID(0);
  234. }
  235. catch (InterruptedException _ex) {
  236. image = null;
  237. }
  238. if (mediatracker.isErrorID(0)) {
  239. image = null;
  240. }
  241. return image;
  242. }
  243. /**
  244.  * 事件处理
  245. */
  246. public void actionPerformed(ActionEvent e) {
  247. Object obj = e.getSource();
  248. if (obj == startButton) { //开始
  249. map = new Map(Setting.Level[setting.LevelIndex]);
  250. ui.setMap(map);
  251. ui.start();
  252. clock.start();
  253. score.setScore( -1, 0);
  254. }
  255. else if (obj == refreshButton) { //刷新
  256. ui.refresh();
  257. }
  258. else if (obj == hintButton) { //提示
  259. ui.findNext(new Point( -1, -1));
  260. }
  261. else if (obj == bombButton) { //炸弹
  262. ui.bomb(new Point( -1, -1), true);
  263. }
  264. else if (obj == demoButton) { //自动演示
  265. ui.autoPlay();
  266. }
  267. else if (obj == aboutButton) { //关于
  268.   aboutDialog.show();
  269. }
  270. else if (obj == helpButton) { //帮助
  271. //new Help();
  272. helpDialog.show();
  273. }
  274. else if (obj == setupButton) { //设置
  275. SetupDialog setupDialog = new SetupDialog(this); //设置对话框
  276. setupDialog.show();
  277. if (setting.Music == 1) {
  278. music.play();
  279. }
  280. else {
  281. music.stop();
  282. }
  283. animateDelete.setSpeed(setting.Animate);
  284. }
  285. else if (obj == goTop10) { //排名
  286. String name = JOptionPane.showInputDialog(this, "请留下大名:", "过眼云烟");
  287. if (!"".equals(name.trim())) { //如果留了名字
  288. System.out.println("ui.encode()="+ui.encode());
  289. new Top10(this, "nickname=" + name + "&" + ui.encode());
  290. goTop10.setEnabled(false);
  291. }
  292. }
  293. }
  294. public static void main(String args[]) {
  295. JFrame.setDefaultLookAndFeelDecorated(true);
  296. JDialog.setDefaultLookAndFeelDecorated(true);
  297. Kyodai kyodai = new Kyodai();
  298. kyodai.show();
  299. }
  300. }