Perfect imitation of the 2048 game, is based on a netizen's 2048 change.
Block.java
Import javax.swing.*;
Import java.awt.*;
public class Block extends JLabel {private int value; The public block () {value = 0;//initialization value is 0 setfont (new font ("Font", Font.plain, 40);/Set Font SetBackground (color.gr
AY)//Set initial color to gray} public int getValue ()//Get value {return value;
public void SetValue (int value) {this.value = value;
String Text = string.valueof (value);
if (value!= 0) setText (text);
else SetText ("");//If the value is 0, SetColor () is not displayed; SetColor ()///////////////////////////////////////////
Or.gray);
Break
Case 2:setbackground (New Color (238, 228, 218));
Break
Case 4:setbackground (New Color (238, 224, 198));
Break
Case 8:setbackground (New Color (243, 177, 116));
Break
Case 16:setbackground (New Color (243, 177, 116));
Break
Case 32:setbackground (New Color (248, 149, 90)); Break
Case 64:setbackground (New Color (249, 94, 50));
Break
Case 128:setbackground (New Color (239, 207, 108));
Break
Case 256:setbackground (New Color (239, 207, 99));
Break
Case 512:setbackground (New Color (239, 203, 82));
Break
Case 1024:setbackground (New Color (239, 199, 57));
Break
Case 2048:setbackground (New Color (239, 195, 41));
Break
Case 4096:setbackground (New Color (255, 60, 57));
Break }
}
}
My2048.java
Import java.awt.*;
Import javax.swing.*;
public class My2048 extends JFrame
{public
My2048 ()//constructor
{
settitle ("2048");//Set
title SetSize ( 400, 400);/Set window size
setlocation (500, 200);//Set window start position
setdefaultcloseoperation (jframe.exit_on_close);
Getcontentpane (). setlayout (New GridLayout (4, 4, 5, 5));//Set layout mode is GridLayout type
new Operation (this);
This.setvisible (TRUE);//Set to Visual
} public
static void Main (String args[])//Program entry point
{
try
{
Uimanager.setlookandfeel ("Org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");//Set UI
} Accept thrown exception
catch (ClassNotFoundException | instantiationexception| illegalaccessexception | Unsupportedlookandfeelexception e)
{
e.printstacktrace ();
}
Jframe.setdefaultlookandfeeldecorated (TRUE);//sets the default appearance of the frame
new My2048 ();
}
Operation.java
Import java.awt.event.*;
Import javax.swing.*;
The public class Operation implements KeyListener {block[] block;//is used to store 16 data JPanel panel;
public Boolean up,down,left,right; The number of times the int moveflag;//is used for cumulative movement Boolean numflag;//is used to determine whether a new digital public Operation (JFrame frame) {This.panel = (JPanel
) Frame.getcontentpane ()//construct the panel block = new block[16];//construct an array with a length of 16 Numflag = true;//Initialize moveflag = 0;
Up=true;down=true;left=true;right=true;
Addblock ();
for (int i = 0; i < 2; i++) Appearblock ();
Frame.addkeylistener (this); private void Addblock () {for (int i = 0; i < i++)//Add block {block[i to panel = new Block ()
;
Block[i].sethorizontalalignment (jlabel.center);/Opaque label block[i].setopaque (true);
Panel.add (Block[i]); } public void Appearblock () {while (Numflag)//When it is also possible to add a random new worth of time {int index = (int) (Math.random) ( ) * 16)//Take a random integer of 0 to 15, which is used as a random addition to the position of 2 or 4 in the disk if (block[Index].getvalue () = 0)//If the value of the block array in which the number is 0, that is, when empty, add a 2 or 4 number {if (Math.random () < 0.5) {
Block[index].setvalue (2);
else {block[index].setvalue (4);
break;//out of While}} The public void Judgeappear ()//statistical block array contains elements with a value of 0, and if not, the Numflag becomes false {
int sum = 0;
for (int i = 0; i < i++) {if (Block[i].getvalue ()!= 0) {sum++;
} if (sum = = Numflag = false; public int find (int i,int j,int A,int b) {while (i<b&&i>=a) {if Block[i].getvalue ()!
=0) {return i;
} i=i+j;
} return-1;
public void Upblock () {int i=0,j=0;int t=0;int valuej=0;int valuei=0;int index=0;
for (i=0;i<4;i++) {index=i;
for (j=i+4;j<16;j+=4) {valuej=0; valuei=0; if (Block[index].getvalue () ==0) {T=find (index,4,0,16);
if (t!=-1) {Block[index].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuei=block[index].getvalue ();
if (Block[j].getvalue () ==0) {t=find (j,4,0,16);
if (t!=-1) {Block[j].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuej=block[j].getvalue ();
if (valuei==valuej&&valuei!=0&&valuej!=0) {block[index].setvalue (Valuei+valuej);
Block[j].setvalue (0);
Numflag = true;
} index=j;
}} public void Downblock () {int i=0,j=0;int t=0;int valuej=0;int valuei=0;int index=0;
for (i=12;i<16;i++) {index=i; for (j=i-4;j>=0;j-=4) {valuej=0; valuei=0;
if (Block[index].getvalue () ==0) {t=find (index,-4,0,16);
if (t!=-1) {Block[index].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuei=block[index].getvalue ();
if (Block[j].getvalue () ==0) {t=find (j,-4,0,16);
if (t!=-1) {Block[j].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuej=block[j].getvalue ();
if (valuei==valuej&&valuei!=0&&valuej!=0) {block[index].setvalue (Valuei+valuej);
Block[j].setvalue (0);
Numflag = true;
} index=j;
}} public void Rightblock () {int i=0,j=0;int t=0;int valuej=0;int valuei=0;int index=0;
for (i=3;i<16;i+=4){index=i;
for (j=i-1;j>i-4;j--) {valuej=0; valuei=0;
if (Block[index].getvalue () ==0) {t=find (index,-1,i-3,index+1);
if (t!=-1) {Block[index].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuei=block[index].getvalue ();
if (Block[j].getvalue () ==0) {t=find (j,-1,i-3,j+1);
if (t!=-1) {Block[j].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuej=block[j].getvalue ();
if (valuei==valuej&&valuei!=0&&valuej!=0) {block[index].setvalue (Valuei+valuej);
Block[j].setvalue (0);
Numflag = true;
} index=j; }} public void Leftblock () {intI=0,j=0;int t=0;int valuej=0;int valuei=0;int index=0;
for (i=0;i<16;i+=4) {index=i;
for (j=i+1;j<i+4;j++) {valuej=0; valuei=0;
if (Block[index].getvalue () ==0) {t=find (index,1,index,i+4);
if (t!=-1) {Block[index].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuei=block[index].getvalue ();
if (Block[j].getvalue () ==0) {t=find (j,1,j,i+4);
if (t!=-1) {Block[j].setvalue (Block[t].getvalue ());
Block[t].setvalue (0);
} else {break;
} valuej=block[j].getvalue ();
if (valuei==valuej&&valuei!=0&&valuej!=0) {block[index].setvalue (Valuei+valuej);
Block[j].setvalue (0);
Numflag = true;
} Index=j; }} public void over () {if (Numflag ==false&& up==false&&down==false&&le
Ft==false&&right==false///When the element cannot be added, and the number of steps that cannot be moved exceeds 36, the loss is lost and the Gameover {block[4].settext ("G") is shown in the center of the disk when the loss is lost;
Block[5].settext ("A");
Block[6].settext ("M");
Block[7].settext ("E");
Block[8].settext ("O");
Block[9].settext ("V");
Block[10].settext ("E");
Block[11].settext ("R");
Block[11].addmouselistener (New Mouseadapter () {public void mousepressed (MouseEvent e) {restart ();}});
} public void Win ()///same over {Block[0].settext ("Y");
Block[1].settext ("O");
Block[2].settext ("U");
Block[13].settext ("W");
Block[14].settext ("I");
Block[15].settext ("N");
Block[15].addmouselistener (New Mouseadapter () {public void mousepressed (MouseEvent e) {restart ();
}
});
public void restart ()//restart game, similar to constructor, not numflag=true; moveflag=0;
Up=true;down=true;left=true;right=true;
for (int i=0;i<16;i++) block[i].setvalue (0);
for (int i = 0; i < 2; i++) Appearblock (); The public void keypressed (KeyEvent e)//judgment is pressed up and down key, and then calls the move function, the judgment function, adds the function, the judgment whether loses the function {switch (E.getkeycode ()) {CAs
e KeyEvent.VK_UP:if (UP) {Upblock ();}
Judgeappear ();
Appearblock ();
Over ();
if (numflag==false) {up=false;
else {up=true;down=true;left=true;right=true;
} break;
Case KeyEvent.VK_DOWN:if (down) {Downblock ();}
Judgeappear ();
Appearblock ();
Over ();
if (numflag==false) {down=false;
else {up=true;down=true;left=true;right=true;
} break;
Case KeyEvent.VK_LEFT:if (left) {Leftblock ();}
Judgeappear ();
Appearblock ();
Over ();
if (numflag==false) {left=false;
} else {up=true;down=true;left=true;right=true;
} break;
Case KeyEvent.VK_RIGHT:if (right) {Rightblock ();}
Judgeappear ();
Appearblock ();
Over ();
if (numflag==false) {right=false;
else {up=true;down=true;left=true;right=true;
} break; keytyped (KeyEvent e) {} public void keyreleased (KeyEvent e) {}}}
The above is described in this article to share about the Java perfect implementation of the 2048 small game of all the code, I hope to learn Java can help.