MainFrame.java
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:18k
源码类别:

Java编程

开发平台:

Java

  1. package russiagame;
  2. import javax.swing.*;
  3. import com.borland.jbcl.layout.*;
  4. import java.awt.*;
  5. import java.awt.Toolkit;
  6. import javax.swing.border.*;
  7. import java.awt.event.*;
  8. import javax.swing.event.*;
  9. /**
  10.  * <p>Title: </p>
  11.  * <p>Description: </p>
  12.  * <p>Copyright: Copyright (c) 2003</p>
  13.  * <p>Company: </p>
  14.  * @author not attributable
  15.  * @version 1.0
  16.  */
  17. public class MainFrame
  18.     extends JFrame {
  19.     private Timer timer; //方块下落时间间隔控制类
  20.     private int nTime = 1000; //方块下落速度
  21.     private Root root; //方块
  22.     private GameTable gGameAreaTable; //方块下落区
  23.     private PreviewTable gPreviewTable; //方块预览区
  24.     private int intGraph; //标志创建哪一个要下落的方块
  25.     private int intNextGraph; //标志下一个要创建的方块
  26.     private int intTotalScore = 0; //用来记录总分数
  27.     private boolean bCanMove = false; //标志是否响应键盘
  28.     private int intSpeed = 1; //速度等级
  29.     private Panel jMainPanel = new Panel(); //添加键盘监听的Panel
  30.     private GameAreaPanel jGamePanel; //方块下落区域
  31.     private PreviewPanel jNextGraphPanel; //方块预览区域
  32.     private XYLayout xyLayout = new XYLayout(); //界面布局
  33.     private JButton jStartButton = new JButton();
  34.     private JButton jRestartButton = new JButton();
  35.     private JButton jExitButton = new JButton();
  36.     private JLabel jSpeedLabel = new JLabel();
  37.     private JSpinner jSpeedSpinner = new JSpinner();
  38.     private JLabel jScoreLabel = new JLabel();
  39.     private JTextField jScoreTextField = new JTextField();
  40.     private JLabel jNextGraphLabel = new JLabel();
  41.     private TitledBorder titledBorder3;
  42.     private JLabel jCopyRightLabel = new JLabel();
  43.     private TitledBorder titledBorder1;
  44.     private TitledBorder titledBorder4;
  45.     private TitledBorder titledBorder5;
  46.     private TitledBorder titledBorder6;
  47.     private TitledBorder titledBorder7;
  48.     private TitledBorder titledBorder8;
  49.     public MainFrame() {
  50.         try {
  51.             setTitle("俄罗斯方块游戏");
  52.             setLocation(200, 130);
  53.             setSize(600, 400);
  54.             jbInit();
  55.             getContentPane().add(jMainPanel, "North");
  56.             getContentPane().addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
  57.                 public void keyPressed(KeyEvent e) {
  58.                     ProcessKeyEvent(e);
  59.                 }
  60.             });
  61.             addWindowListener(new WindowAdapter() {
  62.                 public void windowClosing(WindowEvent e) {
  63.                     dispose();
  64.                 }
  65.             });
  66.         }
  67.         catch (Exception e) {
  68.             e.printStackTrace();
  69.         }
  70.     }
  71.     public static void main(String[] args) {
  72.         MainFrame objMainFrame = new MainFrame();
  73.         objMainFrame.pack();
  74.         objMainFrame.setVisible(true);
  75.         objMainFrame.validate();
  76.         objMainFrame.setResizable(false);
  77.     }
  78.     private void jbInit() throws Exception {
  79.         jMainPanel.setBackground(new Color(72, 112, 112));
  80.         jMainPanel.setForeground(UIManager.getColor(
  81.             "TextArea.selectionBackground"));
  82.         jMainPanel.setLayout(xyLayout);
  83.         gGameAreaTable = new GameTable(15, 26);
  84.         gPreviewTable = new PreviewTable(5, 4);
  85.         jGamePanel = new GameAreaPanel(gGameAreaTable);
  86.         jNextGraphPanel = new PreviewPanel(gPreviewTable);
  87.         jStartButton.setBackground(Color.pink);
  88.         jStartButton.setFont(new java.awt.Font("Dialog", 0, 14));
  89.         jStartButton.setForeground(SystemColor.desktop);
  90.         jStartButton.setBorder(titledBorder1);
  91.         jStartButton.setBorderPainted(true);
  92.         jStartButton.setText("开始游戏");
  93.         jStartButton.addActionListener(new MainFrame_jStartButton_actionAdapter(this));
  94.         jRestartButton.setBackground(Color.pink);
  95.         jRestartButton.setFont(new java.awt.Font("Dialog", 0, 14));
  96.         jRestartButton.setForeground(SystemColor.desktop);
  97.         jRestartButton.setBorder(titledBorder1);
  98.         jRestartButton.setText("重新游戏");
  99.         jRestartButton.addActionListener(new
  100.                                          MainFrame_jRestartButton_actionAdapter(this));
  101.         jExitButton.setBackground(Color.pink);
  102.         jExitButton.setFont(new java.awt.Font("Dialog", 0, 14));
  103.         jExitButton.setForeground(SystemColor.desktop);
  104.         jExitButton.setBorder(titledBorder1);
  105.         jExitButton.setText("退出游戏");
  106.         jExitButton.addActionListener(new MainFrame_jExitButton_actionAdapter(this));
  107.         jExitButton.addActionListener(new MainFrame_jExitButton_actionAdapter(this));
  108.         jSpeedLabel.setFont(new java.awt.Font("SansSerif", 0, 14));
  109.         jSpeedLabel.setForeground(Color.orange);
  110.         jSpeedLabel.setBorder(titledBorder6);
  111.         jSpeedLabel.setText("速度选择");
  112.         jSpeedSpinner.setValue(new Integer(1));
  113.         jSpeedSpinner.addChangeListener(new SpeedChangeListener());
  114.         jScoreLabel.setText("分数记录");
  115.         jScoreLabel.setForeground(Color.orange);
  116.         jScoreLabel.setBorder(titledBorder6);
  117.         jScoreLabel.setDebugGraphicsOptions(0);
  118.         jScoreLabel.setFont(new java.awt.Font("SansSerif", 0, 14));
  119.         jScoreTextField.setBackground(Color.pink);
  120.         jScoreTextField.setBorder(null);
  121.         jScoreTextField.setEditable(false);
  122.         jScoreTextField.setText("0");
  123.         jNextGraphLabel.setFont(new java.awt.Font("SansSerif", 0, 14));
  124.         jNextGraphLabel.setForeground(Color.orange);
  125.         jNextGraphLabel.setBorder(titledBorder6);
  126.         jNextGraphLabel.setText("下一个图形");
  127.         jGamePanel.setBackground(new Color(0, 118, 125));
  128.         jCopyRightLabel.setText("Savage制作,2003-2010 Copyright...");
  129.         jCopyRightLabel.setForeground(Color.orange);
  130.         jCopyRightLabel.setFont(new java.awt.Font("SansSerif", 0, 14));
  131.         xyLayout.setWidth(407);
  132.         xyLayout.setHeight(504);
  133.         jSpeedSpinner.setBackground(SystemColor.desktop);
  134.         jSpeedSpinner.setBorder(BorderFactory.createLoweredBevelBorder());
  135.         jMainPanel.add(jGamePanel, new XYConstraints(20, 21, 257, 447));
  136.         jMainPanel.add(jNextGraphLabel, new XYConstraints(299, 159, 78, 28));
  137.         jMainPanel.add(jCopyRightLabel, new XYConstraints(62, 472, 246, 28));
  138.         jMainPanel.add(jRestartButton, new XYConstraints(299, 367, 88, 30));
  139.         jMainPanel.add(jStartButton, new XYConstraints(299, 311, 88, 30));
  140.         jMainPanel.add(jNextGraphPanel, new XYConstraints(299, 183, 78, 88));
  141.         jMainPanel.add(jScoreLabel, new XYConstraints(299, 79, 68, 28));
  142.         jMainPanel.add(jSpeedLabel, new XYConstraints(299, 7, 78, 28));
  143.         jMainPanel.add(jExitButton, new XYConstraints(299, 419, 88, 31));
  144.         jMainPanel.add(jScoreTextField, new XYConstraints(299, 110, 68, 24));
  145.         jMainPanel.add(jSpeedSpinner, new XYConstraints(300, 32, 69, 25));
  146.         intGraph = ( ( (int) Math.round(Math.random() * 12345)) % 8) + 1; //产生随机方块
  147.         switch (intGraph) { //初始化产生的方块
  148.             case 1:
  149.                 root = new One();
  150.                 break;
  151.             case 2:
  152.                 root = new Two();
  153.                 break;
  154.             case 3:
  155.                 root = new Three();
  156.                 break;
  157.             case 4:
  158.                 root = new Four();
  159.                 break;
  160.             case 5:
  161.                 root = new Five();
  162.                 break;
  163.             case 6:
  164.                 root = new Six();
  165.                 break;
  166.             case 7:
  167.                 root = new Seven();
  168.                 break;
  169.             case 8:
  170.                 root = new Eight();
  171.                 break;
  172.             default:
  173.                 break;
  174.         }
  175.         jNextGraphPanel.PreviewGraph(intGraph);
  176.         root.begin(); //占据桌子,详见root类
  177.         intNextGraph = ( ( (int) Math.round(Math.random() * 12345)) % 8) + 1; //产生下一个提前显示的方块
  178.         intGraph = intNextGraph;
  179.         timer = new Timer(nTime, new MyRun()); //创建记时器,调用MyRun
  180.         addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
  181.             public void keyPressed(KeyEvent e) {
  182.                 ProcessKeyEvent(e);
  183.             }
  184.         });
  185.     }
  186.     public class MyRun
  187.         implements ActionListener { //记时器调用的监听器
  188.         public void actionPerformed(ActionEvent e) {
  189.             if (!bCanMove)
  190.                 return;
  191.             if (!root.down()) { //将方块下落一格,如果不能再下降了,就调用下面语句
  192.                 DeleteLine(); //消行
  193.                 jNextGraphPanel.PreviewGraph(intGraph);
  194.                 switch (intGraph) { //产生新的方块
  195.                     case 1:
  196.                         root = new One();
  197.                         break;
  198.                     case 2:
  199.                         root = new Two();
  200.                         break;
  201.                     case 3:
  202.                         root = new Three();
  203.                         break;
  204.                     case 4:
  205.                         root = new Four();
  206.                         break;
  207.                     case 5:
  208.                         root = new Five();
  209.                         break;
  210.                     case 6:
  211.                         root = new Six();
  212.                         break;
  213.                     case 7:
  214.                         root = new Seven();
  215.                         break;
  216.                     case 8:
  217.                         root = new Eight();
  218.                         break;
  219.                     default:
  220.                         break;
  221.                 }
  222.                 intNextGraph = ( ( (int) Math.round(Math.random() * 12345)) % 8) +
  223.                     1;
  224.                 intGraph = intNextGraph;
  225.                 if (!root.begin()) { //如果失败,游戏结束
  226.                     jGamePanel.UpdateGraph(gGameAreaTable);
  227.                     GameResetGamePara();
  228.                 }
  229.                 else
  230.                     jGamePanel.UpdateGraph(gGameAreaTable);
  231.             }
  232.             else {
  233.                 jGamePanel.UpdateGraph(gGameAreaTable);
  234.             }
  235.         }
  236.     }
  237.     public void ProcessKeyEvent(KeyEvent e) { //判断输入的是哪个键,并做出响应
  238.         jNextGraphPanel.PreviewGraph(intGraph);
  239.         switch (e.getKeyCode()) {
  240.             case KeyEvent.VK_DOWN: //如果是向下键,就一下到底,并产生一个新的方块
  241.                 root.downTo();
  242.                 DeleteLine(); //消去添满的一行,并加10分
  243.                 switch (intGraph) { //产生新的方块
  244.                     case 1:
  245.                         root = new One();
  246.                         break;
  247.                     case 2:
  248.                         root = new Two();
  249.                         break;
  250.                     case 3:
  251.                         root = new Three();
  252.                         break;
  253.                     case 4:
  254.                         root = new Four();
  255.                         break;
  256.                     case 5:
  257.                         root = new Five();
  258.                         break;
  259.                     case 6:
  260.                         root = new Six();
  261.                         break;
  262.                     case 7:
  263.                         root = new Seven();
  264.                         break;
  265.                     case 8:
  266.                         root = new Eight();
  267.                         break;
  268.                     default:
  269.                         break;
  270.                 }
  271.                 intNextGraph = ( ( (int) Math.round(Math.random() * 12345)) % 8) +
  272.                     1;
  273.                 intGraph = intNextGraph;
  274.                 if (!root.begin()) { //如果新的方块产生失败,则游戏结束
  275.                     //jGamePanel.UpdateGraph(gGameAreaTable);
  276.                     GameResetGamePara();
  277.                 }
  278.                 else {
  279.                     jGamePanel.UpdateGraph(gGameAreaTable);
  280.                 }
  281.                 break;
  282.             case KeyEvent.VK_LEFT:
  283.                 root.left();
  284.                 jGamePanel.UpdateGraph(gGameAreaTable);
  285.                 break; //详见root类
  286.             case KeyEvent.VK_RIGHT:
  287.                 root.right();
  288.                 jGamePanel.UpdateGraph(gGameAreaTable);
  289.                 break;
  290.             case KeyEvent.VK_UP:
  291.                 root.change();
  292.                 jGamePanel.UpdateGraph(gGameAreaTable);
  293.                 break;
  294.             default:
  295.                 break;
  296.         }
  297.     }
  298.     public void DeleteLine() { //消去添满的一行,并加10分
  299.         boolean isCan; //标志是否循环消一行
  300.         boolean isContinue = true; //标志是否还有待消的行
  301.         int k = gGameAreaTable.y - 1;
  302.         while (isContinue) {
  303.             isCan = true;
  304.             while (isCan) {
  305.                 for (int i = 0; i < gGameAreaTable.x; i++) {
  306.                     if (gGameAreaTable.myTable[i][k] != 1)
  307.                         isCan = false;
  308.                 }
  309.                 if (isCan) { //满足条件,开始消行
  310.                     for (int i = 0; i < gGameAreaTable.x; i++)
  311.                         gGameAreaTable.myTable[i][k] = 0;
  312.                     for (int j = k - 1; j >= 0; j--)
  313.                         for (int i = 0; i < gGameAreaTable.x; i++) {
  314.                             if (gGameAreaTable.myTable[i][j] == 1) {
  315.                                 gGameAreaTable.myTable[i][j + 1] = 1;
  316.                                 gGameAreaTable.myTable[i][j] = 0;
  317.                             }
  318.                         }
  319.                     jGamePanel.UpdateGraph(gGameAreaTable);
  320.                     intTotalScore += 10; //加成绩
  321.                     UpdateScore(intTotalScore);
  322.                 }
  323.             }
  324.             k--;
  325.             if (k < 1)
  326.                 isContinue = false;
  327.         }
  328.     }
  329.     public void GameResetGamePara() { //游戏结束
  330.         timer.stop();
  331.         bCanMove = false;
  332.         JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
  333.         anOptionPane.showMessageDialog(this, "游戏结束,再来一次?", "Game Over!",
  334.                                        JOptionPane.WARNING_MESSAGE);
  335.         bCanMove = false;
  336.         ResetGamePara(); //处理各个变量,以便重新开始
  337.     }
  338. //游戏结束后,处理各个变量,以便重新开始
  339.     public void ResetGamePara() {
  340.         timer.stop();
  341.         for (int i = 0; i < gGameAreaTable.x; i++) //重新给游戏桌置0标志
  342.             for (int j = 0; j < gGameAreaTable.y; j++) {
  343.                 gGameAreaTable.myTable[i][j] = 0;
  344.             }
  345.         jGamePanel.UpdateGraph(gGameAreaTable);
  346.         bCanMove = false;
  347.         intTotalScore = 0;
  348.         jScoreTextField.setText( (new Integer(intTotalScore)).toString());
  349.         nTime = 1000;
  350.         intSpeed = 1;
  351.         jSpeedSpinner.setValue(new Integer(intSpeed));
  352.         switch (intGraph) { //产生新的方块
  353.             case 1:
  354.                 root = new One();
  355.                 break;
  356.             case 2:
  357.                 root = new Two();
  358.                 break;
  359.             case 3:
  360.                 root = new Three();
  361.                 break;
  362.             case 4:
  363.                 root = new Four();
  364.                 break;
  365.             case 5:
  366.                 root = new Five();
  367.                 break;
  368.             case 6:
  369.                 root = new Six();
  370.                 break;
  371.             case 7:
  372.                 root = new Seven();
  373.                 break;
  374.             case 8:
  375.                 root = new Eight();
  376.                 break;
  377.             default:
  378.                 break;
  379.         }
  380.         intNextGraph = ( ( (int) Math.round(Math.random() * 12345)) % 8) + 1;
  381.         intGraph = intNextGraph;
  382.         jGamePanel.UpdateGraph(gGameAreaTable);
  383.         root.begin();
  384.     }
  385.     void jStartButton_actionPerformed(ActionEvent e) {
  386.         jStartButton.setFocusable(false);
  387.         jRestartButton.setFocusable(false);
  388.         jExitButton.setFocusable(false);
  389.         this.getContentPane().setFocusable(true);
  390.         this.getContentPane().setFocusCycleRoot(true);
  391.         bCanMove = true;
  392.         timer = new Timer(nTime, new MyRun()); //根据选择的nTime间隔重新设置记时器,以变换速度
  393.         timer.start();
  394.     }
  395.     void jRestartButton_actionPerformed(ActionEvent e) {
  396.         timer.stop();
  397.         jGamePanel.Init();
  398.         bCanMove = false;
  399.     }
  400.     void jExitButton_actionPerformed(ActionEvent e) {
  401.         System.exit(0);
  402.     }
  403.     public class SpeedChangeListener
  404.         implements ChangeListener { //速度控件监听器
  405.         public void stateChanged(ChangeEvent e) {
  406.             if (e.getSource() == jSpeedSpinner) {
  407.                 String strSpeed = new String(jSpeedSpinner.getValue().toString());
  408.                 intSpeed = Integer.parseInt(strSpeed);
  409.                 if (intSpeed < 1)
  410.                     intSpeed = 1;
  411.                 if (intSpeed > 9)
  412.                     intSpeed = 9;
  413.                 jSpeedSpinner.setValue(new Integer(intSpeed));
  414.             }
  415.             UpdateSpeed();
  416.         }
  417.     }
  418.     public void UpdateSpeed() {
  419.         jSpeedSpinner.setValue(new Integer(intSpeed));
  420.         String strSpeed = new String(jSpeedSpinner.getValue().toString());
  421.         intSpeed = Integer.parseInt(strSpeed);
  422.         nTime = 1000 - (intSpeed * 110);
  423.         timer = new Timer(nTime, new MyRun()); //根据选择的nTime间隔重新设置记时器,以变换速度
  424.         if (bCanMove)
  425.             timer.start();
  426.     }
  427.     public void UpdateScore(int intScore) {
  428.         if (intScore > intSpeed * 10)
  429.             intSpeed++;
  430.         if (intSpeed > 9)
  431.             intSpeed = 1;
  432.         UpdateSpeed();
  433.         jScoreTextField.setText( (new Integer(intScore)).toString());
  434.     }
  435. }
  436. class MainFrame_jStartButton_actionAdapter
  437.     implements java.awt.event.ActionListener {
  438.     MainFrame adaptee;
  439.     MainFrame_jStartButton_actionAdapter(MainFrame adaptee) {
  440.         this.adaptee = adaptee;
  441.     }
  442.     public void actionPerformed(ActionEvent e) {
  443.         adaptee.jStartButton_actionPerformed(e);
  444.     }
  445. }
  446. class MainFrame_jRestartButton_actionAdapter
  447.     implements java.awt.event.ActionListener {
  448.     MainFrame adaptee;
  449.     MainFrame_jRestartButton_actionAdapter(MainFrame adaptee) {
  450.         this.adaptee = adaptee;
  451.     }
  452.     public void actionPerformed(ActionEvent e) {
  453.         adaptee.jRestartButton_actionPerformed(e);
  454.     }
  455. }
  456. class MainFrame_jExitButton_actionAdapter
  457.     implements java.awt.event.ActionListener {
  458.     MainFrame adaptee;
  459.     MainFrame_jExitButton_actionAdapter(MainFrame adaptee) {
  460.         this.adaptee = adaptee;
  461.     }
  462.     public void actionPerformed(ActionEvent e) {
  463.         adaptee.jExitButton_actionPerformed(e);
  464.     }
  465. }