
正文
java九宫格小游戏代码,java九宫格小游戏代码大全
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
九宫格拼图·求此问题解法~~思路~代码都可~~就是关于其还原算法的·急~在线等~多谢哈
在一个3×3的九宫中有1-8这8个数及一个空格随机的摆放在其中的格子里,如图1-1所示。现在要求实现这个问题:将其调整为如图1-1右图所示的形式。调整的规则是:每次只能将与空格(上、下、或左、右)相邻的一个数字平移到空格中。试编程实现这一问题的求解。
(图1-1)
二、题目分析:
这是人工智能中的经典难题之一,问题是在3×3方格棋盘中,放8格数,剩下的没有放到的为空,每次移动只能是和相邻的空格交换数。程序自动产生问题的初始状态,通过一系列交换动作将其转换成目标排列(如下图1-2到图1-3的转换)。
(图1-2) (图1-3)
该问题中,程序产生的随机排列转换成目标共有两种可能,而且这两种不可能同时成立,也就是奇数排列和偶数排列。可以把一个随机排列的数组从左到右从上到下用一个一维数组表示,如上图1-2我们就可以表示成{8,7,1,5,2,6,3,4,0}其中0代表空格。
在这个数组中我们首先计算它能够重排列出来的结果,公式就是:
∑(F(X))=Y,其中F(X)
是一个数前面比这个数小的数的个数,Y为奇数和偶数时各有一种解法。(八数码问题是否有解的判定 )
上面的数组可以解出它的结果。
F(8)=0;
F(7)=0;
F(1)=0;
F(5)=1;
F(2)=1;
F(6)=3;
F(3)=2;
F(4)=3;
Y=0+0+0+1+1+3+2+3=10
Y=10是偶数,所以其重排列就是如图1-3的结果,如果加起来的结果是奇数重排的结果就是如图1-1最右边的排法。
三、算法分析
求解方法就是交换空格(0)位置,直至到达目标位置为止。图形表示就是:
(图3-1)
要想得到最优的就需要使用广度优先搜索,九宫的所以排列有9!种,也就是362880种排法,数据量是非常大的,使用广度搜索,需要记住每一个结点的排列形式,要是用数组记录的话会占用很多的内存,可以把数据进行适当的压缩。使用DWORD形式保存,压缩形式是每个数字用3位表示,这样就是3×9=27个字节,由于8的二进制表示形式1000,不能用3位表示,使用了一个小技巧就是将8表示为000,然后用多出来的5个字表示8所在的位置,就可以用DWORD表示了。用移位和或操作将数据逐个移入,比乘法速度要快点。定义了几个结果来存储遍历到了结果和搜索完成后保存最优路径。
类结构如下:
class CNineGird
{
public:
struct PlaceList
{
DWORD Place;
PlaceList* Left;
PlaceList* Right;
};
struct Scanbuf
{
DWORD Place;
int ScanID;
};
struct PathList
{
unsigned char Path[9];
};
private:
PlaceList *m_pPlaceList;
Scanbuf *m_pScanbuf;
RECT m_rResetButton;
RECT m_rAutoButton;
public:
int m_iPathsize;
clock_t m_iTime;
UINT m_iStepCount;
unsigned char m_iTargetChess[9];
unsigned char m_iChess[9];
HWND m_hClientWin;
PathList *m_pPathList;
bool m_bAutoRun;
private:
inline bool AddTree(DWORD place , PlaceList* parent);
void FreeTree(PlaceList* parent);
inline void ArrayToDword(unsigned char *array , DWORD data);
inline void DwordToArray(DWORD data , unsigned char *array);
inline bool MoveChess(unsigned char *array , int way);
bool EstimateUncoil(unsigned char *array);
void GetPath(UINT depth);
public:
void MoveChess(int way);
bool ComputeFeel();
void ActiveShaw(HWND hView);
void DrawGird(HDC hDC , RECT clientrect);
void DrawChess(HDC hDC , RECT clientrect);
void Reset();
void OnButton(POINT pnt , HWND hView);
public:
CNineGird();
~CNineGird();
};
计算随机随机数组使用了vector模板用random_shuffle(,)函数来打乱数组数据,并计算目标结果是什么。代码:
void CNineGird::Reset()
{
if(m_bAutoRun) return;
vector vs;
int i;
for (i = 1 ; i 9 ; i ++)
vs.push_back(i);
vs.push_back(0);
random_shuffle(vs.begin(), vs.end());
random_shuffle(vs.begin(), vs.end());
for ( i = 0 ; i 9 ; i ++)
{
m_iChess[i] = vs[i];
}
if (!EstimateUncoil(m_iChess))
{
unsigned char array[9] = {1,2,3,8,0,4,7,6,5};
memcpy(m_iTargetChess , array , 9);
}
else
{
unsigned char array[9] = {1,2,3,4,5,6,7,8,0};
memcpy(m_iTargetChess , array , 9);
}
m_iStepCount = 0;
}
数据压缩函数实现:
inline void CNineGird::ArrayToDword(unsigned char *array , DWORD data)
{
unsigned char night = 0;
for ( int i = 0 ; i 9 ; i ++)
{
if (array[i] == 8)
{
night = (unsigned char)i;
break;
}
}
array[night] = 0;
data = 0;
data = (DWORD)((DWORD)array[0] 29 | (DWORD)array[1] 26 |
(DWORD)array[2] 23 | (DWORD)array[3] 20 |
(DWORD)array[4] 17 | (DWORD)array[5] 14 |
(DWORD)array[6] 11 | (DWORD)array[7] 8 |
(DWORD)array[8] 5 | night);
array[night] = 8;
}
解压缩时跟压缩正好相反,解压代码:
inline void CNineGird::DwordToArray(DWORD data , unsigned char *array)
{
unsigned char chtem;
for ( int i = 0 ; i 9 ; i ++)
{
chtem = (unsigned char)(data (32 - (i + 1) * 3) 0x00000007);
array[i] = chtem;
}
chtem = (unsigned char)(data 0x0000001F);
array[chtem] = 8;
}
由于可扩展的数据量非常的大,加上在保存的时候使用的是DWORD类型,将每一步数据都记录在一个排序二叉树中,按从小到大从左到有的排列,搜索的时候跟每次搜索将近万次的形式比较快几乎是N次方倍,把几个在循环中用到的函数声明为内联函数,并在插入的时候同时搜索插入的数据会不会在树中有重复来加快总体速度。二叉树插入代码:
inline bool CNineGird::AddTree(DWORD place , PlaceList* parent)
{
if (parent == NULL)
{
parent = new PlaceList();
parent-Left = parent-Right = NULL;
parent-Place = place;
return true;
}
if (parent-Place == place)
return false;
if (parent-Place place)
{
return AddTree(place , parent-Right);
}
return AddTree(place , parent-Left);
}
计算结果是奇数排列还是偶数排列的代码:
bool CNineGird::EstimateUncoil(unsigned char *array)
{
int sun = 0;
for ( int i = 0 ; i 8 ; i ++)
{
for ( int j = 0 ; j 9 ; j ++)
{
if (array[j] != 0)
{
if (array[j] == i +1 )
break;
if (array[j] i + 1)
sun++;
}
}
}
if (sun % 2 == 0)
return true;
else
return false;
}
移动到空格位的代码比较简单,只要计算是否会移动到框外面就可以了,并在移动的时候顺便计算一下是不是已经是目标结果,这是用来给用户手工移动是给与提示用的,代码:
inline bool CNineGird::MoveChess(unsigned char *array , int way)
{
int zero , chang;
bool moveok = false;
for ( zero = 0 ; zero 9 ; zero ++)
{
if (array[zero] == 0)
break;
}
POINT pnt;
pnt.x = zero % 3;
pnt.y = int(zero / 3);
switch(way)
{
case 0 : //up
if (pnt.y + 1 3)
{
chang = (pnt.y + 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 1 : //down
if (pnt.y - 1 -1)
{
chang = (pnt.y - 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 2 : //left
if (pnt.x + 1 3)
{
chang = pnt.y * 3 + pnt.x + 1;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 3 : //right
if (pnt.x - 1 -1)
{
chang = pnt.y * 3 + pnt.x - 1;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
}
if (moveok !m_bAutoRun)
{
m_iStepCount ++ ;
DWORD temp1 ,temp2;
ArrayToDword(array , temp1);
ArrayToDword(m_iTargetChess , temp2);
if (temp1 == temp2)
{
MessageBox(NULL , "你真聪明这么快就搞定了!" , "^_^" , 0);
}
}
return moveok;
}
在进行广度搜索时候,将父结点所在的数组索引记录在子结点中了,所以得到目标排列的时候,只要从子结点逆向搜索就可以得到最优搜索路径了。用变量m_iPathsize来记录总步数,具体函数代码:
void CNineGird::GetPath(UINT depth)
{
int now = 0 , maxpos = 100 ;
UINT parentid;
if (m_pPathList != NULL)
{
delete[] m_pPathList;
}
m_pPathList = new PathList[maxpos];
parentid = m_pScanbuf[depth].ScanID;
DwordToArray(m_pScanbuf[depth].Place , m_pPathList[++now].Path);
while(parentid != -1)
{
if (now == maxpos)
{
maxpos += 10;
PathList * temlist = new PathList[maxpos];
memcpy(temlist , m_pPathList , sizeof(PathList) * (maxpos - 10));
delete[] m_pPathList;
m_pPathList = temlist;
}
DwordToArray(m_pScanbuf[parentid].Place , m_pPathList[++now].Path);
parentid = m_pScanbuf[parentid].ScanID;
}
m_iPathsize = now;
}
动态排列的演示函数最简单了,为了让主窗体有及时刷新的机会,启动了一个线程在需要主窗体刷新的时候,用Slee(UINT)函数来暂停一下线程就可以了。代码:
unsigned __stdcall MoveChessThread(LPVOID pParam)
{
CNineGird * pGird = (CNineGird *)pParam;
RECT rect;
pGird-m_iStepCount = 0;
::GetClientRect(pGird-m_hClientWin , rect);
for ( int i = pGird-m_iPathsize ; i 0 ; i --)
{
memcpy(pGird-m_iChess , pGird-m_pPathList[i].Path , 9);
pGird-m_iStepCount ++;
InvalidateRect( pGird-m_hClientWin , rect , false);
Sleep(300);
}
char msg[100];
sprintf(msg , "^_^ ! 搞定了!\r\n计算步骤用时%d毫秒" , pGird-m_iTime);
MessageBox(NULL , msg , "~_~" , 0);
pGird-m_bAutoRun = false;
return 0L;
}
最后介绍一下搜索函数的原理,首先得到源数组,将其转换成DWORD型,与目标比较,如果相同完成,不同就交换一下数据和空格位置,加入二叉树,搜索下一个结果,直到没有步可走了,在搜索刚刚搜索到的位置的子位置,这样直到找到目标结果为止,函数:
bool CNineGird::ComputeFeel()
{
unsigned char *array = m_iChess;
UINT i;
const int MAXSIZE = 362880;
unsigned char temparray[9];
DWORD target , fountain , parent , parentID = 0 , child = 1;
ArrayToDword(m_iTargetChess , target);
ArrayToDword(array , fountain);
if (fountain == target)
{
return false;
}
if (m_pScanbuf != NULL)
{
delete[] m_pScanbuf;
}
m_pScanbuf = new Scanbuf[MAXSIZE];
AddTree(fountain ,m_pPlaceList);
m_pScanbuf[ 0 ].Place = fountain;
m_pScanbuf[ 0 ].ScanID = -1;
clock_t tim = clock();
while(parentID MAXSIZE child MAXSIZE)
{
parent = m_pScanbuf[parentID].Place;
for ( i = 0 ; i 4 ; i ++) // 0 :UP , 1:Down ,2:Left,3:Right
{
DwordToArray(parent , temparray);
if (MoveChess(temparray,i)) //是否移动成功
{
ArrayToDword(temparray , fountain);
if (AddTree(fountain, m_pPlaceList)) //加入搜索数
{
m_pScanbuf[ child ].Place = fountain;
m_pScanbuf[ child ].ScanID = parentID;
if (fountain == target) //是否找到结果
{
m_iTime = clock() - tim;
GetPath(child);//计算路径
FreeTree(m_pPlaceList);
delete[] m_pScanbuf;
m_pScanbuf = NULL;
return true;
}
child ++;
}
}
} // for i
parentID++;
}
m_iTime = clock() - tim;
FreeTree(m_pPlaceList);
delete[] m_pScanbuf;
m_pScanbuf = NULL;
return false;
}
重要函数的介绍结束;下面是程序的运行结果和运算结果:
相关问答
Q1: java 输出九宫格
将你其中某些问题的答案放在代码注释中了.
这个程序输出的是固定的九宫格,我想,是根据固有的九宫格中的数字与数组下标的关系来写的代码。
希望对你有所帮助,加油!
class S{
public static void main(String[] args) {
int arr[][] = new int[3][3];
//创建一个三阶方阵
int a = 2;
//第3行的行下标
//???这里是什么意思,2从何而来
//A:java中数组的下标从0开始
int b = 3/2;
//第2列的列下标
//???同上
//A:这里由于b=1,(int)/(int),java中数组的下标从0开始
for(int i=1;i=9;i++){
//给数组赋值
arr[a++][b++] = i;
if(i%3==0){
//如果i是3的倍数——————???为什么要判断是不是3的倍数
a = a-2;
//————————————————???if...else里面的语句是什么意思,作用是什么
b = b-1;//————————————————???同上
}
//使a,b回到起点:a=2,b=1;
else{
//如果i不是3的倍数//————————————————???同上
a = a%3;
b = b%3;
}
}
//????九宫格的每一行、每一列、对角线都等于15,
//???但是这里连一个15这个数字都没有出现,但还是成功输出
//————————————————————???他是怎么做到的
System.out.println("输出九宫格:");
//遍历输出九宫格
for(int i=0;i3;i++){
for(int j=0;j3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");//从你的程序中将此语句上移到此位置
}
}
}
Q2: java编程题,在九宫格内填入1—9九个数字,使得横竖排的数字相加之和都相等
/*直接复制运行就可以,每一行的九个数字代表一个九宫格的9个数字,从左到右,从上到下*/
import java.util.ArrayList;
import java.util.Arrays;
public class Test1 {
private static ArrayListString arrangeList = new ArrayListString();
public static void main(String[] args) {
String str = "123456789";//你要排列组合的字符串
char list[] = str.toCharArray();//将字符串转换为字符数组
genernateData(list, 0, list.length - 1);//参数为字符数组和0和字符数组最大下标
int arr[]=new int[9];
for(String str1 : arrangeList){
for(int k=0;k9;k++){
arr[k]=Integer.parseInt(str1.substring(k,k+1));
}
if(arr[0]+arr[1]+arr[2]==15arr[3]+arr[4]+arr[5]==15arr[6]+arr[7]+arr[8]==15arr[0]+arr[3]+arr[6]==15arr[1]+arr[4]+arr[7]==15arr[2]+arr[5]+arr[8]==15arr[0]+arr[4]+arr[8]==15arr[2]+arr[4]+arr[6]==15){
System.out.println(Arrays.toString(arr));
}
}
}
public static void genernateData(char list[], int k, int m) {
if (k m) {
StringBuffer sb = new StringBuffer();//创建一个StringBuffer对象sb
for (int i = 0; i = m; i++) {
sb.append(list[i]);//循环将字符数组值追加到StringBuffer中
}
arrangeList.add(sb.toString());
} else {
for (int i = k; i = m; i++) {
swapData(list, k, i);//将下表为k和i的值调换位置
genernateData(list, k + 1, m);
swapData(list, k, i);
}
}
}
private static void swapData(char list[], int k, int i) {
char temp = list[k];
list[k] = list[i];
list[i] = temp;
}
}
Q3: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
Q4: JAVA小游戏程序代码
这个是比较有名的那个烟花,不知道你有没有用:
建个工程,以Fireworks为类即可
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Fireworks extends Applet implements MouseListener,Runnable
{
int x,y;
int top,point;
/**
*对小程序进行变量和颜色的初始化。
*/
public void init()
{
x = 0;
y = 0;
//设置背景色为黑色
setBackground(Color.black);
addMouseListener(this);
}
public void paint(Graphics g)
{
}
/**
*使该程序可以作为应用程序运行。
*/
public static void main(String args[]) {
Fireworks applet = new Fireworks();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(800,400);
applet.init();
applet.start();
frame.setVisible(true);
}
/**
*程序主线程,对一个烟花进行绘制。
*/
public void run()
{
//变量初始化
Graphics g1;
g1 = getGraphics();
int y_move,y_click,x_click;
int v;
x_click = x;
y_click = y;
y_move = 400;
v = 3;
int r,g,b;
while(y_move y_click)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move,5,5);
y_move -= 5;
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move,5,5);
for(int j = 0 ;j=10;j++)
{
if(r55) r -= 20;
if(g55) g -= 20;
if(b55) b -=20;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move+j*5,5,5);
}
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+5*10,5,5);
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
}
for(int j=12;j=0;j--)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+(j*5),5,5);
try
{
Thread.currentThread().sleep((v++)/3);
} catch (InterruptedException e) {}
}
y_move = 400;
g1.setColor(Color.black);
while(y_move y_click)
{
g1.fillOval(x_click-2,y_move,9,5);
y_move -= 5;
}
v = 15;
for(int i=0;i=25;i++)
{
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
if(i23)
{
g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1));
g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2));
}
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
g1.setColor(Color.black);
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
}
}
/**
*对鼠标事件进行监听。
*临听其鼠标按下事件。
*当按下鼠标时,产生一个新线程。
*/
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
Thread one;
one = new Thread(this);
one.start();
one = null;
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseReleased(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseEntered(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseExited(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseClicked(MouseEvent e)
{
}
}







