I am quiet new to Java and I try to make a simple turn-based game just to gain some experience. I have encounter a problem which I cannot solve on my own, despite I did quiet a lot of google research... Probably I cannot solve it because I don't understand it well enough.
The problem is I don't know how to make a program making turns for player. I mean I would like to make a few Jbuttons active till one of them is pressed and then do the same for another player. Since program is quiet complicated, I deleted 80% of code which is unrelated to problem and I post the rest of it below.
The code is divided into classes and I would like to keep it this way due to complicity of a problem (remaining 80% of code). In order to see better what programs do, I made buttons to change colours each time when activated. Unfortunatelly program stops working as soon as it hits notify() method.
Can anyone help? Thank you in advance.
Tester:
package grafika;
import javax.swing.SwingUtilities;
public class OknoTester {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Okno main = new Okno();
}
// ctr + spacja - podpowiedzi w Eclipsie
});
}
}
Main class
package grafika;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class Okno {
private JFrame frame;
public Okno()
{
initialize();
}
private void initialize()
{
// NEW WINDOW
frame = new JFrame();
frame.setTitle("Gra w statki");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(600,100));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
// DIVIDE WINDOW
JSplitPane splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
frame.getContentPane().add(splitPaneH);
splitPaneH.setDividerSize(0);
// INITIALIZATION OF SITES
Komputer left = new Komputer();
Komputer right = new Komputer();
// SHOWPANELS
splitPaneH.setLeftComponent(left.Pokaz());
splitPaneH.setRightComponent(right.Pokaz());
splitPaneH.setResizeWeight(0.5);
splitPaneH.setEnabled(false);
// ALLOW TO USE ONE OF LEFT OR RIGHT SET OF BUTTONS
while(true)
{
left.CzytajRuch();
right.CzytajRuch();
}
}
}
Class with buttons
package grafika;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Komputer implements ActionListener{
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JPanel panel = new JPanel();
private Boolean aktywny = false;
public void CzytajRuch()
{
aktywny = true;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public JPanel Pokaz()
{
return panel;
}
public Komputer() {
GridLayout gl = new GridLayout(1,2);
panel.setLayout(gl);
panel.add(b1);
b1.addActionListener(this);
b2.addActionListener(this);
panel.add(b2);
}
public void actionPerformed(ActionEvent ae) {
if(aktywny == true)
{
JButton pb = (JButton) ae.getSource();
if (pb == b1)
b1.setBackground(new Color((int)(Math.random() * 0x1000000)));
if (pb == b2)
b2.setBackground(new Color((int)(Math.random() * 0x1000000)));
aktywny = false;
notify();
}
}
}
Thank you for your time reading this. I would apprieciate any help.