Just create a JPanel inside your games JFrame with NetBeans and call it e.g. boardPanel.
Set the boardPanel to gridLayout with 10 rows and 10 cols.
Use two nested for loops (x and y coordinates) to add the JButtons to the cells. (See swing tutorials)
Use aButton.putClientProperty(“location”, new Point(x,y)); to store the square’s location for later use.
Add an ActionListener to the buttons to change the states.
In the actionPerformed(event) you can use ((JButton)event.getSender()).getClientProperty(“location”)
to get the location of the square which was clicked
change the square-state in BAW class and change the state of the buttons directly
You should not need any Thread to update the squares.
When you in the game push one button you don’t only change the state of that button but probebly several buttons. what is the best method to refresh all the buttons?
You could create a updateSquares() method in BAW which updates all buttons’ state. You need to store the buttons making your squares in BAW for that. On actionPerformed of one button, set its the state in BAW and call updateSquares().
Building a board game GUI using stock widgets is like trying to build a car by going down
the to the appliance store and buying a refrigerator, blender, and self propelled vacuum cleaner.
You will be much happier with the eventual result if you go all the way down to a plain Canvas
class and build it up from there.
Actually it might be the easiest way to start. And for what he want’s to do, using a grid layout and JButtons might be just right. Going down to a plain canvas for a beginner is likely a frustrating task - you have to know so much more details to get it going.
Now i have read the swing tutorial again, i have read a lot of API’s too.
But there I have some questions still.
this is what i have done for now, not at all finished but still i get a lot of problems and questions to ask (it is only a part of the class, this snippet is a part of the BWA class): http://pastebin.com/f5d55545a
In the first line i get a “interface expected here” when i have “import java.awt.event.MouseAdapter;” why that?
Then i get a error in line 9: I have “import java.awt.Point;” but it says the “Cannot find symbol” error
line 14 and 21 also give errors but i think that have something to do with that error in line 1.
the MouseAdapter is a class, not an interface, so your can not use it with implements. You need to implement a MouseListener instead or create an anonymous instance of the MouseAdapter directly while adding.
Anyway, this is not what you should do. Implement an ActionListener and add it to the button instead.
Speaking of the button:
int placering = y+i;
Pladen.add(new JButton().putClientProperty(placering , Point(y,i)));
is wrong, since this way you would add the result of putClientProperty() to “pladen”. Also you are missing a “new” statement before the Point, hence the “Cannot find symbol” message. The meanign of your “placering” variable is not clear to me. You won’t gain anything by having a client property name, that’s changing for every button. How do you know, which client property you would extract in the actionPerformed() method? Reread my post that suggests the putClientProperty().
Try
JButton button= new JButton();
button.putClientProperty("location" , new Point(y,i));
button.addActionListener(this); // implement ActionListener in PladenPanel.
pladen.add(button);
Btw. you should adhere to some coding conventions:
class and interface names should start with a capital letter (public class PladenPanel extends JPanel implements ActionListener)
method and variable names should start with a lower case letter (JPanel pladen = new JPanel(new GridLayout(10,10));)
name your interfaces, classes, methods and variables in english
All this is fairly basic stuff. I would advice to find some decent tutorials to teach you java and swing and follow them word by word until you have grasped the concepts.
Linking to your local drive will obviously not work
I see now that there is some basic stuff i forgot to make, i were to fast on that ocation sry, and then there is some stuff that i am very unsercure in (especially swing) and i thank you for advecing me
This is the last error NetBean shows to me right now, thank you for all your support so far… sry becuse it is kind af basic, but i hope i just need a push in the right direction then i can stand for myself
I compile and it goes fine, then at runtime i get this error:
White billedet mangler
Blank billedet mangler
Black billedet mangler
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
at Engine.blackAndWhite.KnapIcon(blackAndWhite.java:529)
at Engine.blackAndWhite$pladenPanel.<init>(blackAndWhite.java:575)
at Engine.blackAndWhite.<init>(blackAndWhite.java:93)
at Engine.Main$1.run(Main.java:23)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Looks like either Black or White is null. NetBeans (like mostly all IDEs) has an integrated debugger, which will be useful for such problems. You can set breakpoints (the debugger will stop at this line and you can inspect the values of your variables) and start your code in debug mode. You can then step through your code line by line to see, what is going wrong.
I have tried different things and they all return null:
[quote] return new ImageIcon(getClass().getClassLoader().getResource(“White.png”));
[/quote]
[quote]Image Black = null;
// try {
// Black = ImageIO.read(getClass().getClassLoader().getResourceAsStream(“Black.png”));
// } catch (IOException e) {
//
// System.out.println(“Black billedet mangler”);
//
// }
[/quote]
as you can se i have also changed my images to png.
It is important, that your images are on the classpath (e.g. in the same folder - or later jar - like your compiled classes). If you use getClass().getClassLoader().getResource(), You have to use the full qualified location under that folder, like getClass().getClassLoader().getResource("/your/package/White.png") including the fist slash (/).