Results 1 to 2 of 2
  1. #1
    obious is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Java Chess piece Array

    Hey guys,

    I'm trying to solve this problem where by I want to put the images in my chess game into an array A)to make the code neater and B) to help with further coding problems.

    Here's what I've got so far

    JLabel Piece;
    JPanel board;

    for (int i = 0; i < 64; i++) {
    JPanel square = new JPanel( new BorderLayout() );
    board.add( square );

    int row = (i / 8) % 2;

    if (row == 0)
    square.setBackground( i % 2 == 0 ? Color.black : Color.white );
    else
    square.setBackground( i % 2 == 0 ? Color.white : Color.black);
    }

    JLabel whitepiece = new JLabel( new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    JPanel panel = (JPanel)board.getComponent(8);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(9);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(10);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(11);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(12);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(13);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(14);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wp.gif"));
    panel = (JPanel)board.getComponent(15);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wr.gif"));
    panel = (JPanel)board.getComponent(0);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wk.gif"));
    panel = (JPanel)board.getComponent(1);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wb.gif"));
    panel = (JPanel)board.getComponent(2);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wq.gif"));
    panel = (JPanel)board.getComponent(3);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wk.gif"));
    panel = (JPanel)board.getComponent(4);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wb.gif"));
    panel = (JPanel)board.getComponent(5);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wk.gif"));
    panel = (JPanel)board.getComponent(6);
    panel.add(whitepiece);
    whitepiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\wr.gif"));
    panel = (JPanel)board.getComponent(7);
    panel.add(whitepiece);
    JLabel blackpiece = new JLabel( new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    JPanel bpanel = (JPanel)board.getComponent(48);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(49);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(50);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(51);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(52);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(53);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(54);
    bpanel.add(blackpiece);
    blackpiece = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\Chess Game\\Resources\\bp.gif"));
    bpanel = (JPanel)board.getComponent(55);
    bpanel.add(blackpiece);

    ....and so on.
    My question is, how do I put the 'pieces' into an array whilst at the same time putting them on the board (obviously this isn't all the code).

    Thanks in advance

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    public static final String IMG_RES = "C:\\Users\\user\\Desktop\\Chess Game\\Resources\\";

    Java Code:
    List<ImageIcon> imgList = new ArrayList<ImageIcon>();
     
    private ImageIcon newImageIcon(String path) {
        ImageIcon ic = new ImageIcon(IMG_RES+path);
        imgList.add(ic);
        return ic;
    }

    Java Code:
    blackpiece = new JLabel(newImageIcon("bp.gif"));
    Last edited by ozzyman; 05-04-2011 at 01:27 PM.

Similar Threads

  1. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 10:14 AM
  2. a good java chess program
    By rico16135 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 12:25 PM
  3. Java Game Expert Wanted to clone Chess.com
    By TOPSECRET in forum Jobs Offered
    Replies: 1
    Last Post: 04-28-2010, 03:20 AM
  4. Decode this piece of Code
    By mikeyl62 in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 08:59 PM
  5. I want play chess in Java
    By ganzorig in forum Advanced Java
    Replies: 6
    Last Post: 11-15-2009, 04:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •