Results 1 to 3 of 3
  1. #1
    8492nd is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Help Needed - Creating an Array of Buttons with Images

    Hi all,
    Im new here and to Java, so please bear with me if I seem a little uneducated as my understanding of Java is very limited. I am undergoing a University project in Java SE and am at the end of my tether now - I simply cannot figure how to get this array working.

    My tutor is as useful as a 3-speed walking stick, so could someone of mighty Java knowledge please assist me?

    I need to create an array of 232 buttons which display images in a grid of
    21 x 11, and to some extent I have succeeded. I have managed to code an array using a 'for' loop, as shown here (dashed line is to clarify between forum post and code):

    ------------------------------------------------------------------------

    for(int count=1; count<232; count++)
    {
    jBPitch[count] = new JButton();
    jBPitch[count].setSize(new Dimension(32, 32));
    centrePanel.add(jBPitch[count]);

    ---------------------------------------------------------------------

    I have declared an integer variable called 'count' which keeps track of the number of buttons up to 231 (since its an array, starting at 0 iirc), this is the top of my code with imports etc:

    ----------------------------------------------------------------------

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

    public class CRoboBall extends JFrame implements ActionListener

    {
    public JButton jBPitch[] = new JButton[232];
    private JButton playerRobot;
    private JButton jBSolve;
    public int count;
    public JButton jBClose;

    -----------------------------------------------------------------------

    Essentially, the project is an application to display a 21 x 11 grid of buttons with images showing a football pitch, upon which a 'robot' player(identified by a differently coloured button) must find a 'ball' (again, a button with a football image on it) and take it to the goal, then leave it there and return to its starting point.

    It needs to be able to do this automatically, by clicking an 'Auto-Solve' button, and manually, with a set of buttons to control its direction, movement and rotation.

    I need to get this array done so I can start coding the meat of it, which will be no small task in itself for someone like me....but I just cannot get it to work. How do I display images on an array of buttons?
    Ive tried using ImageIcon but I get conversion errors (int to JButton etc).

    If the rest of the code is needed I can post this, but theres a fair old bit so far.

    Kind regards,

    8492nd

  2. #2
    8492nd is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Bump anyone?

  3. #3
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    what's the problem?
    showing the buttons
    adding images
    both?

    all you need is the right layoutManager for 'centrePanel'
    pseudo code

    Java Code:
    final int ROWS = 21;
    final int COLUMNS = 11;
    JPanel centrePanel = new JPanel(new GridLayout(ROWS,COLUMNS));
    JButton jBPitch[] = new JButton[ROWS*COLUMNS];
    ImageIcon pitch[] = new ImageIcon[ROWS*COLUMNS];
    for(int x = 0, y = ROWS*COLUMNS; x < y; x++}
    {
      pitch[x] = new ImageIcon(....);
      jBPitch[x] = new JButton(pitch[x]);
      centrePanel.add(jBPitch[x]);
    }

Similar Threads

  1. Creating Clear and Exit Buttons
    By flambookey in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 05:08 PM
  2. creating buttons
    By defactor in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 02:06 PM
  3. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM
  4. Creating layer on images ?
    By Qmalq in forum AWT / Swing
    Replies: 2
    Last Post: 01-27-2009, 10:20 PM
  5. Creating a flat tool bar with images
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:10 PM

Posting Permissions

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