⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 centerjpanel.java

📁 五子连珠游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import javax.swing.*;

import java.io.*;

class centerJPanel extends JPanel implements MouseListener {
	/*该类为面板中间部分,处理球点击事件,路径的判断,球的交换,消球,两侧图像的改变,
	 * 显示菜单Show statistics,当前分数的变化
	 * 该类的布局为setLayout(new GridLayout(9,9)),每一个格子都是类jform。
	 */
	//
	int startx,starty,endx,endy;
	jform formpicture[][];            //用于方格贴图像的类             
	int form[][]=new int[9][9];      //帮助数组,用来判断该地方是否有小球
	int key=0;                        //用来判断点击事件,说不清楚,具体看函数
	int loctionx,loctiony,color;     //分别记录随机产生的三个数(坐标和颜色
	int visit[][]=new int[9][9];    //用于判断路径
	int colorAll[]=new int[8],colorCleared[]=new int[8];  //小球的剩余数和消去数
	jform next[];                  //用来显示中间的next的三个小球
	JPanel text;
	int scoreNow=0;                 //当前分数
	int scoreBefore;                //之前的最高分数
	String scorenow;                 
	
	JTextField textField2;
	//JPanel text2Jpanel;
   
    boolean menukey=true;
    JFrame showwindow;
    jform colorShow[]=new jform[8];
    showScores showscores;
    boolean showkey;
    CheckboxMenuItem item;
    int ballAll=0;
    nameScores name[]=new nameScores[5];
	

	
	void deal(int i,int color)         //类数组next的处理函数,用来改变其图像
	{
		switch(color)
		{
			case 0:
				next[i].con.setIcon(new ImageIcon("1.gif"));					
				break;
			case 1:
				next[i].con.setIcon(new ImageIcon("2.gif"));
				break;
			case 2:
				next[i].con.setIcon(new ImageIcon("3.gif"));
				break;
			case 3:
				next[i].con.setIcon(new ImageIcon("4.gif"));
				break;
			case 4:
				next[i].con.setIcon(new ImageIcon("5.gif"));
				break;
			case 5:
				next[i].con.setIcon(new ImageIcon("6.gif"));
				break;
			case 6:
				next[i].con.setIcon(new ImageIcon("7.gif"));
				break;
			default :
				next[i].con.setIcon(new ImageIcon("blank.gif"));
			break;
			
		}	
	}
	
	void deal(int x,int y,int color)            //用来改变类数组formpicture的图像,主要是在
	                                              //球交换事件,消球事件
	{
		switch(color)
		{
			case 0:
				formpicture[x][y].con.setIcon(new ImageIcon("1.gif"));					
				break;
			case 1:
				formpicture[x][y].con.setIcon(new ImageIcon("2.gif"));
				break;
			case 2:
				formpicture[x][y].con.setIcon(new ImageIcon("3.gif"));
				break;
			case 3:
				formpicture[x][y].con.setIcon(new ImageIcon("4.gif"));
				break;
			case 4:
				formpicture[x][y].con.setIcon(new ImageIcon("5.gif"));
				break;
			case 5:
				formpicture[x][y].con.setIcon(new ImageIcon("6.gif"));
				break;
			case 6:
				formpicture[x][y].con.setIcon(new ImageIcon("7.gif"));
				break;
			default :
				formpicture[x][y].con.setIcon(new ImageIcon("blank.gif"));
				break;
			
		}								
	}
	
	void change(int x,int y)                 //处理球点击事件
	{
		switch(formpicture[x][y].getcolor())
		{
			case 0:
				formpicture[x][y].con.setIcon(new ImageIcon("d1.gif"));					
				break;
			case 1:
				formpicture[x][y].con.setIcon(new ImageIcon("d2.gif"));
				break;
			case 2:
				formpicture[x][y].con.setIcon(new ImageIcon("d3.gif"));
				break;
			case 3:
				formpicture[x][y].con.setIcon(new ImageIcon("d4.gif"));
				break;
			case 4:
				formpicture[x][y].con.setIcon(new ImageIcon("d5.gif"));
				break;
			case 5:
				formpicture[x][y].con.setIcon(new ImageIcon("d6.gif"));
				break;
			case 6:
				formpicture[x][y].con.setIcon(new ImageIcon("d7.gif"));
				break;
			default :break;
			
		}								
	}
	
	
	//响应主类中菜单new,初始化各项数值
	public void initJPanel()
	{
		key=0;
		for(int i=0;i<8;i++)             //各个颜色的球的剩余数,消去数的初始化
		{
			colorAll[i]=0;
			colorCleared[i]=0;
		}
		showscores.alter(colorAll,colorCleared);//showscores是响应主类中菜单Show statistics,此时为
		                                        //初始化此项数值
		showscores.show(showkey);               //是否出现Show statistics,初始为false
		scoreNow=0;                             //初始化当前分数
		scorenow=""+scoreNow;
		textField2.setText(scorenow);           //初始化当前分数的显示
		beforePicture.setIcon(new ImageIcon("r-1.gif"));//初始化两侧的图像
		nowPicture.setIcon(new ImageIcon("s-2.gif"));
		for(int i=0;i<9;i++)                            //初始化帮助数组
	    {
		     for(int j=0;j<9;j++)
			     form[i][j]=0;
	    }
		for(int i=0;i<3;i++)                                 //初始化类数组next 
		{
			
			generate();
			next[i].setjform(loctionx,loctiony,color);
			deal(i,color);    
		}
		for(int i=0;i<9;i++)                                 //初始化81个类jform
			for(int j=0;j<9;j++)
			{
				formpicture[i][j].con.setIcon(new ImageIcon("blank.gif"));
			}
		for(int i=0;i<5;i++)                                 //随机产生5个小球
		{
			generate();
			deal(loctionx,loctiony,color);
			colorAll[color]++;
			form[loctionx][loctiony]=1;
			formpicture[loctionx][loctiony].alterColor(color);
			
		}        
		
	}

	
	//构造函数  
	public centerJPanel(JTextField text2,JLabel east,JLabel west,int scoreHigh,nameScores playerName[])
	{
		/* text2用来改变当前分数,east,west用来改变两侧图像,scoreHigh为之前的最高分数,
		 * playerName[]用来保存分数排行榜
		 * 
		 */
		for(int i=0;i<5;i++)      
		{
			name[i]=playerName[i];
		}
		for(int i=0;i<8;i++)   //初始化各个颜色的球的剩余数,消去数,colorAll为剩余数colorCleared位消去数
		{
			colorAll[i]=0;
			colorCleared[i]=0;
		}
		scoreBefore=scoreHigh;
		beforePicture=east;
		nowPicture=west;
		textField2=text2;                        //现在分数
		textField2.setEditable(false);           //分数不可改变
		textField2.setColumns(6);
		next=new jform[3];                        //类数组next
		for(int i=0;i<3;i++)                      //随机产生3个小球,用于显示在主类的next
		{		
			generate();
			next[i]=new jform(loctionx,loctiony);
			next[i].alterColor(color);
			deal(i,color);
		}
		formpicture=new jform[9][9];                //初始化类formpicture
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
				form[i][j]=0;
		}
		setBackground(new Color(106,168,254));
		setLayout(new GridLayout(9,9));	
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
			{
				//form[i][j]=0;
				formpicture[i][j]=new jform(i,j);
				formpicture[i][j].addMouseListener(this);
				add(formpicture[i][j]);
			}
		for(int i=0;i<5;i++)
		{
			generate();
			ballAll++;
			colorAll[color]++;
			form[loctionx][loctiony]=1;
			deal(loctionx,loctiony,color);
			formpicture[loctionx][loctiony].alterColor(color);
			
		}
		showscores=new showScores(colorAll,colorCleared);
		//System.out.println("wewo");
	}
	public boolean lookForStart()//0代表已找到,1代表不通。
	{
		boolean canpass = false;int i,j;
		for(i=0;i<9;i++)
			for(j=0;j<9;j++)
				visit[i][j]=form[i][j];
		visit[startx][starty] = 10;// 用10来表示能达到的棋盘
		for (int a = 0; (a < 81) && (canpass != true); a++)//若循环81次后仍 canpass != true,说明不能到达
		{
			for (i = 0; i < 9; i++)
				for (j = 0; j < 9; j++) {
					if (visit[i][j] == 10) //先找到能到达的棋盘
					{
						if ((i != 0) && (visit[i - 1][j] == 0))//注意不使path[][]越界
							visit[i - 1][j] = 10;//path[i - 1][j]=0说明能到达path[i - 1][j]
						if ((i != 8) && (visit[i + 1][j] == 0))//注意不使path[][]越界
							visit[i + 1][j] = 10;
						if ((j != 0) && (visit[i][j - 1] == 0))//注意不使path[][]越界
							visit[i][j - 1] = 10;
						if ((j != 8) && (visit[i][j + 1] == 0))//注意不使path[][]越界
							visit[i][j + 1] = 10;
						if (visit[endx][endy] == 10) 
						{
							canpass = true;// 标记能到达目的地
							break;//canpass = true则退出循环
						}
					}
				}
		} // 判断路径结束
		return canpass;
	}
	void generate()
	{
		while(true)
		{
			this.loctionx=(int)(Math.floor(Math.random()*9));
			this.loctiony=(int)(Math.floor(Math.random()*9));
			if(form[this.loctionx][loctiony]==0)
			{
				this.color=(int)(Math.floor(Math.random()*7));
				//colorAll[color]++;
				break;
			}
		}
		
	}
	
	
	//鼠标点击处理函数
	public void mouseClicked(MouseEvent event){}
	public void mouseEntered(MouseEvent event){}
	public void mouseExited(MouseEvent event){}
	public void mousePressed(MouseEvent event){
		//int color;
		jform second=(jform)event.getSource();
		if(form[second.getx()][second.gety()]==1)
		{
			//System.out.println("key="+key);
			if(key==1)
			{
				deal(startx,starty,formpicture[startx][starty].getcolor());
				startx=second.getx();
			    starty=second.gety();
			    color=second.getcolor();
			    //System.out.println("start"+color);
				//System.out.println("  "+color);
				change(startx,starty);	
			}
			else 
			{	
				startx=second.getx();
				starty=second.gety();
				color=second.getcolor();
				//System.out.println(color+" ");
				change(startx,starty);
				//System.out.println("startkey0"+color);
				key=1;
			}
		}
		else if(key==1&&form[second.getx()][second.gety()]==0)
		{
		    endx=second.getx();
		    endy=second.gety();
		    //System.out.println(startx+" "+starty+endx+" "+endy+" ");
		    if(lookForStart())
		    {
		    	//System.out.println(endx);
		    	key=0;
		    	form[startx][starty]=0;
		    	form[endx][endy]=1;
		    	deal(startx,starty,7);   		
		    	color=formpicture[startx][starty].getcolor();
		    	//System.out.println("start"+color);
		    	formpicture[startx][starty].alterColor(7);
		    	//System.out.println("startkey1"+color);
		    	//System.out.println("key==1,true,color="+color);
		    	deal(endx,endy,color);
		    	//System.out.println("startkey1"+color);
		    	formpicture[endx][endy].setjform(endx,endy,color);	
		    	clearUp(endx,endy);
		    	//System.out.println("endcolor "+formpicture[endx][endy].getcolor());
		    	for(int i=0;i<3;i++)
		    	{
		    		loctionx=next[i].getx();
		    		loctiony=next[i].gety();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -