?? rectangthread.java
字號(hào):
package net.rectang;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JPanel;public class RectangThread extends Thread implements KeyListener{ public static final int SIZE = 10; private int windowWidth = 0; private int windowHeight = 0; private IndexLocation indexLocation = null; private Rectangle rectangle = null; private Index startIndex = null; private Graphics graphics = null; private JPanel workPanel = null; private boolean flag = true; 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 RectangThread(Index startIndex, JPanel workPanel) { super(); this.startIndex = startIndex; this.workPanel = workPanel; this.flag = true; } @Override public void run() { graphics = workPanel.getGraphics(); windowWidth = workPanel.getWidth(); windowHeight = workPanel.getHeight(); rectangle = new Rectangle(); indexLocation = rectangle.getAllLocation(); workPanel.setFocusable(true); while(flag){ synchronized (graphics) { clearRectang(indexLocation); move(derector, indexLocation); drawRectang(indexLocation); try { sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } } try { join(); } catch (InterruptedException e) { e.printStackTrace(); } } 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); } public void move(int derector, IndexLocation indexLocation) { if(derector == RectangThread.MOVE_RIGHT){ 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 == RectangThread.MOVE_LEFT){ 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 == RectangThread.MOVE_DOWN){ }else if(derector == RectangThread.CHANGE){ } else if(derector == RectangThread.NATURE_DOWN){ 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); } } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public int getWindowWidth() { return windowWidth; } public void setWindowWidth(int windowWidth) { this.windowWidth = windowWidth; } public int getWindowHeight() { return windowHeight; } public void setWindowHeight(int windowHeight) { this.windowHeight = windowHeight; } public IndexLocation getIndexLocation() { return indexLocation; } public void setIndexLocation(IndexLocation indexLocation) { this.indexLocation = indexLocation; } public Rectangle getRectangle() { return rectangle; } public void setRectangle(Rectangle rectangle) { this.rectangle = rectangle; } public Index getStartIndex() { return startIndex; } public void setStartIndex(Index startIndex) { this.startIndex = startIndex; } public Graphics getGraphics() { return graphics; } public void setGraphics(Graphics graphics) { this.graphics = graphics; } public JPanel getWorkPanel() { return workPanel; } public void setWorkPanel(JPanel workPanel) { this.workPanel = workPanel; } 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) { }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -