?? animatedelete.java
字號:
package kyodai.map;
import java.awt.*;
import javax.swing.*;
import kyodai.*;
/**
* 消除連連看方塊的類
*/
public class AnimateDelete implements Runnable {
static JButton[] dots;
static long delay = 20l;
int[] array = new int[44]; //最大距離只可能為2行1列
private int count = 0;
private volatile Thread thread;
public AnimateDelete(JButton[] dots) {
this.dots = dots;
array = new int[0];
}
/**
* 初始化
*/
public AnimateDelete(int direct, Point a, Point b) {
initArray();
calcTwoPoint(direct, a, b);
start();
}
/**
* direct 方向
* 1表示a, b在同一直線上,b, c在同一豎線上;
* 0表示a, b在同一豎線上,b, c在同一直線上
*/
public AnimateDelete(int direct, Point a, Point b, Point c) {
initArray();
if (direct == 1) { //先橫后豎
calcTwoPoint(1, a, b);
count--;
calcTwoPoint(0, b, c);
}
else {
calcTwoPoint(0, a, b);
count--;
calcTwoPoint(1, b, c);
}
start();
}
/**
* direct 方向
* 1表示a, b為橫線,b, c為豎線, c, d為橫線
* 0表示a, b為豎線,b, c為橫線,c, d為豎線
*/
public AnimateDelete(int direct, Point a, Point b, Point c, Point d) {
initArray();
if (direct == 1) { //橫、豎、橫方式處理
calcTwoPoint(1, a, b);
count--;
calcTwoPoint(0, b, c);
count--;
calcTwoPoint(1, c, d);
}
else { //豎、橫、豎方式處理
calcTwoPoint(0, a, b);
count--;
calcTwoPoint(1, b, c);
count--;
calcTwoPoint(0, c, d);
}
start();
}
/**
* 計算消除的兩點
*/
private void calcTwoPoint(int direct, Point a, Point b) {
int offset = 0;
if (direct == 1) { //橫向連接
if (a.y > b.y) { //a點向b點是從右向左在水平線上消除
for (int y = a.y; y >= b.y; y--) {
offset = a.x * Setting.COLUMN + y;
array[count] = offset;
count++;
}
}
else { //a點向b點是從左向右在水平線上消除
for (int y = a.y; y <= b.y; y++) {
offset = a.x * Setting.COLUMN + y;
array[count] = offset;
count++;
}
}
}
else { //豎向連接
if (a.x > b.x) { //a點向b點是從下向上垂直消除
for (int x = a.x; x >= b.x; x--) {
offset = x * Setting.COLUMN + a.y;
array[count] = offset;
count++;
}
}
else { //a點向b點是從上向下垂直消除
for (int x = a.x; x <= b.x; x++) {
offset = x * Setting.COLUMN + a.y;
array[count] = offset;
count++;
}
}
}
}
/**
* 設置動畫速度
*/
public void setSpeed(int speed) {
delay = speed * 10;
}
private void initArray() {
if (array == null || array.length == 0) {
return;
}
for (int i = 0; i < array.length; i++) {
array[i] = -1;
}
}
public void test() {
if (array == null || array.length == 0) {
return;
}
for (int i = 0; i < array.length; i++) {
if (array[i] != -1) {
message("[" + array[i] + "] ");
}
}
System.out.println();
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
if (count < 2) {
return;
}
Thread currentThread = Thread.currentThread();
boolean animate = true;
while (thread == currentThread && animate) {
for (int i = 1; i < count - 1; i++) {
dots[array[i]].setEnabled(true);
dots[array[i]].setIcon(Kyodai.GuideIcon);
try {
thread.sleep(delay);
}
catch (InterruptedException ex) {
}
}
for (int i = 1; i < count - 1; i++) {
dots[array[i]].setIcon(null);
dots[array[i]].setEnabled(false);
try {
thread.sleep(delay);
}
catch (InterruptedException ex) {
}
}
dots[array[0]].setIcon(null);
dots[array[0]].setEnabled(false);
dots[array[count - 1]].setIcon(null);
dots[array[count - 1]].setEnabled(false);
animate = false;
}
stop();
}
public void stop() {
if (thread != null) {
thread = null;
}
}
void message(String str) {
System.out.println(str);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -