Re: Need Loop Help for Game
Re: Need Loop Help for Game
Quote:
I need help on how to make it loop.
I need some info about your loop.
What is to be done in the loop?
When will the execution flow exit the loop?
Please edit your code and wrap it in code tags to preserve formatting. Your code is all shifted left making it hard to read. See: BB Code List - Java Forums
or Go Advanced and use the # icon
Re: Need Loop Help for Game
Quote:
Originally Posted by
Norm
I need some info about your loop.
What is to be done in the loop?
When will the execution flow exit the loop?
Please edit your code and wrap it in code tags to preserve formatting. Your code is all shifted left making it hard to read. See:
BB Code List - Java Forums
or Go Advanced and use the # icon
I want to loop the whole program so that if you click on button A, the whole thing restarts and the buttons shift positions. If you click button B, the program exits.
Alright i think this is what you mean by adding code tags. I am new so i don't know much about posting code. Any suggestions would be helpful! : )
Code :
package button_game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button_Game {
//A1 and B1 contain what to do when they are clicked. Right now they terminate the program until i figure out how to loop it.
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
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 x;
x = (int)(200*Math.random());
int y;
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);
}
//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();
}
});
}
}
Re: Need Loop Help for Game
Quote:
click on button A, the whole thing restarts and the buttons shift positions.
What is the "whole thing" that should restart? What are the exact lines of code that you want executed?
You might be able to make the "whole thing" a method and call it when the A button is clicked.
Your formatting doesn't align the ending } with the line with the opening {
That makes it hard to see code levels in if statements.
Re: Need Loop Help for Game
Quote:
Originally Posted by
Norm
What is the "whole thing" that should restart? What are the exact lines of code that you want executed?
You might be able to make the "whole thing" a method and call it when the A button is clicked.
Your formatting doesn't align the ending } with the line with the opening {
That makes it hard to see code levels in if statements.
By whole thing, I mean that I want the whole program to run again and again as long as you keep clicking Button A1 and it to exit when you click Button B1.
Here is my revised code:
I think i got most of the brackets
Code :
package button_game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button_Game {
//A1 and B1 contain what to do when they are clicked. Right now they terminate the program until i figure out how to loop it.
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
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 x;
x = (int)(200*Math.random());
int y;
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);
}
//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();
}
});
}
}
Re: Need Loop Help for Game
never mind they un-align when i post the code
Re: Need Loop Help for Game
Quote:
I want the whole program to run again
Are you sure? Starting with creating a new Frame and new buttons and new listeners etc?
If that is the case, then start a new thread and call the class's main() method.
Re: Need Loop Help for Game
Im not sure exactly what you meant by starting a new thread. Do you mean start it inside:
Code :
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
or just anywhere before the main method
Re: Need Loop Help for Game
Quote:
I want the whole program to run again and again as long as you keep clicking Button A1
I guess in the actionPerformed method would be ok. Create a Thread that calls the class's main() method.
I've never done it so not sure what will happen.
You'll probably get a whole new window, etc.