2012-07-22 114 views
1

我没有很多swing/awt的经验。Java swing/awt组件绘制棋盘

我的问题是: 我需要绘制像棋盘(NxN)的东西。 一般而言,我需要访问每个单元格以进行更改(程序运行时,例如,我单击按钮,并且该单元板上的单元格发生了变化)。 如果一个Component让我在单元格Image中设置,将会很大。

我尝试使用GridLayout但我没有得到任何满足我的东西。

你有什么想法如何简单地解决这个问题?

+2

你可能希望向我们展示你的GridLayout尝试,因为如果做得对,这可能工作得很好,如果你编写它错了,通过向我们展示它,你允许我们检查你的代码,很可能找到你做错了什么,并告诉你如何解决它。 – 2012-07-22 16:22:30

+4

我必须警告你,你已经有5票中的2票可以结束这个问题(我还没有),所以如果你不能很快向我们展示更多的信息,包括你的努力。 – 2012-07-22 16:28:50

回答

3

我在Java中自己做了一个国际象棋游戏,并找到了我使用的代码。

因此,这里是值得您起步:

ChessBoardTest:

public class ChessBoardTest { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     try { 
      Image blackBlock=ImageIO.read(new File("c:/bblock.jpg")); 
      Image whiteBlock=ImageIO.read(new File("c:/wblock.jpg")); 

      Board board = new Board(whiteBlock,blackBlock); 

      //add pieces to board 
      board.addPiece(new ImageIcon("c:/castle.jpg"), "A1");//just one example 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

Board.java:

import java.awt.*; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/* 
* @author David Kroukamp 
*/ 
public class Board extends JFrame { 

    //intialize variables 
    private Image boardImage1; 
    private Image boardImage2; 
    //intialize components 
    private JPanel centerPanel = new JPanel(); 
    private JPanel southPanel = new JPanel(); 
    private JPanel westPanel = new JPanel(); 
    //initialze arrays to hold panels and images of the board 
    private JLabel[] labels = new JLabel[64]; 
    private ImagePanel[] panels = new ImagePanel[64]; 

    public Board(Image boardImage1, Image boardImage2) { 
     this.boardImage1 = boardImage1; 
     this.boardImage2 = boardImage2; 
     createAndShowGUI();//call method to create gui 
    } 

    private void createAndShowGUI() { 
     setTitle("Chess board example"); 

     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     addComponentsToPane(getContentPane()); 

     setSize(800, 600); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    /** 
    * Adds all the necessary components to the content pane of the JFrame, and 
    * adds appropriate listeners to components. 
    */ 
    private void addComponentsToPane(Container contentPane) { 

     GridLayout gridLayout = new GridLayout(8, 8); 
     centerPanel.setLayout(gridLayout); 

     //call mehod to add labels to south panel 
     addLabelsToSouthPanel(); 
     //call method to add oanels to west panel 
     addLabelsToWestPanel(); 
     //call method to add panels and labels to the center panel which holds the board 
     addPanelsAndLabels(); 
     //add all panels to frame 
     contentPane.add(centerPanel, BorderLayout.CENTER); 
     contentPane.add(southPanel, BorderLayout.SOUTH); 
     contentPane.add(westPanel, BorderLayout.WEST); 
    } 

    private void addLabelsToSouthPanel() { 
     GridLayout gridLayout = new GridLayout(0, 8); 

     southPanel.setLayout(gridLayout); 
     JLabel[] lbls = new JLabel[8]; 
     String[] label = {"A", "B", "C", "D", "E", "F", "G", "H"}; 

     for (int i = 0; i < 8; i++) { 
      lbls[i] = new JLabel(label[i] + ""); 
      southPanel.add(lbls[i]); 
     } 
    } 

    private void addLabelsToWestPanel() { 
     GridLayout gridLayout = new GridLayout(8, 0); 

     westPanel.setLayout(gridLayout); 
     JLabel[] lbls = new JLabel[8]; 
     int[] num = {8, 7, 6, 5, 4, 3, 2, 1}; 
     for (int i = 0; i < 8; i++) { 
      lbls[i] = new JLabel(num[i] + ""); 
      westPanel.add(lbls[i]); 
     } 
    } 

    private void addPanelsAndLabels() { 

     //call methd to create panels with backgound images and appropriate names 
     addPanelsAndImages(); 

     for (int i = 0; i < panels.length; i++) { 
      labels[i] = new JLabel(); 

      //used to know the postion of the label on the board 
      labels[i].setName(panels[i].getName()); 

      panels[i].add(labels[i]); 

      //adds panels created in addPanelsAndImages() 
      centerPanel.add(panels[i]); 
     } 
    } 

    //this method will create panels with backround images of chess board and set its name according to 1-8 for rows and A-H for coloumns 
    private void addPanelsAndImages() { 
     int count = 0; 
     String[] label = {"A", "B", "C", "D", "E", "F", "G", "H"}; 
     int[] num = {8, 7, 6, 5, 4, 3, 2, 1}; 

     for (int row = 0; row < 8; row++) { 
      for (int col = 0; col < 8; col++) { 
       if ((col + row) % 2 == 0) {//even numbers get white pieces 
        panels[count] = new ImagePanel(boardImage1); 
       } else {//odd numbers get black pieces 
        panels[count] = new ImagePanel(boardImage2); 
       } 

       panels[count].setName(label[col] + num[row]); 
       count++; 
      } 
     } 
    } 

    //method sets image of a label at a certain position in the board according to the block name i.e D4 
    public void addPiece(ImageIcon img, String block) { 
     for (int s = 0; s < labels.length; s++) { 
      if (labels[s].getName().equalsIgnoreCase(block)) { 
       labels[s].setIcon(img); 
      } 
     } 
    } 

//nested class used to set the background of frame contenPane 
    class ImagePanel extends JPanel { 

     private Image image; 

     /** 
     * Default constructor used to set the image for the background for the 
     * instance 
     */ 
     public ImagePanel(Image img) { 
      image = img; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      //draws image to background to scale of frame 
      g.drawImage(image, 0, 0, null); 
     } 
    } 
} 

HTH让你开始。

+0

谢谢。这非常有帮助。 – user1055201 2012-07-22 22:39:12