简体   繁体   中英

How to draw a numbers onto a panel in a 2d array for a game board

How to draw a numbers onto a panel in a 2d array for a game board?

import javax.swing.*;
import java.awt.*;

public class board2 extends JFrame {

JFrame frame;
JPanel squares[][] = new JPanel[10][10];

public board2() {

  setSize(500, 500);
  setLayout(new GridLayout(10, 10));

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        squares[i][j] = new JPanel();

        if ((i + j) % 2 == 0) {
            squares[i][j].setBackground(Color.black);
        } else {
            squares[i][j].setBackground(Color.white);
        }   
        add(squares[i][j]);
     }
   }
 }
}

I would like to number the panels in the way shown here .

Summary: You should add a label to each of your panels that displays the number.

A couple points:

  1. If your class extends JFrame , you dont need to have one as a member variable.
  2. It isnt clear that you are setting your frame visible anywhere (perhaps you just didnt include that code in your example. I am bringing this up because there is no declaration of what is actually wrong) with your code so far - so perhaps it just inst showing up, so a setVisible(true) would be important.
  3. If you want to add numbers, you need to ad a JLabel to each as you iterate. It would be good to have the foreground of these JLabel instances alternate foreground. You can create the label by using your i and j counters to calculate your square's number.

It would be good to encapsulate the numbering mechanism in a separate method, as you have noted that the specification requires alternating rows to count from the left or the right. Something like the following:

    JLabel label = new JLabel(getCellNumber(((i*10)+j),10) + "");

and then a crude version of the getCellNumber() method could look something like this:

private int getCellNumber(int id, int columnCnt) {
    int rowID = (id) / columnCnt;
    int colID = (id) % columnCnt;
    if (rowID %2 == 1) {
        colID = columnCnt - colID;
    } else {
        colID++;
    }
    return 101 - ((rowID * columnCnt) + colID);
}

This is your program:

import javax.swing.*;
import java.awt.*;

public class ChessBoard extends JFrame {

JFrame frame;
JPanel squares[][] = new JPanel[10][10];

public ChessBoard() {

setName("Chess Board");

  setSize(500, 500);
  setLayout(new GridLayout(10, 10));



  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        JLabel label = new JLabel(getCellNumber(((i*10)+j),10) + "");
        JPanel panel = new JPanel();
        panel.add(label);
        squares[i][j] = panel;
        if ((i + j) % 2 == 0) {
            squares[i][j].setBackground(Color.black);
            label.setForeground(Color.white);
        } else {
            squares[i][j].setBackground(Color.white);
            label.setForeground(Color.black);
        }
        add(squares[i][j]);
     }
   }
   setVisible(true);
 }

public static void main(String [] args){

    new ChessBoard();

}



private int getCellNumber(int id, int columnCnt) {
    int rowID = (id) / columnCnt;
    int colID = (id) % columnCnt;
    if (rowID %2 == 1) {
        colID = columnCnt - colID;
    } else {
        colID++;
    }
    return 101 - ((rowID * columnCnt) + colID);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM