Results 1 to 3 of 3
- 04-27-2010, 10:16 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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
- 04-29-2010, 12:49 PM #2
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Bump anyone?
- 04-30-2010, 03:06 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
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
-
Creating Clear and Exit Buttons
By flambookey in forum New To JavaReplies: 2Last Post: 03-24-2010, 05:08 PM -
creating buttons
By defactor in forum New To JavaReplies: 5Last Post: 01-02-2010, 02:06 PM -
Changing images by clicking arrow buttons. help?
By ashton in forum New To JavaReplies: 3Last Post: 02-08-2009, 11:29 AM -
Creating layer on images ?
By Qmalq in forum AWT / SwingReplies: 2Last Post: 01-27-2009, 10:20 PM -
Creating a flat tool bar with images
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks