Algorithm experiment--board overlay

Source: Internet
Author: User
Tags gettext

First, the purpose of the experiment:

Familiar with the design technology of divide and conquer algorithm

Second, the experimental requirements:

1, according to the content requirements of the teaching materials, complete the "board coverage Problem" algorithm. Get a complete and correct program.

2. Board Size:32*32(or 16*16)

3, output the final result.

Third, the experimental equipment and environment:

PC One,java virtual machine Eclipse or jdk Environment

Iv. description of the problem:

Write a checkerboard overlay algorithm in a language, and color the checkerboard so that the L -shaped dominoes can use the same color, allowing the board to see where the pieces are and the effects of the checkerboard coloring.

Five, Algorithm analysis:

Add button

Object throws

Initial

Recursive invocation of recursion

Generated

Square

Event

Processing

field of the algorithm:TrStart line number), TC (Start column number), Dr (Special Turn line number), DC (Special Fanglie number), Size (scale),Tilethe initial value of the tag);Red,green,blue (generate red and green blue values for colorsCenterpanel,Southpanel,panel (three x panels),Button[][] (Grid Array);Trtext, Tctext, Drtext, Dctext, Sizetext (the value entered by the row number);Trlabel, Tclabel, Drlabel, Dclabel, Sizelabel (Row number Label); Okbutton,cancelbutton (Start, Clear button);

Chessboard class: The main class of the algorithm, generating the entire frame, including the panel of squares.

GridLayout class: Generate squares.

Buttonaction class: Used to handle events raised by buttons.

Chessboard method (main algorithm): The algorithm internally uses recursive calls to process the markup and shading of each square

Six, the code:

1.gridLayout class

Class GridLayout {//classes that form squares

Public GridLayout () {

Centerpanel.setlayout (New GridLayout (0, size));

button = new Jbutton[size][size];

for (int i = 0; i < size; i++) {

for (int j = 0; J < size; J + +) {

BUTTON[I][J] = new JButton ();

if (i = = Dr && J = = DC) {

Button[i][j].setbackground (Color.Blue);

Button[i][j].settext ("

}

Centerpanel.add (Button[i][j]);

Button[i][j].setenabled (FALSE);

}

}

}

Chessboard ();

}

2.ChessBoard () method

public void chessboard (int tr, int tc, int dr, int dc,int size) {//Paving chess algorithm implementation

if (size = = 1) return;//checkerboard size is 1, indicating recursion to the innermost

int t = tile++;//increments by 1 per increment

Random rd = new random ();

Red = Rd.nextfloat ();

Green = Rd.nextfloat ();

Blue = Rd.nextfloat ();

Color col = new color (red, green, blue);

int s = SIZE/2; Row, column number (equal) in the middle of the chessboard

Check if the special block is in the upper-left sub-chessboard

if (Dr < tr + S && DC < TC + s)//In

Chessboard (TR, TC, DR, DC, s);

else//not, treat the block in the lower right corner of the sub-checkerboard as a special block

{

BUTTON[TR + S-1][TC + s-1].setbackground (col);

BUTTON[TR + S-1][TC + s-1]

. SetText ("

+ "</Font>

Chessboard (TR, TC, TR + s-1, TC + s-1, s);

}

Check if the special block is in the upper-right sub-chessboard

if (Dr < tr + S && DC >= TC + s)//In

Chessboard (TR, TC + S, Dr, DC, s);

else//not, treat the lower left corner of the sub-checkerboard as a special block

{

BUTTON[TR + S-1][TC + s].setbackground (col);

BUTTON[TR + S-1][TC + S]

. SetText ("

+ "</Font>

Chessboard (TR, TC + S, tr + s-1, TC + S, s);

}

Check if the special block is in the bottom-left sub-chessboard

if (Dr >= tr + S && DC < TC + s)//In

Chessboard (tr + S, TC, DR, DC, s);

else//not, treat the block in the upper right corner of the sub-checkerboard as a special block

{

BUTTON[TR + S][TC + s-1].setbackground (col);

BUTTON[TR + S][TC + s-1]

. SetText ("

+ "</Font>

Chessboard (tr + S, TC, TR + S, TC + s-1, s);

}

Check if the special block is in the lower-right sub-chessboard

if (Dr >= tr + S && DC >= TC + s)//In

Chessboard (tr + S, TC + S, Dr, DC, s);

else//not, treat the block in the upper left corner of the sub-checkerboard as a special block

{

BUTTON[TR + S][TC + s].setbackground (col);

BUTTON[TR + S][TC + S]

. SetText ("

+ "</Font>

Chessboard (tr + S, TC + S, tr + S, TC + S, s);

}

}

3.ButtonAction class

public class Buttonaction implements ActionListener {//DOT button when event response

public void actionperformed (ActionEvent e) {//need to call GridLayout class

if (E.getsource () ==okbutton) {

Getcontentpane (). Add (Centerpanel, borderlayout.center);

int TR = Integer.parseint (Trtext.gettext ());

int TC = Integer.parseint (Tctext.gettext ());

int DR = Integer.parseint (Drtext.gettext ());

int DC = Integer.parseint (Dctext.gettext ());

int Size = 1;

for (int i = 0; i < Integer.parseint (Sizetext.gettext ()); i++)

Size *= 2;

TR = TR;

TC = TC;

Dr = Dr;

DC = DC;

size = size;

try {

GridLayout Grid = new GridLayout ();

Grid. Chessboard (TR, TC, DR, DC, size);

Centerpanel.updateui ();

} catch (Exception EX) {

Ex.printstacktrace ();

}

Panel.removeall ();

Okbutton.setenabled (FALSE);

}

if (E.getsource () ==cancelbutton) {//Event response when you click the next prompt button

JLabel label = new JLabel ();

Label.settext ("

Panel.add (label, Centerpanel);

Getcontentpane (). Add (Panel, borderlayout.center);

Panel.updateui ();

Tile = 1;//makes each refresh count from 1

Centerpanel.removeall ();

Okbutton.setenabled (TRUE);

}

}

}

4. Importing packages and main classes

Import Java.awt.BorderLayout;

Import Java.awt.Color;

Import java.awt.Dimension;

Import Java.awt.GridLayout;

Import Java.awt.Toolkit;

Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;

Import Java.util.Random;

Import Javax.swing.JButton;

Import Javax.swing.JFrame;

Import Javax.swing.JLabel;

Import Javax.swing.JPanel;

Import Javax.swing.JTextField;

public class Chessboard extends JFrame {

private int TR, TC, DR, DC, size;//define each member variable

int tile = 1;

Float red, green, blue;//give button color

JPanel Centerpanel,southpanel,panel;

jbutton[][] button;

JTextField Trtext, Tctext, Drtext, Dctext, Sizetext;

JLabel Trlabel, Tclabel, Drlabel, Dclabel, Sizelabel;

JButton OKButton;

JButton CancelButton;

Public chessboard () {

Super ("board overlay");

Init ();

This.setresizable (FALSE);

SetVisible (TRUE);

Okbutton.addactionlistener (New Buttonaction ());

Cancelbutton.addactionlistener (New Buttonaction ());

Trtext.setenabled (FALSE);

Tctext.setenabled (FALSE);

Getcontentpane (). Add (Southpanel, Borderlayout.north);

}

private void Init () {//Add initialization of individual controls

panel = new JPanel ();

Centerpanel = new JPanel ();

Southpanel = new JPanel ();

OKButton = new JButton ("Start");

CancelButton = new JButton ("clear");

Trtext = new JTextField ("0", 2);//define each component

Tctext = new JTextField ("0", 2);

Drtext = new JTextField ("0", 2);

Dctext = new JTextField ("0", 2);

Sizetext = new JTextField ("4", 2);

Toolkit Tk=toolkit.getdefaulttoolkit ();

Dimension ds=tk.getscreensize (); This.setbounds (((int) ds.getwidth () -850)/2, (int) (Ds.getheight ()-650)/2, 850, 650);//Set window size and position

Trlabel = new JLabel ("Starting grid coordinates x:");

Tclabel = new JLabel ("Starting grid coordinates y:");

Drlabel = new JLabel ("Special grid coordinates x:");

Dclabel = new JLabel ("special grid coordinates y:");

Sizelabel = new JLabel ("Checkerboard Size Size:");

Southpanel.add (OKButton);

Southpanel.add (CancelButton);//Add each component to the form

Southpanel.add (Trlabel);

Southpanel.add (Trtext);

Southpanel.add (Tclabel);

Southpanel.add (Tctext);

Southpanel.add (Drlabel);

Southpanel.add (Drtext);

Southpanel.add (Dclabel);

Southpanel.add (Dctext);

Southpanel.add (Sizelabel);

Southpanel.add (Sizetext);

}

New GridLayout ();//Here is the code for the GridLayout class

New Buttonaction ();//Here is the code for the Buttonaction class

public static void Main (string[] args) {//main function method implementation

New Chessboard ();

Runtime.getruntime (). GC ();//manual removal of data garbage

}

}

Seven, commissioning and operation:

1. Special squares in 1 rows 2 columns, with a scale of 5 , the result is as follows

2. special squares in the row of the size of 5 is the debug results are as follows

Viii. Summary of the experiment

Through this experiment, the recursive invocation of the function has a further deep understanding, because it is done in Java , but also for the Java language to build a form of familiarity, in addition to the use of color when programming, A variety of methods to call color to learn more, the experiment is mainly through the method of recursive call to achieve a large area, a regular board coverage problem. It is important to note that the chessboard problem is a representative case in some of the events in our life, which shows that recursion is a good embodiment in dealing with some similar operations but with regular changes.

Algorithm experiment--board overlay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.