public void dealWithTrans() { if (!isCoordinateLock) { isTransforStart = true; cTetrisBlock.tetrisBlockShapeTransformation(m_blockGrid); cGameDc.repaint(); isTransforStart = false; } }
public void dealWithDown() { if (!isCoordinateLock) { if (!isToEnd(cTetrisBlock.m_blockBottomY)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX(), cTetrisBlock.getTetrisCoordinateY() + 20); cGameDc.repaint(); } }
public void dealWithRight() { if (!isCoordinateLock) { if (!isRightToEnd(cTetrisBlock.m_blockRightX)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX() + 20, cTetrisBlock.getTetrisCoordinateY()); cGameDc.repaint(); } }
/** the method for c++ dll */ public void dealWithLeft() { System.out.println("nihaoma"); if (!isCoordinateLock) { if (!isLeftToEnd(cTetrisBlock.m_blockLeftX)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX() - 20, cTetrisBlock.getTetrisCoordinateY()); cGameDc.repaint(); } }
/** * inti the UI and some params * * @param none */ public void initUI() { // cNextBlockDC.setLayout(new BorderLayout()); getContentPane().setLayout(null); // the total panel JPanel outer = new JPanel(); getContentPane().add(outer); outer.setBounds(4, 4, frameWeight - 20, frameHeight - 50); outer.setBorder(BorderFactory.createTitledBorder("Game DC")); outer.setLayout(null); JPanel rightPanel = new JPanel(); rightPanel.setLayout(null); // the panel to show next block JPanel tipNext = new JPanel(); tipNext.setBounds(10, 30, 130, 80); tipNext.setBorder(BorderFactory.createTitledBorder("Next Block")); tipNext.setLayout(null); cNextBlockDC.setBounds(25, 20, 48, 48); tipNext.add(cNextBlockDC); rightPanel.add(tipNext); // the panel to show the score JPanel playerScore = new JPanel(); playerScore.setBounds(10, 110, 130, 40); playerScore.setBorder(BorderFactory.createTitledBorder("Score")); playerScore.setLayout(null); m_lTotalScore.setBounds(40, 15, 100, 20); playerScore.add(m_lTotalScore); rightPanel.add(playerScore); // the panel to show the button JPanel buttonArea = new JPanel(); buttonArea.setBounds(10, 160, 130, 170); buttonArea.setBorder(BorderFactory.createTitledBorder("Game Control")); buttonArea.setLayout(null); m_bGameHalt.setBounds(10, 30, 110, 30); buttonArea.add(m_bGameHalt); m_bGameStart.setBounds(10, 62, 110, 30); buttonArea.add(m_bGameStart); m_bGameOver.setBounds(10, 94, 110, 30); buttonArea.add(m_bGameOver); m_bVoiceControl.setBounds(10, 126, 110, 30); buttonArea.add(m_bVoiceControl); rightPanel.add(buttonArea); // the Panel to show the tips for the game JPanel gameTip = new JPanel(); gameTip.setBounds(10, 330, 130, 160); gameTip.setBorder(BorderFactory.createTitledBorder("Game Tip")); gameTip.setLayout(null); m_lGameTip.setBounds(10, 20, 130, 100); Font fnt = new Font("楷体_GB2312", Font.ITALIC, 15); m_lGameTip.setText( "<html><body>" + "<font style='color:#2f4f4f;size:10px;'>" + "1.由方向键控制游戏<br/>,向上有变形;<br/>2." + "语音控制命令见配<br/>置文件;<br/>3.Author:"); // the link for blog JLabel linkForBlog = new JLabel("<html><body><a style='color:#00755e;' href ='#'>kymo</a></body></html>"); linkForBlog.setCursor(new Cursor(Cursor.HAND_CURSOR)); linkForBlog.setBounds(20, 113, 130, 15); // add mouse event linkForBlog.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { //// System.out.println("click !"); try { Desktop.getDesktop().browse(new java.net.URI(kymoBlog)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (URISyntaxException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); // the link to open voice xml file JLabel linkForXml = new JLabel( "<html><body><a style='color:#00755e;' href ='#'>open voice xml</a></body></html>"); linkForXml.setCursor(new Cursor(Cursor.HAND_CURSOR)); linkForXml.setBounds(20, 128, 130, 15); // add mouse event linkForXml.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { // JLabel xmlFileContent = new JLabel(); try { Runtime.getRuntime() .exec("cmd /c start " + System.getProperty("user.dir") + "\\voice.xml"); } catch (IOException ex) { //// System.out.println(ex); } } }); gameTip.add(linkForBlog); gameTip.add(linkForXml); gameTip.add(m_lGameTip); rightPanel.add(gameTip); // pack(); rightPanel.setBounds(340, 30, 150, 500); rightPanel.setBorder(BorderFactory.createTitledBorder("Game Control&Tips")); outer.add(rightPanel); cGameDc = new DrawComponent(this, cTetrisBlock); cGameDc.setBounds(30, 30, 300, 500); outer.add(cGameDc); setVisible(true); setResizable(false); pack(); this.setSize(frameWeight, frameHeight); this.setLocation(frameLocaX, frameLocaY); m_bGameHalt.addActionListener((ActionListener) this); m_bGameStart.addActionListener((ActionListener) this); m_bGameOver.addActionListener((ActionListener) this); m_bVoiceControl.addActionListener((ActionListener) this); // if the windows is close ,the game thread will end this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { isGameOver = true; System.exit(0); } }); // the listen of the keyboard Toolkit.getDefaultToolkit() .addAWTEventListener( new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { // TODO Auto-generated method stub if (((KeyEvent) event).getID() == KeyEvent.KEY_RELEASED) isKeyPressed = false; if (((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) { // 放入自己的键盘监听事件 // ((KeyEvent) event).getKeyCode();// 获取按键的code // ((KeyEvent) event).getKeyChar();// // 获取按键的字符////System.out.println(e.getKeyCode()); if (!isGameHalt && isGameStart) { if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_RIGHT) { if (!isCoordinateLock) { isKeyPressed = true; if (!isRightToEnd(cTetrisBlock.m_blockRightX)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX() + 20, cTetrisBlock.getTetrisCoordinateY()); cGameDc.repaint(); } } else if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_LEFT) { if (!isCoordinateLock) { isKeyPressed = true; /* for(int j = 0; j < 4; j ++) //System.out.print(cTetrisBlock.m_blockLeftX[j] + " "); */ //// System.out.println(); if (!isLeftToEnd(cTetrisBlock.m_blockLeftX)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX() - 20, cTetrisBlock.getTetrisCoordinateY()); cGameDc.repaint(); } } else if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_DOWN) { if (!isCoordinateLock) { isKeyPressed = true; if (!isToEnd(cTetrisBlock.m_blockBottomY)) cTetrisBlock.setCoordinateXY( cTetrisBlock.getTetrisCoordinateX(), cTetrisBlock.getTetrisCoordinateY() + 20); cGameDc.repaint(); } } else if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_UP) { if (!isCoordinateLock) { isKeyPressed = true; isTransforStart = true; cTetrisBlock.tetrisBlockShapeTransformation(m_blockGrid); cGameDc.repaint(); isTransforStart = false; } } } } } }, AWTEvent.KEY_EVENT_MASK); }