This article shares the specific code of the Java version of Sudoku game interface implementation for your reference. The specific content is as follows
Implementation renderings:
Write a picture description here
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(); mainFrame.setVisible(true); }}Main form class (including message area, time area, 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 JLabel lbPass; // display the level's lable public static long usedTime = 0; // player use 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 area addPanelTime(panelComponent); // Add component 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 system time timer Timer sysTimeAction = new Timer(500, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { long 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(ActionEvent e) { lbUserTime.setText(" You have used: " + (++usedTime)+ " sec."); } }); userTimeAction.start(); panelComponent.add(panelTime, BorderLayout.EAST); } /* * Add message area*/ private void addPanelMsg(JPanel panelComponent) { // panelComponent.setBorder(new 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("Level:"); lbPass1.setFont(font14); panelMsg.add(lbPass1); // Display the number of levels lbPass = new JLabel("" + pass); lbPass.setForeground(Color.RED); lbPass.setFont(font28); panelMsg.add(lbPass); JLabel lbPass2 = new JLabel("off/10 levels in total"); 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()); // Set the initial window size this.setSize(515, 600); // Set the initial position of the window this.setLocation(500, 50); // Set the window title this.setTitle("Sukua Game (By: Hou Longchao)"); // Set the form to not change the size this.setResizable(false); // Set the default close operation 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 the sudoku array int[][] maps = new int[9][9]; private SelectNumFrame selectNum; /* * Default constructor*/ public ShuduCanvers() { ShuduMainFrame.usedTime = 0; maps = ShuduHelper.getMap(); // Load the sudoku area 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(20 + i * 50 + (i / 3) * 5, 20 + j * 50 + (j / 3) * 5); if (passRole(ShuduMainFrame.pass)) { cells[i][j].setText("" + maps[i][j]); // Set 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 it is completed*/ private void checkFinish() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (!check(i, j)) { return; } } } // Stop the user timer ShuduMainFrame.userTimeAction.stop(); // Clear all cell listeners clearAllListener(); // Add one to the pass number ShuduMainFrame.pass += 1; if (ShuduMainFrame.pass > 10) { int o = JOptionPane .showConfirmDialog(this, "You have passed the level, have you started over?", "", 0); if (o == 1) { System.exit(0); } else { ShuduMainFrame.pass = 1; } } else { JOptionPane.showMessageDialog(this, "Congratulations on passing this level! Time spent:" + ShuduMainFrame.usedTime + "seconds/n will enter the next level soon!"); } // Update the level prompt ShuduMainFrame.lbPass.setText("" + ShuduMainFrame.pass); // Start a new level reLoadCanvers(); // Open the user 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().equals(cells[i][k].getText().trim()) && i !=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 area*/ public void reLoadCanvers() { ShuduMainFrame.usedTime = 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(20 + i * 50 + (i / 3) * 5, 20 + j * 50 + (j / 3) * 5); if (passRole(ShuduMainFrame.pass)) { cells[i][j].setText("" + maps[i][j]); // Set 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 generate whether the number is displayed in this position*/ private boolean passRole(int pass) { // TODO Auto-generated method stub return Math.random() * 11 > pass; } /* * Get color based on the 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 } @Override public void mousePressed(MouseEvent e) { int modes = e.getModifiers(); if ((modes & InputEvent.BUTTON3_MASK) != 0) {// Right-click// Clear the content on the click cell ((ShuduCell) e.getSource()).setText(""); } else if ((modes & InputEvent.BUTTON1_MASK) != 0) {// Left-click// If the selected number window exists, destroy if (selectNum != null) { selectNum.dispose(); } // Create a new selection window selectNum = new SelectNumFrame(); // Set to the modal window selectNum.setModal(true); // Set the position 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()); // Display the number selection window selectNum.setVisible(true); } checkFinish(); } /* * Clear all cells' click listening */ 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 Auto-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 on the interface this.setUndecorated(true); this.setSize(150, 150); this.setBackground(new Color(255,204,153, 123)); this.setLayout(null); addNum(); } //Add numbers 1~9 private void addNum() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { JButton btn = new JButton(); btn.setSize(50, 50); btn.setLocation(i*50,j*50); btn.setText(""+(j*3+i+1)); btn.addMouseListener(this); this.add(btn); } } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { int modes = e.getModifiers(); if ((modes & InputEvent.BUTTON1_MASK) != 0) { JButton btn = (JButton) e.getSource(); cell.setText(btn.getText()); } this.dispose(); } @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 mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub }}The complete package can be downloaded on GitHub: https://github.com/houlongchao/shudu.git
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.