The example of this article describes the Java-written simple mobile block game code. Share to everyone for your reference, specific as follows:
The screenshot of the running effect is as follows:
The first time in Java to write a graphical interface, or a bit sentimental. Continue to work hard later!! The specific code is as follows:
Little Box Game by the Alexyui//game.java by 1093710210@ HIT import javax.swing.*;
Import java.awt.event.*;
Import java.awt.geom.*;
Import java.awt.*;
Import java.util.*;
public class game{public static void Main (string[] agrs) {myframe gameframe = new MyFrame ();
Gameframe.settitle ("Java box Game by Alexyui");
Gameframe.setdefaultcloseoperation (Jframe.exit_on_close);
Gameframe.setvisible (TRUE);
}//Framework class MyFrame extends JFrame {private int flag = 0;
Protected double x[] = {0,0,270,0,270};
Protected double y[] = {0,0,0,270,270};
protected int winner[]={0,0,0,0,0};
public static final int default_width = 317;
public static final int default_height = 339;
Public MyFrame () {Mypanel a = new Mypanel ();
Addkeylistener (New Mykeylistener ());
This.add (a);
SetSize (Default_width,default_height);
Class Mypanel extends jpanel{public mypanel () {} public void Paint (Graphics g) {super.paint (g);
graphics2d g2 = (graphics2d) g; Rectangle2D rect1 = new Rectangle2d.doublE (x[1],y[1],30f,30f);
Rectangle2D rect2 = new rectangle2d.double (x[2],y[2],30f,30f);
Rectangle2D rect3 = new rectangle2d.double (x[3],y[3],30f,30f);
Rectangle2D Rect4 = new rectangle2d.double (x[4],y[4],30f,30f);
Draw the middle of the four box G2.setpaint (color.red);
G2.drawrect (120,120, 30,30);
G2.drawrect (120,150, 30,30);
G2.drawrect (150,120, 30,30);
G2.drawrect (150,150, 30,30);
Fill Four corners of the square g2.setpaint (Color.Blue);
if (winner[1] = = 0) g2.fill (rect1);
if (winner[2] = = 0) g2.fill (RECT2);
if (winner[3] = = 0) g2.fill (RECT3);
if (winner[4] = = 0) g2.fill (RECT4);
G2.setpaint (color.red);
Paint the selected squares as red if (flag = 1) g2.fill (RECT1);
if (flag = = 2) g2.fill (RECT2);
if (flag = = 3) G2.fill (RECT3);
if (flag = = 4) g2.fill (RECT4);
G2.setpaint (Color.Blue);
if (winner[1] = = 1) g2.fill (RECT1);
if (winner[2] = = 1) g2.fill (RECT2);
if (winner[3] = = 1) g2.fill (RECT3);
if (winner[4] = = 1) g2.fill (RECT4);
G2.setpaint (color.red); if (winner[1] = = 1 && winner[2] = = 1 && winner[3] = 1 &&amP WINNER[4] = = 1) g2.drawstring ("succes!
You won! ", 100,75);
Keep the color of the border G2.drawrect (120,120, 30,30);
G2.drawrect (120,150, 30,30);
G2.drawrect (150,120, 30,30);
G2.drawrect (150,150, 30,30);
Repaint (); } class Mykeylistener implements keylistener{//overide public void keypressed (KeyEvent event) {int keycode =
Event.getkeycode ();
if (keycode = = keyevent.vk_1) {flag = 1;}
if (keycode = = keyevent.vk_2) {flag = 2;}
if (keycode = = keyevent.vk_3) {flag = 3;}
if (keycode = = keyevent.vk_4) {flag = 4;}
if (keycode = = keyevent.vk_up) {move (0,-30,flag); win ();}
if (keycode = = Keyevent.vk_down) {move (0,30,flag); win ();}
if (keycode = = Keyevent.vk_left) {move ( -30,0,flag); win ();}
if (keycode = = keyevent.vk_right) {move (30,0,flag); win ();} The public void keyreleased (KeyEvent event) {} is public void keytyped (KeyEvent event) {}}///used to discriminate the base rule, move the box void m
Ove (int mx,int my,int mflag) {int i;
Boolean teller = false;
X[mflag] +=MX; y[Mflag] +=my; if (! ((X[flag] >= 0 && x[flag]<=270) && (Y[flag] >= 0 && y[flag]<=270))
{X[mflag]-=mx;
Y[mflag]-=my; for (i = 1;i<5;i++) {if (Mflag!= i) if (x[mflag] = = X[i] && Y[mflag] = = Y[i]) Teller
= true;
} if (Teller = = True) {X[mflag]-= mx;
Y[mflag] = my;
}///Used to discriminate the Box void win () {int j1,j2 that has entered the qualifying area; for (j1=1;j1<5;j1++) if (x[j1]<=150&&x[j1]>=120) && (y[j1]<=150&&y[j1]>=120
)) Winner[j1]=1; for (j2=1;j2<5;j2++) if (!) (
(x[j2]<=150&&x[j2]>=120) && (y[j2]<=150&&y[j2]>=120)) winner[j2]=0;
}
}
I hope this article will help you with your Java programming.