package qqlink;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.Timer;
public class LinkPanel extends JPanel implements Data
{
public LinkPanel(JFrame frame)
{
this.frame = frame;
setBackground(Color.WHITE);
timer = new Timer(1000, new TimerAction());
startButton = new JButton("开始");
add(startButton);
startButton.addActionListener(new StartAction());
pauseButton = new JButton("暂停");
add(pauseButton);
pauseButton.addActionListener(new PauseAction());
//提示下一步
clewButton = new JButton("提示");
add(clewButton);
clewButton.addActionListener(new ClewNextAction());
recordButton = new JButton("排行榜");
add(recordButton);
recordButton.addActionListener(new RecordAction());
aboutButton = new JButton("关于");
add(aboutButton);
aboutButton.addActionListener(new AboutAction());
map = new int[MAXX][MAXY];
inflexionList = new ArrayList();
addMouseListener(new MouseClickListener());
addMouseMotionListener(new MouseMoveListener());
init();
}
public void init()
{
initMap();
times = GRADETIMES1;
allTimesInGameGrade = GRADETIMES1;
score = 0;
gameState = INIT_STATE;
gameGrade = 1;
}
public void initMap()
{
int index = 0;
int[] random = ToolClass.imageIDArray();
for(int i = 1; i < MAXX - 1; i++)
{
for(int j = 1; j < MAXY - 1; j++)
{
map[i][j] = random[index++];
}
}
for(int i = 0; i < MAXX; i++)
{
map[i][0] = 0;
map[i][MAXY - 1] = 0;
}
for(int i = 0; i < MAXY; i++)
{
map[0][i] = 0;
map[MAXX - 1][i] = 0;
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(gameState == INIT_STATE)
{
drawStartText(g2);
}
if(gameState == PAUSE_STATE)
{
drawPauseText(g2);
}
if(gameState != RUN_STATE)
{
return;
}
drawTimeRect(g2);
drawScoreAndGrade(g2);
for(int i = 0; i < MAXX; i++)
{
for(int j = 0; j < MAXY; j++)
{
Picture.draw(g, i, j, map[i][j]);
}
}
drawMousePointerRectangele(g2);
drawLastAndCurrentPointRectangle(g2);
g2.setStroke(new BasicStroke(5));
for(int i = 0; isDrawClewLineFlag && i + 1 < inflexionList.size(); i++)
{
Point a = (Point)inflexionList.get(i);
Point b = (Point)inflexionList.get(i + 1);
drawLine(g2, Color.RED, a.x, a.y, b.x, b.y);
}
g2.setStroke(new BasicStroke(1));
isDrawClewLineFlag = false;
}
private void drawLastAndCurrentPointRectangle(Graphics2D g2)
{
if(lastPoint != null)
{
Point p = ToolClass.getActualCoordinate(lastPoint);
Rectangle2D rect = new Rectangle2D.Double(p.x, p.y, LEN, LEN);
g2.setPaint(Color.RED);
g2.draw(rect);
}
if(currentPoint != null)
{
Point p = ToolClass.getActualCoordinate(currentPoint);
Rectangle2D rect = new Rectangle2D.Double(p.x, p.y, LEN, LEN);
g2.setPaint(Color.RED);
g2.draw(rect);
}
}
private void drawMousePointerRectangele(Graphics2D g2)
{
if(currentMovePoint != null)
{
Rectangle2D rect = new Rectangle2D.Double(currentMovePoint.x, currentMovePoint.y, LEN, LEN);
Rectangle2D rect2 = new Rectangle2D.Double(currentMovePoint.x - 2, currentMovePoint.y - 2, LEN + 4, LEN + 4);
g2.setPaint(Color.GREEN);
g2.draw(rect);
g2.draw(rect2);
}
}
public void drawBackgroundLine(Graphics2D g2)
{
Rectangle2D frame = new Rectangle2D.Double(LEFT, UP, LEN * MAXX, LEN * MAXY);
g2.draw(frame);
for(int row = UP + LEN; row < DOWN; row = row + LEN)
{
g2.draw(new Line2D.Double(new Point(LEFT, row), new Point(RIGHT, row)));
}
for(int column = LEFT + LEN; column < RIGHT; column = column + LEN)
{
g2.draw(new Line2D.Double(new Point(column, UP), new Point(column, DOWN)));
}
}
public void drawLine(Graphics2D g2, Color color, int x1, int y1, int x2, int y2)
{
Point a = new Point(x1, y1);
Point b = new Point(x2, y2);
a = ToolClass.getActualCoordinate(a);
b = ToolClass.getActualCoordinate(b);
a.x = a.x + LEN / 2;
a.y = a.y + LEN / 2;
b.x = b.x + LEN / 2;
b.y = b.y + LEN / 2;
Line2D line = new Line2D.Double(a, b);
g2.setPaint(Color.RED);
g2.draw(line);
}
public void drawTimeRect(Graphics2D g2)
{
Rectangle2D.Double bkRect = new Rectangle2D.Double(TIME_RECT_X, TIME_RECT_Y,
TIME_RECT_WIDTH, TIME_RECT_HEIGHT);
g2.draw(bkRect);
int width = times * (TIME_RECT_WIDTH - 1) / allTimesInGameGrade;
width = Math.min(width, TIME_RECT_WIDTH);
Rectangle2D.Double moveRect = new Rectangle2D.Double(TIME_RECT_X + 1, TIME_RECT_Y + 1,
width, TIME_RECT_HEIGHT - 1);
g2.setPaint(Color.ORANGE);
g2.fill(moveRect);
}
public void drawScoreAndGrade(Graphics2D g2)
{
g2.setPaint(Color.BLUE);
g2.drawString("级别:" + gameGrade, 600, 65);
g2.drawString("成绩: " + score, 650, 65);
}
public void drawStartText(Graphics2D g2)
{
g2.setPaint(Color.BLUE);
g2.drawString("请按开始按钮开始游戏!", 300, 200);
}
public void drawPauseText(Graphics2D g2)
{
g2.setPaint(Color.BLUE);
g2.drawString("请按开始按钮,继续游戏!", 300, 200);
}
//判断两个点之间是否有直线连接
private boolean hasBeelineLink(Point a, Point b)
{
boolean flag = false;
Point equalsPoint = null;
int ax, ay, bx, by;
ax = a.x;
ay = a.y;
bx = b.x;
by = b.y;
//如果两个点是同一个点时候 或者 两个点的横坐标和纵坐标都不相同,则退出
if(a.equals(b) || (ax != bx && ay != by))
{
return flag;
}
//如果两点的横坐标相同
if(ax == bx)
{
//判断两个点是否相邻
if(ay == by + 1 || ay == by - 1)
{
flag = true;
}
else
{
Point maxPoint = null;
Point minPoint = null;
if(ay > by)
{
maxPoint = a;
minPoint = b;
}
else if(ay < by)
{
maxPoint = b;
minPoint = a;
}
Point[] array1 = getYUpAxesNullPoint(maxPoint);
Point[] array2 = getYDownAxesNullPoint(minPoint);
flag = null != getEqualsPoint(array1, array2);
}
}
//如果两点的纵坐标相同
else if(ay == by)
{
//判断两个点是否相邻
if(ax == bx + 1 || ax == bx - 1)
{
flag = true;
}
else
{
Point maxPoint = null;
Point minPoint = null;
if(ax > bx)
{
maxPoint = a;
minPoint = b;
}
else if(ax < bx)
{
maxPoint = b;
minPoint = a;
}
Point[] array1 = getXLeftAxesNullPoint(maxPoint);
Point[] array2 = getXRightAxesNullPoint(minPoint);
flag = null != getEqualsPoint(array1, array2);
}
}
if(flag)
{
inflexionList.clear();
inflexionList.add(a);
inflexionList.add(b);
}
return flag;
}
//判断两个点之间是否有一个拐点连接
private boolean hasOneInflexionLink(Point a, Point b)
{
boolean flag = false;
Point equalsPoint = null;
if(a.equals(b))
{
return false;
}
Point[] arrayAX = getXAxesNullPoint(a);
Point[] arrayBY = getYAxesNullPoint(b);
if(!flag)
{
equalsPoint = getEqualsPoint(arrayAX, arrayBY);
flag = null != equalsPoint;
}
Point[] arrayAY = getYAxesNullPoint(a);
Point[] arrayBX = getXAxesNullPoint(b);
if(!flag)
{
equalsPoint = getEqualsPoint(arrayBX, arrayAY);
flag = null != equalsPoint;
}
if(flag)
{
inflexionList.clear();
inflexionList.add(a);
inflexionList.add(equalsPoint);
inflexionList.add(b);
}
return flag;
}
//找出两个数组中相同的一个元素,如果存在多个,就返回任意相同的一个
//如果都不相同返回null
private Point getEqualsPoint(Point[] array1, Point[] array2)
{
Point r
java课程设计--连连看
需积分: 9 126 浏览量
2008-01-12
19:51:12
上传
评论
收藏 82KB RAR 举报
高山流水
- 粉丝: 5
- 资源: 16
最新资源
- [咒术回战][19][黑闪][中文][1080P].mp4
- 随机森林RF葡萄酒分类,随机森林二分类(代码完整,数据齐全)
- 香蕉新鲜度等级识别香蕉成熟度识别分类数据集1186张3类别.7z
- [咒术回战][18][贤者][中文][1080P].mp4
- 香蕉成熟度识别分类数据集13478张4类别.7z
- 安卓Android图书馆座位占座app设计可导入Studio源码案例设计.zip
- 安卓Android汽车票售票订票app设计可导入Studio源码案例设计.zip
- 安卓android高校开放式实验室管理app可导入Studio源码案例设计.zip
- 安卓Android车辆运输信息管理App可升级Studio源码案例设计.zip
- Android Studio移动应用开发.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈