📄 rectangactioncontroller.java
字号:
package net.rectang;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JFrame;import javax.swing.JOptionPane;public class RectangActionController implements ActionListener, WindowListener, KeyListener{ public static final String NEW_GAME = "new game"; public static final String PASE_GAME = "pase game"; public static final String EXIT_GAME = "exit game"; public static final int SIZE = 10; public static final int MOVE_RIGHT = 1; public static final int MOVE_LEFT = 2; public static final int MOVE_DOWN = 3; public static final int CHANGE = 4; public static final int NATURE_DOWN = 5; private int derector = NATURE_DOWN; public static final int DEFAULT_SIZE = 5; private IndexLocation indexLocation = null; private Rectangle rectang = null; private Graphics graphics = null; private int windowWidth; private int windowHeight; private boolean flag = true; private boolean[][] masks = new boolean[12][25]; private int sleepTime = 400; private JFrame win; private MainPanel mainPanel; private static RectangActionController instance;// static{// instance = new ActionController();// } private RectangActionController(){ } public synchronized static RectangActionController getInstance(){ if(instance == null){ instance = new RectangActionController(); } return instance; } public JFrame getWin() { return win; } public void setWin(JFrame win) { this.win = win; } public MainPanel getMainPanel() { return mainPanel; } public void setMainPanel(MainPanel mainPanel) { this.mainPanel = mainPanel; } public void startGame(){ win.setSize(175, 332); win.setContentPane(mainPanel); win.setVisible(true); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals(NEW_GAME)){ gameStart(); }else if(cmd.equals(PASE_GAME)){ }else if(cmd.equals(EXIT_GAME)){ exit(); } } private void gameStart(){ flag = true; for(int i = 0; i < 12; i++){ for(int j = 0; j < 25; j++){ masks[i][j] = false; if(j ==24){ masks[i][j] = true; } } } rectang = new Rectangle(); graphics = mainPanel.getRecPanel().getGraphics(); indexLocation = rectang.getAllLocation(); windowWidth = mainPanel.getRecPanel().getWidth(); windowHeight = mainPanel.getRecPanel().getHeight(); GameThread gameThread = new GameThread(); gameThread.start(); } private class GameThread extends Thread{ public void run(){ int score = 0; int mask = 0; sleepTime = 300; derector = NATURE_DOWN; while(flag){ synchronized (graphics) { clearRectang(indexLocation); if(derector != NATURE_DOWN){ move(derector, indexLocation); derector = NATURE_DOWN; }else{ indexLocation = getNextIndex(indexLocation); } drawRectang(indexLocation); if(downStop(indexLocation)){ masks[(indexLocation.getIndexOne().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexOne().getLocationY() - DEFAULT_SIZE) / 10] = true; masks[(indexLocation.getIndexTwo().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexTwo().getLocationY() - DEFAULT_SIZE) / 10] = true; masks[(indexLocation.getIndexThree().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexThree().getLocationY() - DEFAULT_SIZE) / 10] = true; masks[(indexLocation.getIndexFour().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexFour().getLocationY() - DEFAULT_SIZE) / 10] = true; rectang = new Rectangle(); indexLocation = rectang.getAllLocation(); sleepTime = 300; score = 0; for(int j = 23; j > 1; j--){ for(int i = 0; i < 12; i++){ if(!masks[i][j]){ mask = 1; break; } } if(mask == 0){ score = ((score + 1) / 2) * 2 + 1; clear(j); j++; } mask = 0; } mainPanel.getScoreNumLabel().setText(Integer.parseInt(mainPanel.getScoreNumLabel().getText()) + score + ""); for(int i = 0; i < 12; i++){ if(masks[i][0] == true){ JOptionPane.showMessageDialog(win, "Game Over!"); flag = false; break; } } } try { sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } } } private void clear(int j) { int step = j; for(int i = 0; i < 12; step--){ if(!masks[i][step - 1]){ masks[i][step] = false; graphics.clearRect(i * 10 + DEFAULT_SIZE, step * 10 + DEFAULT_SIZE, SIZE, SIZE); step = j + 1; i++; } } } private boolean downStop(IndexLocation indexLocation) { if(indexLocation.getMaxHeight() >= (windowHeight - 3 * DEFAULT_SIZE)){ return true; } if(indexLocation.getIndexOne().getLocationY() >= DEFAULT_SIZE){ if(masks[(indexLocation.getIndexOne().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexOne().getLocationY() + DEFAULT_SIZE) / 10]){ return true; } } if(indexLocation.getIndexTwo().getLocationY() >= DEFAULT_SIZE){ if(masks[(indexLocation.getIndexTwo().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexTwo().getLocationY() + DEFAULT_SIZE) / 10]){ return true; } } if(indexLocation.getIndexThree().getLocationY() >= DEFAULT_SIZE){ if(masks[(indexLocation.getIndexThree().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexThree().getLocationY() + DEFAULT_SIZE) / 10]){ return true; } } if(indexLocation.getIndexFour().getLocationY() >= DEFAULT_SIZE){ if(masks[(indexLocation.getIndexFour().getLocationX() - DEFAULT_SIZE) / 10][(indexLocation.getIndexFour().getLocationY() + DEFAULT_SIZE) / 10]){ return true; } } return false; } } private void clearRectang(IndexLocation indexLocation){ graphics.clearRect(indexLocation.getIndexOne().getLocationX(), indexLocation.getIndexOne().getLocationY(), SIZE, SIZE); graphics.clearRect(indexLocation.getIndexTwo().getLocationX(), indexLocation.getIndexTwo().getLocationY(), SIZE, SIZE); graphics.clearRect(indexLocation.getIndexThree().getLocationX(), indexLocation.getIndexThree().getLocationY(), SIZE, SIZE); graphics.clearRect(indexLocation.getIndexFour().getLocationX(), indexLocation.getIndexFour().getLocationY(), SIZE, SIZE); } private void drawRectang(IndexLocation indexLocation){ graphics.fillRect(indexLocation.getIndexOne().getLocationX(), indexLocation.getIndexOne().getLocationY(), SIZE, SIZE); graphics.fillRect(indexLocation.getIndexTwo().getLocationX(), indexLocation.getIndexTwo().getLocationY(), SIZE, SIZE); graphics.fillRect(indexLocation.getIndexThree().getLocationX(), indexLocation.getIndexThree().getLocationY(), SIZE, SIZE); graphics.fillRect(indexLocation.getIndexFour().getLocationX(), indexLocation.getIndexFour().getLocationY(), SIZE, SIZE); } private IndexLocation getNextIndex(IndexLocation indexLocation){ indexLocation.getIndexOne().setLocationY(indexLocation.getIndexOne().getLocationY() + SIZE); indexLocation.getIndexTwo().setLocationY(indexLocation.getIndexTwo().getLocationY() + SIZE); indexLocation.getIndexThree().setLocationY(indexLocation.getIndexThree().getLocationY() + SIZE); indexLocation.getIndexFour().setLocationY(indexLocation.getIndexFour().getLocationY() + SIZE); return indexLocation; } public void move(int derector, IndexLocation indexLocation) { if(derector == RectangActionController.MOVE_RIGHT){ //System.out.println(windowWidth + "" + windowHeight); if(indexLocation.getMaxWidth() < windowWidth - DEFAULT_SIZE - SIZE){ indexLocation.getIndexOne().setLocationX(indexLocation.getIndexOne().getLocationX() + SIZE); indexLocation.getIndexTwo().setLocationX(indexLocation.getIndexTwo().getLocationX() + SIZE); indexLocation.getIndexThree().setLocationX(indexLocation.getIndexThree().getLocationX() + SIZE); indexLocation.getIndexFour().setLocationX(indexLocation.getIndexFour().getLocationX() + SIZE); } }else if(derector == RectangActionController.MOVE_LEFT){ if(indexLocation.getMinWidth() > DEFAULT_SIZE){ indexLocation.getIndexOne().setLocationX(indexLocation.getIndexOne().getLocationX() - SIZE); indexLocation.getIndexTwo().setLocationX(indexLocation.getIndexTwo().getLocationX() - SIZE); indexLocation.getIndexThree().setLocationX(indexLocation.getIndexThree().getLocationX() - SIZE); indexLocation.getIndexFour().setLocationX(indexLocation.getIndexFour().getLocationX() - SIZE); } }else if(derector == RectangActionController.MOVE_DOWN){ sleepTime = 20; }else if(derector == RectangActionController.CHANGE){ indexLocation.setIndexTwo(ChangeIndex(indexLocation.getIndexTwo(), indexLocation.getIndexOne())); indexLocation.setIndexThree(ChangeIndex(indexLocation.getIndexThree(), indexLocation.getIndexOne())); indexLocation.setIndexFour(ChangeIndex(indexLocation.getIndexFour(), indexLocation.getIndexOne())); } } private Index ChangeIndex(Index changeIndex, Index orderIndex){ if(changeIndex.getLocationY() == orderIndex.getLocationY()){ changeIndex.setLocationY(orderIndex.getLocationY() + changeIndex.getLocationX() - orderIndex.getLocationX()); changeIndex.setLocationX(orderIndex.getLocationX()); }else if(changeIndex.getLocationX() == orderIndex.getLocationX()){ changeIndex.setLocationX(orderIndex.getLocationX() + orderIndex.getLocationY() - changeIndex.getLocationY()); changeIndex.setLocationY(orderIndex.getLocationY()); }else if((changeIndex.getLocationX() - orderIndex.getLocationX()) * (changeIndex.getLocationY() - orderIndex.getLocationY()) == SIZE * SIZE){ changeIndex.setLocationX(2 * orderIndex.getLocationX() - changeIndex.getLocationX()); //changeIndex.setLocationY(orderIndex.getLocationY()); }else if((changeIndex.getLocationX() - orderIndex.getLocationX()) * (changeIndex.getLocationY() - orderIndex.getLocationY()) == -SIZE * SIZE){ changeIndex.setLocationY(2 * orderIndex.getLocationY() - changeIndex.getLocationY()); //changeIndex.setLocationX(orderIndex.getLocationX()); } return changeIndex; } private void exit() { int isExit = JOptionPane.showConfirmDialog(win, "确认离开吗?"); if(isExit == JOptionPane.YES_OPTION) System.exit(0); } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { if(e.getWindow() == win){//是否是主窗口 exit(); } } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_RIGHT){ derector = MOVE_RIGHT; }else if(e.getKeyCode() == KeyEvent.VK_LEFT){ derector = MOVE_LEFT; }else if(e.getKeyCode() == KeyEvent.VK_DOWN){ derector = MOVE_DOWN; }else if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_ALT){ derector = CHANGE; }else{ derector = NATURE_DOWN; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -