Results 1 to 3 of 3
  1. #1
    Ryden7 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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]);
                          }
                        } 
            */
    I commented some stuff out that I thought would not work or I was still working with it when I found a better solution. To change horizontal and vertical colors, I decided to use the [r+1][c+1] etc to change the positions of the ones above, below, right, and left of the button clicked. I just don't know how to implement it. Any help is appreciated.

    Thanks!

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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.

  3. #3
    Ryden7 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •