简体   繁体   中英

I need help in creating a Lights Game in Java

I have to write a GUI program to play a lights game. This game should display 6 lights, include a menu bar.

This is how far I have gotten so far:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LightsGame extends JFrame{

LightsPanel lightPnl = new LightsPanel();

JButton switchButton[] = new JButton[6];

private LightsGame(){

    JFrame frame = new JFrame("Lights Game");

    JPanel buttonPanel = new JPanel();
    JPanel labelPanel = new JPanel();
    this.getContentPane().add(labelPanel);
    JLabel label = new JLabel("Switch on all the Lights");
    labelPanel.add(label);  

    //LightsPanel lightPnl = new LightsPanel();

    //Code for buttons:
    for (int k = 0; k <= 5; k++){
        switchButton[k] = new JButton("Light " + k );
        buttonPanel.add(switchButton[k]);
    }

    frame.add(labelPanel, BorderLayout.NORTH); 

    frame.add(lightPnl, BorderLayout.CENTER);
    //lightPnl.add(light1, BorderLayout.CENTER);

    frame.add(buttonPanel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

    frame.setVisible(true); //Displays the frame
    frame.pack(); //Makes the frame as big as it needs to be.


}//LightsGame()

private class ButtonHandler implements ActionListener {

    public void actionPerformed(ActionEvent e){

     if( e.getSource() == switchButton[0])  // toggle lights controlled by button 0

        //Switch the light on or off
        lightPnl.toggleLight();
    }//actionPerformed

}//ButtonHandler

private class MenuHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //Stop the Program
        System.exit(0);
    }//ActionPerformed

}//MenuHandler

public static void main(String[] args){

    //JLabel label = new JLabel("Hello");  // ?
    LightsGame game = new LightsGame();


}//main

 }//LightsGame

and I am using this LightsPanel class (that needs work too):

 import javax.swing.*;
 import java.awt.*;

 public class LightsPanel extends JPanel{

//Background color
private final Color backClr = Color.LIGHT_GRAY,
//Outline color
outlineClr = Color.BLACK,
//Color of the light when it is on
onClr = Color.YELLOW,
//Color of the light when it is off
offClr = Color.GRAY;

private boolean switchedOn;    //  ?
    boolean[] array = new boolean[6];   

public LightsPanel(){
    super(); 
    setPreferredSize(new Dimension(800, 200));
    boolean[] switchedOn = new boolean[6];  //  ?
    //switchedOn = false;       

}//LightsPanel()

public void paintComponent(Graphics gc){
    super.paintComponent(gc);

    Graphics2D l = (Graphics2D)gc;

    //Draw the background
    setBackground(backClr);

    //Draw the light
    if (switchedOn)
        l.setColor(onClr);
    else
        l.setColor(offClr);
        l.fillOval(75, 75, 50, 50);

    //Draw the outline of the light 
    l.setColor(outlineClr);
    l.drawOval(75, 75, 50, 50);

}//paintComponent   

public void toggleLight() {

//Change the state of the light
if(switchedOn)
    switchedOn = false;
else
    switchedOn = true; 

//Redraw the panel
repaint();

}//toggleLight

public void allLightsOn() {

}//allLightsOn
 }//LightsPanel

Right now I am stuck trying to make a loop to display 6 lights and have them work with 6 buttons. I cannot get the buttons to show up either. I need to add buttons to the buttonPanel but I'm not sure how to do that.

My buttons should toggle and correspond to certain lights..

Button 0 : lights 0, 2
Button 1 : lights 1, 3
Button 2 : lights 0, 1, 2, 3, 5
Button 3 : lights 0, 1, 2, 4, 5
Button 4 : lights 2, 4
Button 5 : lights 3, 5

If anyone is willing or has time to help me with this it would be greatly appreciated. Thank you!

Basically, you're not adding any buttons to anything...

You have this great loop, which seems to make sense to build your logic here...

for (int k = 0; k <= 5; k++){
    switchButton[k] = new JButton("Light" + k );
}

For example...

for (int k = 0; k < switchButton.length; k++){
    switchButton[k] = new JButton("Light" + k );
    buttonPane.add(switchButton[k]);
}

Now the next time you need to attach some kind of handler to the button so you can change the light state accordingly...

For this, you will need to take a look at How to use buttons and How to write an action listener .

Personally, I would make the LightsPanel responsible for a single light and simply create as many instance of this panel as you need. Then you could simply create a switch method which turns the light on or off as required...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM