package button_game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button_Game {
static int k;
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
k = 1;
}
private static class B1 implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
//addComponentsToPane positions the buttons on the screen randomly
public static void addComponentsToPane(Container pane){
int r = 1;
while (r == 1)
{ r = r +1;
int x;
int y;
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
int a = (int)(1000*Math.random());
int b = a%2;
if (b == 0){
String c = Integer.toString(a);
JButton A2 = new JButton(c);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
int c = a +1;
String d = Integer.toString(c);
JButton A2 = new JButton(d);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
while (k == 1){
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
if (b == 0){
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
}
}
//this runs the game
private static void runGUI(){
JFrame window = new JFrame("Button Game");
addComponentsToPane(window.getContentPane());
window.setSize(300,300);
window.setVisible(true);
}
//this part runs the subroutine which runs the game
public static void main(String Args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
runGUI();
}
});
}
}
}