Implementation Effect Diagram main function used to start a program main form class contains message area time zone game Area gaming area Canvas Sudoku cell number selection box
To implement the effect diagram:
The main function is used to start the program:
Package Hlc.shudu.app;
Import Hlc.shudu.src.ShuduHelper;
Import Hlc.shudu.ui.ShuduMainFrame;
public class AppStart {public
static void Main (string[] args) {
shudumainframe mainFrame = new Shudumainframe (); C7/>mainframe.setvisible (True);
}
main Form Class (includes message area, time zone, game area):
Package hlc.shudu.ui;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.sql.Time;
Import Java.text.SimpleDateFormat;
Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
Import Javax.swing.JTextField;
Import Javax.swing.Timer;
Import Javax.swing.border.TitledBorder; /* Sudoku main Form/public class Shudumainframe extends JFrame {public static int pass = 1;//level public static JLab El Lbpass; lable public static Long Usedtime = 0 showing the checkpoint; Player when private shuducanvers panelcanvers;
Main game Area public static Timer usertimeaction;
/* Default constructor */Public Shudumainframe () {//initialization method init ();
Add component AddComponent ();
Add main game Area addcanvers ();
} * * Add main game Area * * private void Addcanvers () {panelcanvers = new shuducanvers ();
Panelcanvers.setborder (New Titledborder ("Game area"));
Add the main game area to the form This.add (Panelcanvers, Borderlayout.center);
* * * Add component area * * private void AddComponent () {JPanel panelcomponent = new JPanel ();
Add Message area addpanelmsg (panelcomponent);
Add Time zone Addpaneltime (panelcomponent);
Add components to the top of the form This.add (Panelcomponent, Borderlayout.north);
} private void Addpaneltime (JPanel panelcomponent) {JPanel paneltime = new JPanel ();
Paneltime.setborder (New Titledborder ("Time"));
Paneltime.setlayout (New GridLayout (2, 1));
Final JLabel lbsystime = new JLabel ();
Final JLabel lbusertime = new JLabel ();
Paneltime.add (Lbsystime, Borderlayout.north);
Paneltime.add (Lbusertime, Borderlayout.south); Set up the system time Timer timer systimeaction = new Timer (500,New ActionListener () {@Override public void actionperformed (ActionEvent e) {lon
G Timemillis = System.currenttimemillis ();
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Lbsystime.settext ("System time:" + Df.format (timemillis));
}
});
Systimeaction.start (); Usertimeaction = new Timer (1000, new ActionListener () {@Override public void actionperformed (ACT
Ionevent e) {lbusertime.settext ("You have:" + (++usedtime) + "sec.");});
Usertimeaction.start ();
Panelcomponent.add (Paneltime, borderlayout.east); * * * Add message area */private void addpanelmsg (JPanel panelcomponent) {//Panelcomponent.setborder (n
EW Titledborder ("message area"));
Panelcomponent.setlayout (New GridLayout (1, 3));
Font font14 = new Font ("", 4, 14); Font font28 = new Font ("", 2, 28);
JPanel panelmsg = new JPanel ();
Panelmsg.setborder (New Titledborder ("message area"));
JLabel LbPass1 = new JLabel ("Checkpoint: section");
Lbpass1.setfont (font14);
Panelmsg.add (LBPASS1);
Show level number Lbpass = new JLabel ("" + Pass);
Lbpass.setforeground (color.red);
Lbpass.setfont (FONT28);
Panelmsg.add (Lbpass);
JLabel lbPass2 = new JLabel ("Off/total 10 off");
Lbpass2.setfont (font14);
Panelmsg.add (LBPASS2);
Panelcomponent.add (panelmsg, Borderlayout.center);
* * * Interface initialization/private void init () {ImageIcon image = new ImageIcon ("Icon/icon.png");
This.seticonimage (Image.getimage ());
Sets the initial size of the window this.setsize (515, 600);
Sets the initial position of the window this.setlocation (500, 50);
Sets the window title This.settitle ("Sudoku Game by: Houlong");
Setting the form does not allow changes in size this.setresizable (false); Set the default shutdown action This.setdefaultCloseoperation (Jframe.exit_on_close);
}
}
Game Area canvas:
Package hlc.shudu.ui;
Import Hlc.shudu.src.ShuduHelper;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.Dialog.ModalExclusionType;
Import java.awt.event.InputEvent;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Javax.swing.JDialog;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Javax.swing.border.Border;
Import Javax.swing.border.TitledBorder;
public class Shuducanvers extends JPanel implements MouseListener {shuducell[][] cells;
Get Sudoku Array int[][] maps = new INT[9][9];
Private Selectnumframe Selectnum;
/* Default constructor */Public Shuducanvers () {shudumainframe.usedtime = 0;
Maps = Shuduhelper.getmap ();
Load Sudoku region this.setlayout (NULL);
Cells = new Shuducell[9][9];
for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J +) {//This.remove (Cells[i][j]); Create cell Cells[i][j] = new Shuducell (); Set position cells[i][j].setlocation (M + i * + (I/3) * 5 + J * + (J/3) *
5);
if (Passrole (Shudumainframe.pass)) {Cells[i][j].settext ("" + maps[i][j]);
Sets the background color Cells[i][j].setbackground (GetColor (Maps[i][j]));
Cells[i][j].setenabled (FALSE);
Cells[i][j].setforeground (Color.gray);
else {Cells[i][j].addmouselistener (this);
} this.add (Cells[i][j]);
} checkfinish ();
Reloadcanvers (); * * * Check whether the * * is complete/private void Checkfinish () {for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J + +)
{if (!check (i, J)) {return;
Stop User Time Timer ShuduMainFrame.userTimeAction.stop ();
Clear all Cell monitor clearalllistener ();
The number of Shudumainframe.pass plus one + + 1; if (Shudumainframe.pass >) {int o = Joptionpane. Showconfirmdialog (This, "you have cleared the border, whether Start over.
"," ", 0);
if (o = = 1) {system.exit (0);
else {shudumainframe.pass = 1; } else {Joptionpane.showmessagedialog (this), "Congratulations on your passing this pass." Spents: "+ shudumainframe.usedtime +" seconds \ n is about to enter the next level.
");
//Update the checkpoint prompt ShuduMainFrame.lbPass.setText ("" + Shudumainframe.pass);
Start a new checkpoint reloadcanvers ();
When the user is turned on, the timer ShuduMainFrame.userTimeAction.start (); * * * Check the cell at the specified coordinates */private Boolean check (int i, int j) {if (Cells[i][j].gettext (). IsEmpty (
) {return false; for (int k = 0; k < 9; k++) {if (Cells[i][j].gettext (). Trim (). Equals (Cells[i][k].gettext (). Trim ()) && j!=k) {return false;
} if (Cells[i][j].gettext (). Trim (). Equals (Cells[k][j].gettext (). Trim ()) && I!= k) {
return false;
int II = (I/3) * 3 + K/3;
int JJ = (J/3) * 3 + k% 3; if (Cells[i][j].gettext (). Trim (). Equals (Cells[ii][jj].gettext (). Trim ()) &&! (
i = = II && j = = JJ)) {return false;
} return true;
/* * Reload Sudoku/public void Reloadcanvers () {shudumainframe.usedtime = 0;
Maps = Shuduhelper.getmap ();
for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J +) {This.remove (cells[i][j]);
Create cell cells[i][j] = new Shuducell (); Set position cells[i][j].setlocation (M + i * + (I/3) * 5, 20 + J * + (J/3) * 5);
if (Passrole (Shudumainframe.pass)) {Cells[i][j].settext ("" + maps[i][j]);
Sets the background color Cells[i][j].setbackground (GetColor (Maps[i][j]));
Cells[i][j].setenabled (FALSE);
Cells[i][j].setforeground (Color.gray);
else {Cells[i][j].addmouselistener (this);
} this.add (Cells[i][j]);
} this.repaint ();
Checkfinish (); * * * * randomly generated at the level of the location is displayed number/private Boolean passrole (int pass) {//TODO auto-generated method
Stub return Math.random () * one > pass;
}/* * To obtain color based on number/private color getcolor (int i) {color color = Color.pink;
switch (i) {case 1:color = new color (255, 255, 204);
Break Case 2:color = new Color (204, 255, 255);
Break
Case 3:color = new color (255, 204, 204);
Break
Case 4:color = new color (255, 204, 153);
Break
Case 5:color = new color (204, 255, 153);
Break
Case 6:color = new color (204, 204, 204);
Break
Case 7:color = new color (255, 204, 204);
Break
Case 8:color = new color (255, 255, 255);
Break
Case 9:color = new color (153, 255, 153);
Break
Default:break;
return color; @Override public void mouseclicked (MouseEvent e) {//TODO auto-generated method stub} @Over
Ride public void mousepressed (MouseEvent e) {int modes = E.getmodifiers ();
if ((Modes & Inputevent.button3_mask)!= 0) {//Click the right mouse button//empty click on the contents of the cell ((Shuducell) E.getsource ()). SetText (""); else if ((Modes & Inputevent.button1_mask)!= 0) {//Click the left mouse button//If the number window is selected to destroy if (Selectnum!)
= null) {selectnum.dispose ();
//Create a new selection window Selectnum = new Selectnumframe ();
Set to modal window Selectnum.setmodal (true); Sets the location of the selection window on the Monitor selectnum.setlocation (E.getlocationonscreen (). x, E.getlocationonscreen ()
. y);
Pass the clicked cell to the Number Selection window Selectnum.setcell ((Shuducell) E.getsource ());
Displays the number Selection window selectnum.setvisible (true);
} checkfinish ();
* * * Clear all cell clicks/private void Clearalllistener () {for (int i = 0; i < 9; i++) {
for (int j = 0; J < 9; J + +) {Cells[i][j].removemouselistener (this); @Override public void mousereleased (mouseevent e) {//TODO auto-generated method stub} @Override public void mouseentered (MouseEvent e) { Todo auto-generated method stub} @Override public void mouseexited (MouseEvent e) {//Todo Aut
o-generated method Stub}}
Sudoku Cell:
Package hlc.shudu.ui;
Import Java.awt.Color;
Import Java.awt.Font;
Import Javax.swing.JButton;
public class Shuducell extends JButton {public
Shuducell () {
this.setsize (50,50);
Font font = new Font ("", 2,24);
This.setfont (font);
This.setbackground (New Color (255,153,102));
This.setforeground (Color.Blue);
}
number selection box:
package hlc.shudu.ui;
Import Java.awt.Color;
Import Java.awt.Window;
Import java.awt.event.InputEvent;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Javax.swing.JButton;
Import Javax.swing.JDialog;
Import Javax.swing.JFrame;
public class Selectnumframe extends JDialog implements MouseListener {private Shuducell cell;
public void Setcell (Shuducell cell) {This.cell = cell;
Public Selectnumframe () {//Hide the toolbar this.setundecorated (true) above the interface;
This.setsize (150, 150);
This.setbackground (New Color (255,204,153, 123));
This.setlayout (NULL);
Addnum (); }//Add digital 1~9 private void Addnum () {for (int i = 0; i < 3; i++) {for (int j = 0; J <