Results 1 to 3 of 3
Thread: Need help with program?
- 11-17-2011, 10:26 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Need help with program?
Hi everyone, im new to the forums.
I was looking for some help on my program. Im supposed to be designing a game that's called "Lights Out." Here is an example of the game: Sarksap | Lights Out Game
So far I have been stuck on creating the buttons and how the game will work. I have a good idea of what to do because ive been thinking about it for a couple of days. For this program im using a 2d array to create it but the hard part for me is implementing it into the board. Here is my code:
Java Code:import javax.swing.*; import java.awt.; import java.awt.event.; public class Game extends JFrame implements ActionListener { public static void main (String[] args) { Game c = new Game(); c.setVisible(true); } private JButton rows; private JButton columns; public Game () { //Everything goes on the main panel JPanel mainPanel = new JPanel(); // When the window is closed, the application ends setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set title and size setTitle("Lights Off"); setSize(400, 400); // This only/merely allocates an array of JButtons. // Each element of the array is null. JButton[][] boxes = new JButton[5][5]; for(int r = 0; r < boxes.length; r++) { for(int c = 0; c < boxes[r].length; c++) { int n = r*boxes[r].length + c+1; JButton[][] calculate = new JButton[i][x]; //mainPanel.add(calculate); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } } /* setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Lights OFF"); JPanel mainPanel = new JPanel(new GridLayout(5,5));; mainPanel.setLayout(new BorderLayout()); setSize(300, 300); JButton[][] button = new JButton[0][0]; for(int r = 0; r < button.length; r++) { for(int c = 0; c < button[r].length; c++) { mainPanel.add(button[r][c]); } } */
Thanks!
- 11-18-2011, 01:15 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
Re: Need help with program?
You'd be better off implementing your game logic separately from the GUI (as in, using separate classes). Focus on the former before trying to build the latter.
- 11-18-2011, 03:18 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Re: Need help with program?
Thanks for the reply. The way its supposed to be setup is all under 1 class. This is my first semester in Java and I find it to be kind of difficult. I just came across a way to create 25 buttons. But now I need to access them. I was thinking of making the logic of how the buttons work in a different method
The way this is going to work is, when a button is clicked, its going to find that button, find out the position, subtract 1 from the x and , then add 1 to x and y to find out all of the positions around the one that was clicked then change the colors of those ones. The problem right now is adding the action listener which im trying to do right now. Like I said, I am very new to programming and I am trying my best.
Java Code:package layout; import javax.swing.JFrame; //imports JFrame library import javax.swing.JButton; //imports JButton library import java.awt.GridLayout; //imports GridLayout library import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyListener; public class ButtonGrid extends JFrame implements ActionListener { JFrame frame=new JFrame(); //creates frame JButton[][] grid; //names the grid of buttons public ButtonGrid(int width, int length){ //constructor frame.setLayout(new GridLayout(width,length)); //set layout grid=new JButton[width][length]; //allocate the size of grid for(int y=0; y<length; y++){ for(int x=0; x<width; x++){ grid[x][y]=new JButton("("+x+","+y+")"); //creates new button frame.add(grid[x][y]); //adds button to grid } } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); //sets appropriate size for frame frame.setVisible(true); //makes frame visible } public static void main(String[] args) { new ButtonGrid(5,5);//makes new ButtonGrid with 2 parameters } @Override public void actionPerformed(ActionEvent e) { } }
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 01:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 07:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM
Bookmarks