当前位置:首页 > 代码 > 正文

java连连看源代码下载(java连连看完整源代码)[20240425更新]

admin 发布:2024-04-25 04:06 117


今天给各位分享java连连看源代码下载的知识,其中也会对java连连看完整源代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

连连看JAVA源代码

连连看java源代码 2007-11-01 02:25 import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //分别记录两次被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

public void init(){

mainFrame=new JFrame("JKJ连连看");

thisContainer = mainFrame.getContentPane();

thisContainer.setLayout(new BorderLayout());

centerPanel=new JPanel();

southPanel=new JPanel();

northPanel=new JPanel();

thisContainer.add(centerPanel,"Center");

thisContainer.add(southPanel,"South");

thisContainer.add(northPanel,"North");

centerPanel.setLayout(new GridLayout(6,5));

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));

diamondsButton[cols][rows].addActionListener(this);

centerPanel.add(diamondsButton[cols][rows]);

}

}

exitButton=new JButton("退出");

exitButton.addActionListener(this);

resetButton=new JButton("重列");

resetButton.addActionListener(this);

newlyButton=new JButton("再来一局");

newlyButton.addActionListener(this);

southPanel.add(exitButton);

southPanel.add(resetButton);

southPanel.add(newlyButton);

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));

northPanel.add(fractionLable);

mainFrame.setBounds(280,100,500,450);

mainFrame.setVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins=15;twins++) {

randoms=(int)(Math.random()*25+1);

for(int alike=1;alike=2;alike++) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=randoms;

}

}

}

public void fraction(){

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));

}

public void reload() {

int save[] = new int[30];

int n=0,cols,rows;

int grid[][]= new int[8][7];

for(int i=0;i=6;i++) {

for(int j=0;j=5;j++) {

if(this.grid[i][j]!=0) {

save[n]=this.grid[i][j];

n++;

}

}

}

n=n-1;

this.grid=grid;

while(n=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=save[n];

n--;

}

mainFrame.setVisible(false);

pressInformation=false; //这里一定要将按钮点击信息归为初始

init();

for(int i = 0;i 6;i++){

for(int j = 0;j 5;j++ ){

if(grid[i+1][j+1]==0)

diamondsButton[i][j].setVisible(false);

}

}

}

public void estimateEven(int placeX,int placeY,JButton bz) {

if(pressInformation==false) {

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

pressInformation=true;

}

else {

x0=x;

y0=y;

fristMsg=secondMsg;

firstButton=secondButton;

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

if(fristMsg==secondMsg secondButton!=firstButton){

xiao();

}

}

}

public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻

remove();

}

else{

for (j=0;j7;j++ ) {

if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空

if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0) {

k=0;

break;

}

else{ k=1; } //K=1说明通过了第一次验证

}

if (k==1) {

linePassOne();

}

}

if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0){

k=0;

break;

}

else { k=1; }

}

if (k==1){

linePassOne();

}

}

if (y==j ) {

linePassOne();

}

}

if (k==2) {

if (x0==x) {

remove();

}

if (x0x) {

for (n=x0;n=x-1;n++ ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x-1) {

remove();

}

}

}

if (x0x) {

for (n=x0;n=x+1 ;n-- ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x+1) {

remove();

}

}

}

}

}

for (i=0;i8;i++ ) { //列

if (grid[i][y0]==0) {

if (xi) {

for (j=x-1;j=i ;j-- ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (xi) {

for (j=x+1;j=i;j++ ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (x==i) {

rowPassOne();

}

}

if (k==2){

if (y0==y) {

remove();

}

if (y0y) {

for (n=y0;n=y-1 ;n++ ) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y-1) {

remove();

}

}

}

if (y0y) {

for (n=y0;n=y+1 ;n--) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y+1) {

remove();

}

}

}

}

}

}

}

public void linePassOne(){

if (y0j){ //第一按钮同行空按钮在左边

for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i]!=0) {

k=0;

break;

}

else { k=2; } //K=2说明通过了第二次验证

}

}

if (y0j){ //第一按钮同行空按钮在与第二按钮之间

for (i=y0+1;i=j ;i++){

if (grid[x0][i]!=0) {

k=0;

break;

}

else{ k=2; }

}

}

}

public void rowPassOne(){

if (x0i) {

for (j=x0-1;j=i ;j-- ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

if (x0i) {

for (j=x0+1;j=i ;j++ ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

}

public void remove(){

firstButton.setVisible(false);

secondButton.setVisible(false);

fraction();

pressInformation=false;

k=0;

grid[x0][y0]=0;

grid[x][y]=0;

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==newlyButton){

int grid[][] = new int[8][7];

this.grid = grid;

randomBuild();

mainFrame.setVisible(false);

pressInformation=false;

init();

}

if(e.getSource()==exitButton)

System.exit(0);

if(e.getSource()==resetButton)

reload();

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

if(e.getSource()==diamondsButton[cols][rows])

estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

lianliankan llk = new lianliankan();

llk.randomBuild();

llk.init();

}

}

//old 998 lines

//new 318 lines

求JAVA连连看游戏源代码,打包的,能运行的

123456789101112131415161718192021222324252627282930    import java.io.File;import java.util.Scanner; public class FileTest {     /**     * @param args     */    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        String fileName = sc.next();        File file = new File(fileName);        if (file.exists()) {            if (file.isDirectory()) {                String[] list = file.list();                System.out.println(list.length);                for (String string : list) {                    System.out                            .println(string                                    .substring(string.lastIndexOf(".") = 0 ? string                                            .lastIndexOf(".") : 0));                }            } else {                System.out.println("不是文件夹");            }        } else {            System.out.println("文件不存在");        }    }

求连连看源代码

连连看的代码(基本算法)加了部分注释

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

static String s="no"; //用来纪录点击按钮的信息

int x0=0,y0=0,x=0,y=0,n1=0,n2=0; //用来纪录按钮的位置信息

Frame f,f1;

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10; //用比较笨的方法添加了

Button b11,b12,b13,b14,b15,b16,b17,b18; //30个按钮来实现游戏界面

Button b19,b20,b21,b22,b23,b24,b25; //可以用数组实现,这是本人

Button b26,b27,b28,b29,b30,bc; //学java时,入门的联系,所以

Button b,ba,br,bt1,bt2; //有些东西很业余!!嘻嘻

Panel p1,p2,p3;

TextField t; //用来显示一些随机信息,方法是下面的guli().

Label l;

int d[][]={ //用来和界面的按钮建立映射关系

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

public static void main(String[] args)

{

lianliankan t=new lianliankan();

t.suiji();

t.go();

}

public void actionPerformed(ActionEvent e) //再来一次按钮的响应事件。

{

int d[][]={

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

this.d=d;

suiji();

f.setVisible(false);

f1.setVisible(false);

s="no";

go();

}

public void go()//初始化界面

{

l=new Label("亲爱的玩家,");

f=new Frame("连连看");

t=new TextField();

p2=new Panel();

p1=new Panel();

p3=new Panel();

bc=new Button("退出");

br=new Button("重列");

b=new Button();

b1=new Button(String.valueOf(d[1][1]));

b2=new Button(String.valueOf(d[1][2]));

b3=new Button(String.valueOf(d[1][3]));

b4=new Button(String.valueOf(d[1][4]));

b5=new Button(String.valueOf(d[1][5]));

b6=new Button(String.valueOf(d[2][1]));

b7=new Button(String.valueOf(d[2][2]));

b8=new Button(String.valueOf(d[2][3]));

b9=new Button(String.valueOf(d[2][4]));

b10=new Button(String.valueOf(d[2][5]));

b11=new Button(String.valueOf(d[3][1]));

b12=new Button(String.valueOf(d[3][2]));

b13=new Button(String.valueOf(d[3][3]));

b14=new Button(String.valueOf(d[3][4]));

b15=new Button(String.valueOf(d[3][5]));

b16=new Button(String.valueOf(d[4][1]));

b17=new Button(String.valueOf(d[4][2]));

b18=new Button(String.valueOf(d[4][3]));

b19=new Button(String.valueOf(d[4][4]));

b20=new Button(String.valueOf(d[4][5]));

b21=new Button(String.valueOf(d[5][1]));

b22=new Button(String.valueOf(d[5][2]));

b23=new Button(String.valueOf(d[5][3]));

b24=new Button(String.valueOf(d[5][4]));

b25=new Button(String.valueOf(d[5][5]));

b26=new Button(String.valueOf(d[6][1]));

b27=new Button(String.valueOf(d[6][2]));

b28=new Button(String.valueOf(d[6][3]));

b29=new Button(String.valueOf(d[6][4]));

b30=new Button(String.valueOf(d[6][5]));

p3.setLayout(null);

p1.setSize(250,300);

p2.setSize(100,40);

p3.setSize(300,30);

t.setSize(60,30);

l.setSize(70,30);

p1.setLayout(new GridLayout(6,5));

p1.setBackground(Color.pink);

p1.setLocation(100,100);

p2.setLocation(0,400);

p3.setLocation(50,50);

t.setLocation(230,2);

l.setLocation(150,2);

bc.setLocation(0,40);

br.setLocation(0,100);

f.add(p1);

f.add(p2);

f.add(p3);

p3.add(l);

p3.add(t);

p2.add(bc);

p2.add(br);

p1.add(b1);

p1.add(b2);

p1.add(b3);

p1.add(b4);

p1.add(b5);

p1.add(b6);

p1.add(b7);

p1.add(b8);

p1.add(b9);

p1.add(b10);

p1.add(b11);

p1.add(b12);

p1.add(b13);

p1.add(b14);

p1.add(b15);

p1.add(b16);

p1.add(b17);

p1.add(b18);

p1.add(b19);

p1.add(b20);

p1.add(b21);

p1.add(b22);

p1.add(b23);

p1.add(b24);

p1.add(b25);

p1.add(b26);

p1.add(b27);

p1.add(b28);

p1.add(b29);

p1.add(b30);

f.pack();

f.setBounds(280,100,500,450);

f.setResizable(false);

f.setVisible(true);

bc.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

ex();

}

});

br.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

chonglie();

}

});

b1.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,1,b1);

}

});

b2.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,2,b2);

}

});

b3.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,3,b3);

}

});

b4.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,4,b4);

}

});

b5.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,5,b5);

}

});

b6.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,1,b6);

}

});

b7.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,2,b7);

}

});

b8.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,3,b8);

}

});

b9.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,4,b9);

}

});

b10.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,5,b10);

}

});

b11.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,1,b11);

}

});

b12.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,2,b12);

}

});

b13.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,3,b13);

}

});

b14.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,4,b14);

}

});

b15.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,5,b15);

}

});

b16.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,1,b16);

}

});

b17.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,2,b17);

}

});

b18.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,3,b18);

}

});

b19.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,4,b19);

}

});

b20.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,5,b20);

}

});

b21.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,1,b21);

}

});

b22.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,2,b22);

}

});

b23.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,3,b23);

}

});

b24.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,4,b24);

}

});

b25.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,5,b25);

}

});

b26.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,1,b26);

}

});

b27.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,2,b27);

}

});

b28.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,3,b28);

}

});

b29.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,4,b29);

}

});

b30.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,5,b30);

}

});

}

public void ex() //退出界面,可用diolog来实现有模式的类型,更加符合

{

f1=new Frame("游戏作业");

f1.setLayout(new GridLayout(1,1));

bt1=new Button("确定退出");

bt2=new Button("再来一局");

f1.add(bt1);

f1.add(bt2);

f1.pack();

f1.setBounds(400,250,90,60);

f1.setResizable(false);

f1.show();

f1.setVisible(true);

bt1.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

System.exit(0);

}

});

bt2.addActionListener(this);

}

public void suiji() //产生随机数,来填充游戏界面对应的数组的各个位置

{

int m,n,k=0,k1,k2,k3;

for(m=1;m=15;m++)

{

k1=(int)(Math.random()*25+1);

for(n=1;n=2;n++)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

while(d[k2][k3]!=0 k!=30)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

}

this.d[k2][k3]=k1;

k++;

}

}

}

public void guli() //随机信息

{

int l=0;

t.setText("");

l=(int)(Math.random()*10);

System.out.println(l);

switch(l)

{

case 1:

t.setText("好!加油!");

break;

case 3:

t.setText("你真棒!");

break;

case 5:

t.setText("加快速度!");

break;

case 6:

t.setText("不错啊!");

break;

case 8:

t.setText("加油吧!");

break;

case 9:

t.setText("够聪明!");

break;

default:

break;

}

}

public void chonglie() //重列方法

{

int save[],i,j,n=0,k2,k3,k;

int d[][]={

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

save=new int[30];

for(n=0;n30;n++)

save[n]=0; //定义一个数组来保存当前的每个按钮位置上的信息

n=0;

for(i=0;i=6;i++)

for(j=0;j=5;j++)

{

if(this.d[i][j]!=0)

{

save[n]=this.d[i][j];

n++;

}

}

n=n-1;

this.d=d;

while(n=0) //产生随机位置,放置按钮

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

while(d[k2][k3]!=0)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

}

this.d[k2][k3]=save[n];

n--;

}

f.setVisible(false);

s="no"; //这里一定要将按钮点击信息归为初始

go();

ling();

}

public void ling() //将数组中为零的成员对应的按钮消去

{ //用按钮类型的数组实现会简化得多,

if(d[1][1]==0)

b1.setVisible(false);

if(d[1][2]==0)

b2.setVisible(false);

if(d[1][3]==0)

b3.setVisible(false);

if(d[1][4]==0)

b4.setVisible(false);

if(d[1][5]==0)

b5.setVisible(false);

if(d[2][1]==0)

b6.setVisible(false);

if(d[2][2]==0)

b7.setVisible(false);

if(d[2][3]==0)

b8.setVisible(false);

if(d[2][4]==0)

b9.setVisible(false);

if(d[2][5]==0)

b10.setVisible(false);

if(d[3][1]==0)

b11.setVisible(false);

if(d[3][2]==0)

b12.setVisible(false);

if(d[3][3]==0)

b13.setVisible(false);

if(d[3][4]==0)

b14.setVisible(false);

if(d[3][5]==0)

b15.setVisible(false);

if(d[4][1]==0)

b16.setVisible(false);

if(d[4][2]==0)

b17.setVisible(false);

if(d[4][3]==0)

b18.setVisible(false);

if(d[4][4]==0)

b19.setVisible(false);

if(d[4][5]==0)

b20.setVisible(false);

if(d[5][1]==0)

b21.setVisible(false);

if(d[5][2]==0)

b22.setVisible(false);

if(d[5][3]==0)

b23.setVisible(false);

if(d[5][4]==0)

b24.setVisible(false);

if(d[5][5]==0)

b25.setVisible(false);

if(d[6][1]==0)

b26.setVisible(false);

if(d[6][2]==0)

b27.setVisible(false);

if(d[6][3]==0)

b28.setVisible(false);

if(d[6][4]==0)

b29.setVisible(false);

if(d[6][5]==0)

b30.setVisible(false);

}

public void wei(int w1,int w2,Button bz) //判断并纪录每次点击按钮的信息

{ //当两次的按钮相同才能消去

if((s.trim()).equals("no"))

{

s=b1.getLabel();

x0=w1;

y0=w2;

n1=d[x0][y0];

b=bz;

x=w1;

y=w2;

n2=d[x][y];

ba=bz;

}

else

{

x0=x;

y0=y;

n1=d[x0][y0];

b=ba;

x=w1;

y=w2;

n2=d[x][y];

ba=bz;

if(n1==n2 ba!=b)

{

xiao();

}

}

}

public void xiao() //这里是整个游戏最重要的部分,就是判断两个按钮在信息

{ //相同的情况下能不能消去。仔细分析,不一条条注释

int i=0, j=0,n=0,k=0;

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))) //相邻的情况

{

ba.setVisible(false);

b.setVisible(false);

guli();

s="no";

d[x0][y0]=0;

d[x][y]=0;

}

else

{

for (j=0;j7;j++ ) //两个按钮按行分析,看能否消去

{

if (d[x0][j]==0)

{

if (yj)

{

for (i=y-1;i=j;i-- )

{

if (d[x][i]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (yj)

{

for (i=y+1;i=j ;i++ )

{

if (d[x][i]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (y==j )

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (k==2)

{ if (x0==x)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

if (x0x)

{

for (n=x0;n=x-1;n++ )

{

if (d[n][j]!=0)

{

k=0;

break;

}

if(d[n][j]==0 n==x-1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

if (x0x)

{

for (n=x0;n=x+1 ;n-- )

{

if (d[n][j]!=0)

{

k=0;

break;

}

if(d[n][j]==0 n==x+1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

}

}

for (i=0;i8;i++ ) //按列分析,看能不能消去

{

if (d[i][y0]==0)

{

if (xi)

{

for (j=x-1;j=i ;j-- )

{

if (d[j][y]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (xi)

{

for (j=x+1;j=i;j++ )

{

if (d[j][y]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i ;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (x==i)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i ;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (k==2)

{

if (y0==y)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

if (y0y)

{

for (n=y0;n=y-1 ;n++ )

{

if (d[i][n]!=0)

{

k=0;

break;

}

if(d[i][n]==0 n==y-1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

if (y0y)

{

for (n=y0;n=y+1 ;n--)

{

if (d[i][n]!=0)

{

k=0;

break;

}

if(d[i][n]==0 n==y+1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

}

}

}

}

}

连连看JAVA源代码是什么?

importjavax.swing.*;\x0d\x0aimportjava.awt.*;\x0d\x0aimportjava.awt.event.*;\x0d\x0apublicclasslianliankanimplementsActionListener\x0d\x0a{\x0d\x0aJFramemainFrame;//主面板\x0d\x0aContainerthisContainer;\x0d\x0aJPanelcenterPanel,southPanel,northPanel;//子面板\x0d\x0aJButtondiamondsButton[][]=newJButton[6][5];//游戏按钮数组\x0d\x0aJButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮\x0d\x0aJLabelfractionLable=newJLabel("0");//分数标签\x0d\x0aJButtonfirstButton,secondButton;//分别记录两次被选中的按钮\x0d\x0aintgrid[][]=newint[8][7];//储存游戏按钮位置\x0d\x0astaticbooleanpressInformation=false;//判断是否有按钮被选中\x0d\x0aintx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标\x0d\x0ainti,j,k,n;//消除方法控制\x0d\x0apublicvoidinit(){\x0d\x0amainFrame=newJFrame("JKJ连连看");\x0d\x0athisContainer=mainFrame.getContentPane();\x0d\x0athisContainer.setLayout(newBorderLayout());\x0d\x0acenterPanel=newJPanel();\x0d\x0asouthPanel=newJPanel();\x0d\x0anorthPanel=newJPanel();\x0d\x0athisContainer.add(centerPanel,"Center");\x0d\x0athisContainer.add(southPanel,"South");\x0d\x0athisContainer.add(northPanel,"North");\x0d\x0acenterPanel.setLayout(newGridLayout(6,5));\x0d\x0afor(intcols=0;cols=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0awhile(grid[cols][rows]!=0){\x0d\x0acols=(int)(Math.random()*6+1);\x0d\x0arows=(int)(Math.random()*5+1);\x0d\x0a}\x0d\x0athis.grid[cols][rows]=save[n];\x0d\x0an--;\x0d\x0a}\x0d\x0amainFrame.setVisible(false);\x0d\x0apressInformation=false;//这里一定要将按钮点击信息归为初始\x0d\x0ainit();\x0d\x0afor(inti=0;ij){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边\x0d\x0afor(i=y-1;i=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮\x0d\x0aif(grid[x][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=1说明通过了第一次验证\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0alinePassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(yx){\x0d\x0afor(n=x0;n=x+1;n--){\x0d\x0aif(grid[n][j]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[n][j]==0n==x+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0afor(i=0;ii){\x0d\x0afor(j=x-1;j=i;j--){\x0d\x0aif(grid[j][y]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0aif(k==1){\x0d\x0arowPassOne();\x0d\x0a}\x0d\x0a}\x0d\x0aif(xy){\x0d\x0afor(n=y0;n=y+1;n--){\x0d\x0aif(grid[i][n]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aif(grid[i][n]==0n==y+1){\x0d\x0aremove();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0apublicvoidlinePassOne(){\x0d\x0aif(y0j){//第一按钮同行空按钮在左边\x0d\x0afor(i=y0-1;i=j;i--){//判断第一按钮同左侧空按钮之间有没按钮\x0d\x0aif(grid[x0][i]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse//K=2说明通过了第二次验证\x0d\x0a}\x0d\x0a}\x0d\x0aif(y0i){\x0d\x0afor(j=x0-1;j=i;j--){\x0d\x0aif(grid[j][y0]!=0){\x0d\x0ak=0;\x0d\x0abreak;\x0d\x0a}\x0d\x0aelse\x0d\x0a}\x0d\x0a}\x0d\x0aif(x0

回答于 2022-12-14

如何用JAVA 编写一个连连看游戏全程设计

刚试了。。测试通过。。

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

publicclass LianLianKan implements ActionListener {

JFrame mainFrame; // 主面板

Container thisContainer;

JPanel centerPanel, southPanel, northPanel; //子面板

JButton diamondsButton[][] = newJButton[6][5];// 游戏按钮数组

JButton exitButton, resetButton, newlyButton;// 退出,重列,重新开始按钮

JLabel fractionLable = newJLabel("0"); // 分数标签

JButton firstButton, secondButton; // 分别记录两次被选中的按钮

// 储存游戏按钮位置(这里其实只要6行,5列。但是我们用了8行,7列。是等于在这个面板按钮的周围还围

//了一层是0的按钮,这样就可以实现靠近面板边缘的两个按钮可以消去)

int grid[][] = new int[8][7];

static boolean pressInformation = false; // 判断是否有按钮被选中

int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg =0, secondMsg = 0, validateLV; // 游戏按钮的位置坐标

int i, j, k, n;// 消除方法控制

public void init() {

mainFrame = new JFrame("JKJ连连看");

thisContainer = mainFrame.getContentPane();

thisContainer.setLayout(new BorderLayout());

centerPanel = new JPanel();

southPanel = new JPanel();

northPanel = new JPanel();

thisContainer.add(centerPanel,"Center");

thisContainer.add(southPanel,"South");

thisContainer.add(northPanel,"North");

centerPanel.setLayout(new GridLayout(6, 5));

for (int cols = 0; cols 6; cols++) {

for (int rows = 0; rows 5; rows++) {

diamondsButton[cols][rows] = newJButton(String

.valueOf(grid[cols + 1][rows + 1]));

diamondsButton[cols][rows].addActionListener(this);

centerPanel.add(diamondsButton[cols][rows]);

}

}

exitButton = new JButton("退出");

exitButton.addActionListener(this);

resetButton = new JButton("重列");

resetButton.addActionListener(this);

newlyButton = new JButton("再来一局");

newlyButton.addActionListener(this);

southPanel.add(exitButton);

southPanel.add(resetButton);

southPanel.add(newlyButton);

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable

.getText())));

northPanel.add(fractionLable);

mainFrame.setBounds(280, 100, 500, 450);

mainFrame.setVisible(true);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void randomBuild() {

int randoms, cols, rows;

for (int twins = 1; twins = 15; twins++){//一共15对button,30个

randoms = (int) (Math.random() * 25 +1);//button上的数字

for (int alike = 1; alike = 2; alike++){

cols = (int) (Math.random() * 6 + 1);

rows = (int) (Math.random() * 5 + 1);

while (grid[cols][rows] != 0) {//等于0说明这个空格有了button

cols = (int) (Math.random() * 6 + 1);

rows = (int) (Math.random() * 5 + 1);

}

this.grid[cols][rows] = randoms;

}

}

}

public void fraction() {

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable

.getText()) + 100));

}

public void reload() {

int save[] = new int[30];

int n = 0, cols, rows;

int grid[][] = new int[8][7];

for (int i = 0; i = 6; i++) {

for (int j = 0; j = 5; j++) {

if (this.grid[i][j] != 0) {

save[n] = this.grid[i][j];//记下每个button的数字

n++;//有几个没有消去的button

}

}

}

n = n - 1;

this.grid = grid;

while (n = 0) {//把没有消去的button重新放一次

cols = (int) (Math.random() * 6 + 1);

rows = (int) (Math.random() * 5 + 1);

while (grid[cols][rows] != 0) {

cols = (int) (Math.random() * 6 + 1);

rows = (int) (Math.random() * 5 + 1);

}

this.grid[cols][rows] = save[n];

n--;

}

mainFrame.setVisible(false);

pressInformation = false; // 这里一定要将按钮点击信息归为初始

init();

for (int i = 0; i 6; i++) {

for (int j = 0; j 5; j++) {

if (grid[i + 1][j + 1] == 0)

diamondsButton[i][j].setVisible(false);

}

}

}

public void estimateEven(int placeX, intplaceY, JButton bz) {

if (pressInformation == false) {

x = placeX;

y = placeY;

secondMsg = grid[x][y];

secondButton = bz;

pressInformation = true;

} else {

x0 = x;

y0 = y;

fristMsg = secondMsg;

firstButton = secondButton;

x = placeX;

y = placeY;

secondMsg = grid[x][y];

secondButton = bz;

if (fristMsg == secondMsg secondButton != firstButton) {

xiao();

}

}

}

public void xiao() { // 相同的情况下能不能消去。仔细分析,不一条条注释

if ((x0 == x (y0 == y + 1 || y0 ==y - 1))

|| ((x0 == x + 1 || x0 == x - 1) (y0 == y))) { // 判断是否相邻

remove();

} else {

for (j = 0; j 7; j++) {

if (grid[x0][j] == 0) { // 判断和第一个按钮同行的哪个按钮为空

//如果找到一个为空的,就按列值的三种情况比较第二个按钮与空按钮的位置

if (y j) {//第二个按钮在空按钮右边

for (i = y - 1; i = j; i--) { //检测从第二个按钮横向左边到空格所在列为止是否全是空格

if (grid[x][i] != 0) {

k = 0;

break;//存在非空格的就退出,这一退出就不可能k==2了,所以就会到下而215行出同理的判断列

} else {

k = 1;

} // K=1说明全是空格通过了第一次验证,也就是从第二个按钮横向左边到空格所在列为止全是空格

}

if (k == 1) {

linePassOne();//进入第二次验证,也就是从第一个按钮到它同行的空格之间的空格判断

}

}

if (y j) { // 第二个按钮在空按钮左边

for (i = y + 1; i = j; i++) {//检测从第二个按钮横向右边到空格所在列为止是否全是空格

if (grid[x][i] != 0) {

k = 0;

break;

} else {

k = 1;

}

}

if (k == 1) {

linePassOne();

}

}

if (y == j) {//第二个按钮和空按钮同列

linePassOne();

}

}

//第三次检测,检测确定为空的第j列的那个按钮竖向到第二个按钮,看是不是有按钮

if (k == 2) {

if (x0 == x) {//第一,二按钮在同行

remove();

}

if (x0 x) {//第一按钮在第二按钮下边

for (n = x0; n = x - 1; n++) {//从空按钮竖向到第二个按钮所在行是否有按钮

if (grid[n][j] != 0) {

k= 0;

break;

}

//没有按钮,说明这条路经就通了

if (grid[n][j] == 0 n == x -1) {

remove();

}

}

}

if (x0 x) {//第一按钮在第二按钮上边

for (n = x0; n = x + 1; n--) {

if (grid[n][j] != 0) {

k = 0;

break;

}

if (grid[n][j] == 0 n == x +1) {

remove();

}

}

}

}

}//-------------------------------------for

//当上面的检测与第一个按钮同行的空格按钮失败后(不能找到与第二个按钮的相连路经),下面就执行

//检测与第一个按钮同列的空格按钮

for (i = 0; i 8; i++) {

if (grid[i][y0] == 0) {// 判断和第一个按钮同列的哪个按钮为空

if (x i) {//第二个按钮在这个空按钮的下面

for (j = x - 1; j = i; j--) {

if (grid[j][y] != 0) {

k = 0;

break;

} else {

k = 1;

}

}

if (k == 1) {

rowPassOne();

}

}

if (x i) {//第二个按钮在这个空按钮的上面

for (j = x + 1; j = i; j++) {

if (grid[j][y] != 0) {

k = 0;

break;

} else {

k = 1;

}

}

if (k == 1) {

rowPassOne();

}

}

if (x == i) {//第二个按钮与这个空按钮同行

rowPassOne();

}

}

if (k == 2) {

if (y0 == y) {//第二个按钮与第一个按钮同列

remove();

}

if (y0 y) {//第二个按钮在第一个按钮右边

for (n = y0; n = y - 1; n++) {

if (grid[i][n] != 0) {

k = 0;

break;

}

if (grid[i][n] == 0 n == y -1) {

remove();

}

}

}

if (y0 y) {//第二个按钮在第一个按钮左边

for (n = y0; n = y + 1; n--) {

if (grid[i][n] != 0) {

k = 0;

break;

}

if (grid[i][n] == 0 n == y +1) {

remove();

}

}

}

}

}//--------------------------------for

}//-------------else

}//------------xiao

public void linePassOne() {

if (y0 j) { // 第一按钮同行空按钮在左边

for (i = y0 - 1; i = j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i] != 0) {

k = 0;

break;

} else {

k = 2;

} // K=2说明通过了第二次验证

}

}

if (y0 j) { // 第一按钮同行空按钮在右边

for (i = y0 + 1; i = j; i++) {

if (grid[x0][i] != 0) {

k = 0;

break;

} else {

k = 2;

}

}

}

}

public void rowPassOne() {

if (x0 i) {//第一个按钮在与它同列的那个空格按钮下面

for (j = x0 - 1; j = i; j--) {

if (grid[j][y0] != 0) {

k = 0;

break;

} else {

k = 2;

}

}

}

if (x0 i) {//第一个按钮在与它同列的那个空格按钮上面

for (j = x0 + 1; j = i; j++) {

if (grid[j][y0] != 0) {

k = 0;

break;

} else {

k = 2;

}

}

}

}

public void remove() {

firstButton.setVisible(false);

secondButton.setVisible(false);

fraction();

pressInformation = false;

k = 0;

grid[x0][y0] = 0;

grid[x][y] = 0;

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == newlyButton) {

int grid[][] = new int[8][7];

this.grid = grid;

randomBuild();

mainFrame.setVisible(false);

pressInformation = false;

init();

}

if (e.getSource() == exitButton)

System.exit(0);

if (e.getSource() == resetButton)

reload();

for (int cols = 0; cols 6; cols++) {

for (int rows = 0; rows 5; rows++) {

if (e.getSource() ==diamondsButton[cols][rows])

estimateEven(cols + 1, rows + 1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

LianLianKan llk = new LianLianKan();

llk.randomBuild();

llk.init();

}

}

求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); }}

关于java连连看源代码下载和java连连看完整源代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/510.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载