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