Hello,
I'm new to the forums, so I apologize in advance for breaking any posting conventions.
I'm creating a game where I need to be able to display a grid and I need to be able to update what's contained in each cell of the grid frequently. The contents of each cell will either be nothing, or one of two images. I would like to eventually be able to make it so that if the user clicks on a specific cell, I will be able to respond to that click. It would also be ideal if I could drag and drop (from within my program) to each cell.
I'm not sure what class to use, buttons, content, text boxes etc to populate the grid and any direction would be much appreciated.
I'm new to GUI's, not new to programming, but so far have created a window, which on a button click closes and opens another window.
Thanks in advance for any advice!
-Pnev
1 reply to this topic
#1
Posted 22 November 2010 - 12:03 AM
|
|
|
#2
Posted 22 November 2010 - 06:16 AM
The grid with cells which contain image and you can click on is fairly simple. You just need a jpanel with a gridlayout and you fill that grid with a bunch of JButtons. JButtons can contain an image, and you can obviously click on them and catch that click.
To make the buttons not look like buttons you'll need to change each button as followed:
Dragging and dropping is no more 'basic' swing. I think it's abit more advanced.
You'll need mouseMotionListener, mouseAdapter maybe. You'll need to override the paint method of the area you want to drag and drop in.
Please proved a bit more info about what exactly you want to drag and drop and i may give you additional info.
Note that I just wrote the above code out of nowhere, i hope it doesn't contain errors but it might ;)
Public class MyClass extends JFrame{
private ImageIcon icon= new ImageIcon("images/icon.jpg");
private ImageIcon otherIcon= new ImageIcon("images/otherIcon.jpg");
public MyClass(){
JPanel centerPanel = new JPanel(new GridLayout(0,10));
for(int i=0 ; i<100 ; i++){
JButton button = new JButton();
button.setIcon(icon);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JButton button = e.getSource();
button.setIcon(otherIcon);
}
});
centerPanel.add(button);
}
pack();
setVisible(true);
}
}
This will create a frame with a 10x10 grid of buttons, each containing an image, and upon clicking will change into another image.To make the buttons not look like buttons you'll need to change each button as followed:
setContentAreaFilled(false); setBorderPainted(false); setOpaque(false); setFocusPainted(false);
Dragging and dropping is no more 'basic' swing. I think it's abit more advanced.
You'll need mouseMotionListener, mouseAdapter maybe. You'll need to override the paint method of the area you want to drag and drop in.
Please proved a bit more info about what exactly you want to drag and drop and i may give you additional info.
Note that I just wrote the above code out of nowhere, i hope it doesn't contain errors but it might ;)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









