Question: In this lab, you will create a GUI modeled after Lite-Brite. This lab introduces several concepts that you will need in your final project, so
In this lab, you will create a GUI modeled after Lite-Brite. This lab introduces several concepts that you will need in your final project, so make sure you understand why you are doing things.
Objectives
Write your first Graphical User Interface (GUI) in Java.
Design and write classes to model your GUI data.
Manage the GUI layout using sub-panels.
Use a GridLayout for displaying a 2D array of objects.
Use JButtons and JPanels.
Implement and add action listeners to GUI elements to handle user interaction events.
Getting Started Create a lab12 project in Eclipse.
Import LiteBrite.java You will be using the existing LiteBrite class and writing your own LiteBriteBoard and LitePeg classes.
Specifications
LiteBrite.java The LiteBrite class is already complete, but it will not compile until you implement your LiteBriteBoard and LitePeg classes. LiteBriteBoard and LitePeg Create classes for LitePeg and LiteBriteBoard. LitePeg will represent the state of a single peg button. Implement the methods as shown in the class design below. LiteBriteBoard will represent the state of the entire board. Implement the methods as shown in the class design below. The following example may be helpful when completing this lab: colorchooser
LiteBrite.java code below
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * Represents a LiteBrite game board GUI. * * @author CS121 Instructors */ @SuppressWarnings("serial") public class LiteBrite extends JPanel { private LiteBriteBoard board; private JButton resetButton; /** * Creates a new LiteBrite GUI with specified width and height. * @param width The number of pegs in the horizontal axis. * @param height The number of pegs in the vertical axis. */ public LiteBrite(int width, int height) { // Create new LiteBriteBoard with specified dimensions board = new LiteBriteBoard(width, height); // Create reset button and add ActionListener to it. resetButton = new JButton("Reset"); resetButton.addActionListener(new ResetButtonListener()); // Add sub-components to this main panel. this.add(board.getJPanel()); this.add(resetButton); } /** * The ActionListener for the button to reset the game. */ private class ResetButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { board.reset(); } } /** * Creates a JFrame and adds the main JPanel to the JFrame. * @param args (unused) */ public static void main(String args[]) { JFrame frame = new JFrame("Lite Brite"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new LiteBrite(20, 20)); frame.pack(); frame.setVisible(true); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
