Java

Soldato
Joined
17 Mar 2005
Posts
4,042
Location
Home
A little problem i currently have a grid numbered 0 through to 99 and i need to place a jpeg file which will be randomly added to any of the buttons ( the grid is made of 100 buttons ) and on clicking this button i need it to produce a message saying "blahblah".
 
Not entierly sure what you are trying to do with the first bit - I assume you wish to use ImageIcon though.

But for the button,

Code:
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() 
{
       public void actionPerformed(ActionEvent e)
       {
                 JOptionPane.showMessageDialog(null, "blah");
       }
});
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bShip2 extends JFrame implements ActionListener
{

private JPanel Panel, jPanelButtons, jPanelCenter;
private JButton [] lButton = new JButton[100];
private JButton [] rButton = new JButton[100];
private JButton btnStart;
private Graphics g;
private JLabel message;
private boolean miss = false;


public bShip2 ()
{
super ("BattleShips V1.0");



// JOptionPane.showMessageDialog(null,"Welcome"); // Welcoming Message
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());

message = new JLabel("Battleships Game");
message.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,50));
c.add(message, BorderLayout.NORTH);

jPanelButtons = new JPanel(new GridLayout(10,10));
jPanelButtons.setPreferredSize(new Dimension(250,250));
for(int loop2 = 0; loop2 <100; loop2++)
{
lButton[loop2] = new JButton();
jPanelButtons.add(lButton[loop2]);
}
c.add(jPanelButtons, BorderLayout.EAST);

jPanelCenter = new JPanel(new GridLayout(10,10));
jPanelCenter.setPreferredSize(new Dimension(250,250));
for(int loop2 = 0; loop2 <100; loop2++)
{
rButton[loop2] = new JButton();
jPanelCenter.add(rButton[loop2]);
}
c.add(jPanelCenter, BorderLayout.WEST);
jPanelCenter.setBackground(new Color(255,255,128));

Panel = new JPanel();
Panel.setPreferredSize(new Dimension(50,75));
c.add(Panel, BorderLayout.CENTER);




btnStart = new JButton("Start"); // Button is made
btnStart.addActionListener(this); // Button is given instructions on pressing
c.add(btnStart, BorderLayout.SOUTH);


setSize(600,450);
show();



}

public static void main(String[] args)
{
bShip2 myShip = new bShip2();

}

What i have so far not sure entirely what that code you suggest does because im using arrays to assign numbers to each block and on a random block being clicked on i wish for a message coming up saying youve been hit or something and then from there i will work out how to assign an image instead.
 
try this.
Simple but does the job, just need to replace hit.gif and miss.gif with the FULL path to your own images!
If you want to pop up a message as well then just add the code to the actionPerformed() method at the bottom

Code:
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class BattleShips extends JFrame
{
	private static final int GRID_SIZE = 10;
	private List<OurBtn> theButtons = new ArrayList(GRID_SIZE* GRID_SIZE);

	public BattleShips()
	{
		super ("Battle Ships");
		getContentPane().setLayout(new GridLayout(GRID_SIZE, GRID_SIZE));
		for (int i = 0; i < GRID_SIZE* GRID_SIZE; i++)
		{
			final OurBtn b = new OurBtn(Integer.valueOf(i).toString());
			getContentPane().add(b);
			theButtons.add(b);
		}
		assignShips();
	}

	private void assignShips()
	{
		for (int i = 0; i < 10; i++) {
			OurBtn b = theButtons.get((int)Math.round(Math.random() * 99));
			b.setImage(new ImageIcon("hit.gif"));
		}
	}

	public static void main(String[] args)
	{
		BattleShips bs = new BattleShips();
		bs.setSize(600,600);
		bs.setVisible(true);
	}
}

class OurBtn extends JButton implements ActionListener
{

	private ImageIcon theImage = new ImageIcon("miss.gif");
	OurBtn(String s)
	{
		super(s);
		addActionListener(this);
	}

	public void setImage(ImageIcon i)
	{
		theImage = i;
	}

	public void actionPerformed(ActionEvent e)
	{
		setIcon(theImage);
		setText("");
	}
}
 
Back
Top Bottom