?? mapui.java
字號:
package kyodai.map;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import kyodai.*;
/**
* 生成圖形用戶界面
*/
public class MapUI extends JPanel
implements ActionListener, Runnable {
private Map map;
private JButton[] dots;
private Point lastPoint = new Point(0, 0); //上一個點的坐標
private boolean isSelected = false; //是否已經選擇了一個點
private int score = 0; //記錄用戶的得分
private ClockAnimate clockAnimate; //同步顯示時鐘
//AnimateDelete animate; //動畫
JButton goTop10;
private ScoreAnimate scoreAnimate;
int stepScore = 0; //計算距離的分
int limitTime = 0; //限定尋找的時間(秒)
private boolean isPlaying = false; //當前是否正在游戲中
/**
* 構造函數
*/
public MapUI(Map map, JButton[] dots) {
this.map = map;
this.dots = dots;
GridLayout gridLayout = new GridLayout();
this.setLayout(gridLayout);
gridLayout.setRows(Setting.ROW);
gridLayout.setColumns(Setting.COLUMN);
gridLayout.setHgap(2);
gridLayout.setVgap(2);
this.setLayout(gridLayout);
this.setBackground(Kyodai.DarkColor);
for (int row = 0; row < Setting.ROW; row++) {
for (int col = 0; col < Setting.COLUMN; col++) {
int index = row * Setting.COLUMN + col;
dots[index].addActionListener(this);
this.add(dots[index]);
}
}
}
/**
* 設置地圖
*/
public void setMap(Map map) {
this.map = map;
}
/**
* 獲取主界面上的goTop10按鈕,以便操作
*/
public void setTop10Button(JButton goTop10) {
this.goTop10 = goTop10;
}
/**
* 根據數組來繪置畫面
*/
private void paint() {
for (int row = 0; row < Setting.ROW; row++) {
for (int col = 0; col < Setting.COLUMN; col++) {
int index = row * Setting.COLUMN + col;
if (map.getMap()[row][col] > 0) {
dots[index].setIcon(Kyodai.BlocksIcon[map.getMap()[row][col] - 1]);
dots[index].setEnabled(true);
}
else {
dots[index].setIcon(null);
dots[index].setEnabled(false);
}
}
}
}
public void repaint(Graphics g) {
paint();
}
/**
* 判斷當前是否已經沒有可消除的方塊
*/
private boolean validMap(Point a) {
if (map.getCount() == 0) {
return true;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(true); //只測試
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { //找到了可消除的
return true;
}
else {
return false;
}
}
/**
* 更新當前顯示的分數
*/
private void showScore(int l, int c) {
if (scoreAnimate == null) {
return;
}
scoreAnimate.setScore(l, c);
}
/**
* 刷新當前的排列方式
*/
public void refresh() {
if (!isPlaying) { //不在游戲中,返回
return;
}
if (map.getCount() == 0) {
Kyodai.showHint("還刷,都沒了!");
}
if (Setting.Sound == 1) {
new Sound(Sound.REFRESH);
}
if (validMap(new Point( -1, -1))) {
score -= Setting.freshScore;
showScore(score - 1, score);
}
else {
showScore(score, score + Setting.freshScore);
score += Setting.freshScore;
}
score -= Setting.freshScore;
showScore(score - 1, score);
map.refresh();
paint();
}
/**
* 消除兩個點
*/
void earse(Point a, Point b) {
//paint();
int offset;
offset = a.x * Setting.COLUMN + a.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
offset = b.x * Setting.COLUMN + b.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
//如果地圖清除完成,關閉
if (map.getCount() == 0) {
int remainTime = limitTime - clockAnimate.getUsedTime();
message("剩余時間 = " + remainTime);
if (remainTime > 0) {
showScore(score, score + remainTime * Setting.timeScore);
score += remainTime * Setting.timeScore;
}
isPlaying = false;
stop();
Kyodai.showHint("時間+ " + remainTime * Setting.timeScore + ",想看看你的排名嗎?");
goTop10.setEnabled(true);
}
else {
//test1(map.getDeleteArray());
}
}
/**
* 自動尋找最佳答案
*/
public void findNext(Point a) {
if (!isPlaying) { //不在游戲中,返回
return;
}
if (map.getCount() == 0) {
Kyodai.showHint("你找昏了頭吧,沒了!");
return;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(true); //告訴map當前只是測試,并不需要進行刪除動畫
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { //找到了可消除的
if (Setting.Sound == 1) {
new Sound(Sound.HINT);
}
offset = line.a.x * Setting.COLUMN + line.a.y;
dots[offset].setBorder(Kyodai.Hint);
offset = line.b.x * Setting.COLUMN + line.b.y;
dots[offset].setBorder(Kyodai.Hint);
score -= Setting.hintScore;
showScore(score - 1, score);
}
else {
Kyodai.showHint("找不到,請刷新");
}
}
/**
* 自動找出并消除地圖上的兩個點
*/
public boolean bomb(Point a, boolean showMessage) {
if (!isPlaying) { //不在游戲中,返回
return false;
}
if (map.getCount() == 0) {
Kyodai.showHint("你炸昏了頭吧,沒了!");
return false;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(false);
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { //找到了可消除的
if (Setting.Sound == 1) {
new Sound(Sound.BOMB);
}
offset = line.a.x * Setting.COLUMN + line.a.y;
dots[offset].setBorder(Kyodai.unSelected);
offset = line.b.x * Setting.COLUMN + line.b.y;
dots[offset].setBorder(Kyodai.unSelected);
map.earse(line.a, line.b);
earse(line.a, line.b);
score -= Setting.bombScore;
showScore(score - 1, score);
return true;
}
else {
if (showMessage) {
Kyodai.showHint("炸彈用不了,請刷新!");
}
return false;
}
}
private void message(String str) {
Kyodai.showHint(str);
}
/**
* 自動游戲
*/
public void autoPlay() {
if (!isPlaying) { //不在游戲中,返回
return;
}
//如果使用該功能,不計時間分
limitTime = 0;
while (map.getCount() > 0) {
if (bomb(new Point( -1, -1), false)) {
message("炸彈使用成功!");
}
else {
message("找不到可用點,刷新……");
refresh();
}
}
}
/**
* 獲取系統的計分板
*/
public void setScore(ScoreAnimate score) {
this.scoreAnimate = score;
}
/**
* 獲取系統的計時板
*/
public void setClock(ClockAnimate clock) {
this.clockAnimate = clock;
}
/**
* 事件處理
*/
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int offset = Integer.parseInt(button.getActionCommand());
int row, col;
row = Math.round(offset / Setting.COLUMN);
col = offset - row * Setting.COLUMN;
//如果上面沒有圖片
if (map.getMap()[row][col] < 1) {
return;
}
//選擇時的聲音
if (Setting.Sound == 1) {
new Sound(Sound.SELECT);
}
if (isSelected) {
message("上次已經選擇了一個點");
message("上次選擇點的坐標為: " + lastPoint.x + ", " + lastPoint.y +
" 值為: " + map.getMap()[lastPoint.x][lastPoint.y] +
" 位移為: " + (lastPoint.x * Setting.COLUMN + lastPoint.y));
//是上次選擇的點
if (lastPoint.x == row && lastPoint.y == col) {
message("這次選擇的點和上次的是同一點,取消選擇狀態");
button.setBorder(Kyodai.unSelected);
isSelected = false;
}
else {
//判斷是否可以消除
message("這次選擇的點和上次的點并不相同");
Point current = new Point(row, col);
message("這次選擇的點的坐標為: " + row + ", " + col + " 值為: " +
map.getMap()[row][col] +
" 位移為: " + (row * Setting.COLUMN + col));
map.setTest(false);
if (map.test(lastPoint, current)) {
message("兩點可以消除,執行消除");
//消除前先取消當前選擇點的邊框,因為有可能是提示
dots[row * Setting.COLUMN + col].setBorder(Kyodai.unSelected);
map.earse(current, lastPoint);
earse(current, lastPoint);
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(Kyodai.unSelected);
lastPoint = new Point(0, 0);
isSelected = false;
showScore(score, score + Setting.correctScore + stepScore);
score += Setting.correctScore + stepScore;
if (Setting.Sound == 1) {
new Sound(Sound.EARSE);
}
}
else {
message("這次選擇的點與上次選擇的點無解,改變選擇為當前點");
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(Kyodai.unSelected);
button.setBorder(Kyodai.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
score -= Setting.wrongScore;
showScore(score - 1, score);
}
}
}
else {
message("上次并未選擇的點,置當前點為選擇點");
message("當前點坐標為: " + row + ", " + col + " 值為: "
+map.getMap()[row][col]
+" 位移為: " + (row * Setting.COLUMN + col));
button.setBorder(Kyodai.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
}
}
/**
* 加密當前的分數
*/
public String encode() {
if (score < 0) {
Kyodai.showHint("負分還想有排名?下次努力吧!");
}
int level, f1, f2;
level = Setting.LevelIndex;
f1 = (score - 102) * (score - 102);
f2 = (1978 - score) * (1978 - score);
return "s=" + score + "&l=" + level + "&f1=" + f1 + "&f2=" + f2;
}
public void start() {
goTop10.setEnabled(false);
isPlaying = true;
limitTime = map.getCount() * Setting.limitScore;
message("時限 = " + limitTime);
paint();
}
public void run() {
}
public void stop() {
clockAnimate.stop();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -